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.
445 446 447 |
# File 'lib/ronin/support/crypto.rb', line 445 def self.aes128_cipher(**kwargs) Cipher::AES128.new(**kwargs) end |
.aes128_decrypt(data, **kwargs) ⇒ String
Decrypts data using AES-128.
519 520 521 |
# File 'lib/ronin/support/crypto.rb', line 519 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.
482 483 484 |
# File 'lib/ronin/support/crypto.rb', line 482 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.
556 557 558 |
# File 'lib/ronin/support/crypto.rb', line 556 def self.aes256_cipher(**kwargs) Cipher::AES256.new(**kwargs) end |
.aes256_decrypt(data, **kwargs) ⇒ String
Decrypts data using AES-256.
630 631 632 |
# File 'lib/ronin/support/crypto.rb', line 630 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.
593 594 595 |
# File 'lib/ronin/support/crypto.rb', line 593 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.
328 329 330 |
# File 'lib/ronin/support/crypto.rb', line 328 def self.aes_cipher(**kwargs) Cipher::AES.new(**kwargs) end |
.aes_decrypt(data, **kwargs) ⇒ String
Decrypts data using AES.
408 409 410 |
# File 'lib/ronin/support/crypto.rb', line 408 def self.aes_decrypt(data,**kwargs) self.aes_cipher(direction: :decrypt, **kwargs).decrypt(data) end |
.aes_encrypt(data, **kwargs) ⇒ String
Encrypts data using AES.
368 369 370 |
# File 'lib/ronin/support/crypto.rb', line 368 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.
210 211 212 |
# File 'lib/ronin/support/crypto.rb', line 210 def self.cipher(name,**kwargs) Cipher.new(name,**kwargs) end |
.ciphers ⇒ Array<String>
The list of supported ciphers.
167 168 169 |
# File 'lib/ronin/support/crypto.rb', line 167 def self.ciphers Cipher.supported end |
.decrypt(data, cipher:, **kwargs) ⇒ String
Decrypts data using the cipher.
288 289 290 |
# File 'lib/ronin/support/crypto.rb', line 288 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.
249 250 251 |
# File 'lib/ronin/support/crypto.rb', line 249 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.
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 |
# File 'lib/ronin/support/crypto.rb', line 760 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.
727 728 729 730 731 |
# File 'lib/ronin/support/crypto.rb', line 727 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.
693 694 695 696 697 |
# File 'lib/ronin/support/crypto.rb', line 693 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.
651 652 653 654 655 656 657 658 659 660 661 662 663 |
# File 'lib/ronin/support/crypto.rb', line 651 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.
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 |
# File 'lib/ronin/support/crypto.rb', line 800 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 |