Sha256: 4c59952aa3990762d679d371676efa1097844b2f81ff13d8a701f28cafcc6f1d

Contents?: true

Size: 893 Bytes

Versions: 12

Compression:

Stored size: 893 Bytes

Contents

require 'openssl'
require 'digest'
require 'base64'

module Keen
  module AESHelper

    BLOCK_SIZE = 32

    def aes256_encrypt(key, plaintext)
      aes = OpenSSL::Cipher::AES.new(256, :CBC)
      aes.encrypt
      aes.key = key
      iv = aes.random_iv
      [aes.update(plaintext) + aes.final, iv]
    end

    def aes256_decrypt(key, iv_plus_encrypted)
      iv = iv_plus_encrypted[0, 16]
      encrypted = iv_plus_encrypted[16, iv_plus_encrypted.length]
      aes = OpenSSL::Cipher::AES.new(256, :CBC)
      aes.decrypt
      aes.key = key
      aes.iv = iv
      aes.update(encrypted) + aes.final
    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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
keen-0.9.0 lib/keen/aes_helper.rb
keen-0.8.10 lib/keen/aes_helper.rb
keen-0.8.9 lib/keen/aes_helper.rb
keen-0.8.8 lib/keen/aes_helper.rb
keen-0.8.7 lib/keen/aes_helper.rb
keen-0.8.6 lib/keen/aes_helper.rb
keen-0.8.5 lib/keen/aes_helper.rb
keen-0.8.4 lib/keen/aes_helper.rb
keen-0.8.3 lib/keen/aes_helper.rb
keen-0.8.2 lib/keen/aes_helper.rb
keen-0.8.1 lib/keen/aes_helper.rb
keen-0.8.0 lib/keen/aes_helper.rb