Module: Ronin::Support::Crypto
- Defined in:
- lib/ronin/support/crypto.rb,
lib/ronin/support/crypto/key.rb,
lib/ronin/support/crypto/cert.rb,
lib/ronin/support/crypto/hmac.rb,
lib/ronin/support/crypto/mixin.rb,
lib/ronin/support/crypto/cipher.rb,
lib/ronin/support/crypto/key/dh.rb,
lib/ronin/support/crypto/key/ec.rb,
lib/ronin/support/crypto/key/dsa.rb,
lib/ronin/support/crypto/key/rsa.rb,
lib/ronin/support/crypto/cert_chain.rb,
lib/ronin/support/crypto/cipher/aes.rb,
lib/ronin/support/crypto/key/methods.rb,
lib/ronin/support/crypto/cipher/aes128.rb,
lib/ronin/support/crypto/cipher/aes256.rb
Overview
Crypto provides a nicer more user-friendly API ontop of OpenSSL.
Core-Ext Methods
- File.md5
- File.sha1
- File.sha128
- File.sha256
- File.sha512
- File.sha2
- File.sha5
- File.rmd160
- File.hmac
- File.encrypt
- File.decrypt
- File.aes_encrypt
- File.aes_decrypt
- File.aes128_encrypt
- File.aes128_decrypt
- File.aes256_encrypt
- File.aes256_decrypt
- File.rsa_encrypt
- File.rsa_decrypt
- String#md5
- String#sha1
- String#sha256
- String#sha512
- String#rmd160
- String#hmac
- String#encrypt
- String#decrypt
- String#aes_encrypt
- String#aes_decrypt
- String#aes128_encrypt
- String#aes128_decrypt
- String#aes256_encrypt
- String#aes256_decrypt
- String#rsa_encrypt
- String#rsa_decrypt
- String#rot
- String#xor
Defined Under Namespace
Modules: Key, Mixin Classes: Cert, CertChain, Cipher, HMAC
Class Method Summary collapse
-
.aes128_cipher(**kwargs) ⇒ Cipher::AES
Creates a new AES-128 cipher.
-
.aes128_decrypt(data, **kwargs) ⇒ String
Decrypts data using AES-128.
-
.aes128_encrypt(data, **kwargs) ⇒ String
Encrypts data using AES-128.
-
.aes256_cipher(**kwargs) ⇒ Cipher::AES
Creates a new AES-256 cipher.
-
.aes256_decrypt(data, **kwargs) ⇒ String
Decrypts data using AES-256.
-
.aes256_encrypt(data, **kwargs) ⇒ String
Encrypts data using AES-256.
-
.aes_cipher(**kwargs) ⇒ Cipher::AES
Creates a new AES cipher.
-
.aes_decrypt(data, **kwargs) ⇒ String
Decrypts data using AES.
-
.aes_encrypt(data, **kwargs) ⇒ String
Encrypts data using AES.
-
.Cert(cert) ⇒ Cert
Coerces a value into a Cert object.
-
.cipher(name, **kwargs) ⇒ OpenSSL::Cipher
Creates a cipher.
-
.ciphers ⇒ Array<String>
The list of supported ciphers.
-
.decrypt(data, cipher:, **kwargs) ⇒ String
Decrypts data using the cipher.
-
.digest(name) ⇒ OpenSSL::Digest
Looks up a digest.
-
.encrypt(data, cipher:, **kwargs) ⇒ String
Encrypts data using the cipher.
-
.hmac(data = nil, key:, digest: :sha1) {|hmac| ... } ⇒ OpenSSL::HMAC
Creates a new HMAC.
-
.Key(key) ⇒ RSA, ...
Coerces a value into a Key object.
-
.rot(string, n = 13, alphabets: [('A'..'Z').to_a, ('a'..'z').to_a, ('0'..'9').to_a]) ⇒ String
Rotates the characters in the given string using the given alphabet.
-
.rsa_decrypt(data, key: nil, key_file: nil, key_password: nil, **kwargs) ⇒ String
Decrypts data using a RSA key.
-
.rsa_encrypt(data, key: nil, key_file: nil, key_password: nil, **kwargs) ⇒ String
Encrypts data using a RSA key.
-
.rsa_key(key = nil, path: nil, password: nil) ⇒ Key::RSA
Loads an RSA key.
-
.xor(string, key) ⇒ String
XOR encodes the String.
Class Method Details
.aes128_cipher(**kwargs) ⇒ Cipher::AES
Creates a new AES-128 cipher.
443 444 445 |
# File 'lib/ronin/support/crypto.rb', line 443 def self.aes128_cipher(**kwargs) Cipher::AES128.new(**kwargs) end |
.aes128_decrypt(data, **kwargs) ⇒ String
Decrypts data using AES-128.
517 518 519 |
# File 'lib/ronin/support/crypto.rb', line 517 def self.aes128_decrypt(data,**kwargs) self.aes128_cipher(direction: :decrypt, **kwargs).decrypt(data) end |
.aes128_encrypt(data, **kwargs) ⇒ String
Encrypts data using AES-128.
480 481 482 |
# File 'lib/ronin/support/crypto.rb', line 480 def self.aes128_encrypt(data,**kwargs) self.aes128_cipher(direction: :encrypt, **kwargs).encrypt(data) end |
.aes256_cipher(**kwargs) ⇒ Cipher::AES
Creates a new AES-256 cipher.
554 555 556 |
# File 'lib/ronin/support/crypto.rb', line 554 def self.aes256_cipher(**kwargs) Cipher::AES256.new(**kwargs) end |
.aes256_decrypt(data, **kwargs) ⇒ String
Decrypts data using AES-256.
628 629 630 |
# File 'lib/ronin/support/crypto.rb', line 628 def self.aes256_decrypt(data,**kwargs) self.aes256_cipher(direction: :decrypt, **kwargs).decrypt(data) end |
.aes256_encrypt(data, **kwargs) ⇒ String
Encrypts data using AES-256.
591 592 593 |
# File 'lib/ronin/support/crypto.rb', line 591 def self.aes256_encrypt(data,**kwargs) self.aes256_cipher(direction: :encrypt, **kwargs).encrypt(data) end |
.aes_cipher(**kwargs) ⇒ Cipher::AES
Creates a new AES cipher.
326 327 328 |
# File 'lib/ronin/support/crypto.rb', line 326 def self.aes_cipher(**kwargs) Cipher::AES.new(**kwargs) end |
.aes_decrypt(data, **kwargs) ⇒ String
Decrypts data using AES.
406 407 408 |
# File 'lib/ronin/support/crypto.rb', line 406 def self.aes_decrypt(data,**kwargs) self.aes_cipher(direction: :decrypt, **kwargs).decrypt(data) end |
.aes_encrypt(data, **kwargs) ⇒ String
Encrypts data using AES.
366 367 368 |
# File 'lib/ronin/support/crypto.rb', line 366 def self.aes_encrypt(data,**kwargs) self.aes_cipher(direction: :encrypt, **kwargs).encrypt(data) end |
.Cert(cert) ⇒ Cert
Coerces a value into a Cert object.
554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/ronin/support/crypto/cert.rb', line 554 def self.Cert(cert) case cert when String then Cert.parse(cert) when Cert then cert when OpenSSL::X509::Certificate new_cert = Cert.allocate new_cert.send(:initialize_copy,cert) new_cert else raise(ArgumentError,"value must be either a String or a OpenSSL::X509::Certificate object: #{cert.inspect}") end end |
.cipher(name, **kwargs) ⇒ OpenSSL::Cipher
Creates a cipher.
208 209 210 |
# File 'lib/ronin/support/crypto.rb', line 208 def self.cipher(name,**kwargs) Cipher.new(name,**kwargs) end |
.ciphers ⇒ Array<String>
The list of supported ciphers.
165 166 167 |
# File 'lib/ronin/support/crypto.rb', line 165 def self.ciphers Cipher.supported end |
.decrypt(data, cipher:, **kwargs) ⇒ String
Decrypts data using the cipher.
286 287 288 |
# File 'lib/ronin/support/crypto.rb', line 286 def self.decrypt(data, cipher: ,**kwargs) self.cipher(cipher, direction: :decrypt, **kwargs).decrypt(data) end |
.digest(name) ⇒ OpenSSL::Digest
Looks up a digest.
96 97 98 |
# File 'lib/ronin/support/crypto.rb', line 96 def self.digest(name) OpenSSL::Digest.const_get(name.upcase) end |
.encrypt(data, cipher:, **kwargs) ⇒ String
Encrypts data using the cipher.
247 248 249 |
# File 'lib/ronin/support/crypto.rb', line 247 def self.encrypt(data, cipher: ,**kwargs) self.cipher(cipher, direction: :encrypt, **kwargs).encrypt(data) end |
.hmac(data = nil, key:, digest: :sha1) {|hmac| ... } ⇒ OpenSSL::HMAC
Creates a new HMAC.
139 140 141 142 143 144 145 146 147 |
# File 'lib/ronin/support/crypto.rb', line 139 def self.hmac(data=nil, key: , digest: :sha1) hmac = HMAC.new(key,digest(digest).new) if block_given? then yield hmac elsif data then hmac.update(data) end return hmac end |
.Key(key) ⇒ RSA, ...
Coerces a value into a Key object.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ronin/support/crypto/key.rb', line 130 def self.Key(key) case key when String then Key.parse(key) when OpenSSL::PKey::PKey key_class = case key when OpenSSL::PKey::RSA then Key::RSA when OpenSSL::PKey::DSA then Key::DSA when OpenSSL::PKey::DH then Key::DH when OpenSSL::PKey::EC then Key::EC else raise(NotImplementedError,"#{key.inspect} is not supported") end new_key = key_class.allocate new_key.send(:initialize_copy,key) new_key else raise(ArgumentError,"value must be either a String or a OpenSSL::PKey::PKey object: #{key.inspect}") end end |
.rot(string, n = 13, alphabets: [('A'..'Z').to_a, ('a'..'z').to_a, ('0'..'9').to_a]) ⇒ String
This method was added as a joke and should not be used for secure cryptographic communications.
Rotates the characters in the given string using the given alphabet.
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 |
# File 'lib/ronin/support/crypto.rb', line 758 def self.rot(string,n=13, alphabets: [('A'..'Z').to_a, ('a'..'z').to_a, ('0'..'9').to_a]) translation_table = {} alphabets.each do |alphabet| modulo = alphabet.count alphabet.each_with_index do |char,index| translation_table[char] = alphabet[(index + n) % modulo] end end new_string = String.new(encoding: string.encoding) string.each_char do |char| new_string << translation_table.fetch(char,char) end return new_string end |
.rsa_decrypt(data, key: nil, key_file: nil, key_password: nil, **kwargs) ⇒ String
Decrypts data using a RSA key.
Optional padding mode. `nil` and `false` will disable padding.
725 726 727 728 729 |
# File 'lib/ronin/support/crypto.rb', line 725 def self.rsa_decrypt(data, key: nil, key_file: nil, key_password: nil, **kwargs) rsa = rsa_key(key, path: key_file, password: key_password) return rsa.private_decrypt(data,**kwargs) end |
.rsa_encrypt(data, key: nil, key_file: nil, key_password: nil, **kwargs) ⇒ String
Encrypts data using a RSA key.
Optional padding mode. `nil` and `false` will disable padding.
691 692 693 694 695 |
# File 'lib/ronin/support/crypto.rb', line 691 def self.rsa_encrypt(data, key: nil, key_file: nil, key_password: nil, **kwargs) rsa = rsa_key(key, path: key_file, password: key_password) return rsa.public_encrypt(data,**kwargs) end |
.rsa_key(key = nil, path: nil, password: nil) ⇒ Key::RSA
Loads an RSA key.
649 650 651 652 653 654 655 656 657 658 659 660 661 |
# File 'lib/ronin/support/crypto.rb', line 649 def self.rsa_key(key=nil, path: nil, password: nil) if path Key::RSA.load_file(path, password: password) elsif key case key when Key::RSA then key when OpenSSL::PKey::RSA then Key::RSA.new(key) when String then Key::RSA.load(key, password: password) end else raise(ArgumentError,"either key: or key_file: keyword arguments must be given") end end |
.xor(string, key) ⇒ String
XOR encodes the String.
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
# File 'lib/ronin/support/crypto.rb', line 798 def self.xor(string,key) key = case key when Integer then [key] when String then key.bytes else key end key = key.cycle result = String.new(encoding: string.encoding) string.bytes.each do |b| result << (b ^ key.next).chr end return result end |