Class: Keen::AESHelper
- Inherits:
-
Object
- Object
- Keen::AESHelper
- Defined in:
- lib/keen/aes_helper.rb
Class Method Summary collapse
- .aes256_decrypt(key, iv_plus_encrypted) ⇒ Object
- .aes256_encrypt(key, plaintext, iv = nil) ⇒ Object
- .hexlify(msg) ⇒ Object
- .unhexlify(msg) ⇒ Object
Class Method Details
.aes256_decrypt(key, iv_plus_encrypted) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/keen/aes_helper.rb', line 9 def aes256_decrypt(key, iv_plus_encrypted) aes = OpenSSL::Cipher::AES.new(256, :CBC) iv = iv_plus_encrypted[0, aes.key_len] encrypted = iv_plus_encrypted[aes.key_len, iv_plus_encrypted.length] aes.decrypt aes.key = unhexlify(key) aes.iv = unhexlify(iv) aes.update(unhexlify(encrypted)) + aes.final end |
.aes256_encrypt(key, plaintext, iv = nil) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/keen/aes_helper.rb', line 19 def aes256_encrypt(key, plaintext, iv = nil) raise OpenSSL::Cipher::CipherError.new("iv must be 16 bytes") if !iv.nil? && iv.length != 16 aes = OpenSSL::Cipher::AES.new(256, :CBC) aes.encrypt aes.key = unhexlify(key) aes.iv = iv unless iv.nil? iv ||= aes.random_iv hexlify(iv) + hexlify(aes.update(plaintext) + aes.final) end |
.hexlify(msg) ⇒ Object
29 30 31 |
# File 'lib/keen/aes_helper.rb', line 29 def hexlify(msg) msg.unpack('H*')[0] end |
.unhexlify(msg) ⇒ Object
33 34 35 |
# File 'lib/keen/aes_helper.rb', line 33 def unhexlify(msg) [msg].pack('H*') end |