Sha256: 627010d53058d9ec147b1f0b1210bc24b8e2f10f4f1a76a4217fe5631f525b87
Contents?: true
Size: 1.21 KB
Versions: 3
Compression:
Stored size: 1.21 KB
Contents
require 'openssl' require 'digest' require 'base64' module Keen class AESHelperOld BLOCK_SIZE = 32 class << self def aes256_decrypt(key, iv_plus_encrypted) padded_key = pad(key) unhexed_iv_plus_encrypted = unhexlify(iv_plus_encrypted) iv = unhexed_iv_plus_encrypted[0, 16] encrypted = unhexed_iv_plus_encrypted[16, unhexed_iv_plus_encrypted.length] aes = OpenSSL::Cipher::AES.new(256, :CBC) aes.decrypt aes.key = padded_key aes.iv = iv aes.update(encrypted) + aes.final end def aes256_encrypt(key, plaintext, iv = nil) padded_key = pad(key) aes = OpenSSL::Cipher::AES.new(256, :CBC) aes.encrypt aes.key = padded_key aes.iv = iv unless iv.nil? iv ||= aes.random_iv encrypted = aes.update(plaintext) + aes.final hexlify(iv) + hexlify(encrypted) end def hexlify(msg) msg.unpack('H*')[0] end def unhexlify(msg) [msg].pack('H*') end def pad(msg) pad_len = BLOCK_SIZE - (msg.length % BLOCK_SIZE) padding = pad_len.chr * pad_len padded = msg + padding padded end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
keen-0.9.7 | lib/keen/aes_helper_old.rb |
keen-0.9.6 | lib/keen/aes_helper_old.rb |
keen-0.9.5 | lib/keen/aes_helper_old.rb |