Sha256: 35450c7878370970c66ddf22c2ef3b6310bffd09e078f4b5e502bd8b34ac13b6

Contents?: true

Size: 1009 Bytes

Versions: 3

Compression:

Stored size: 1009 Bytes

Contents

module Rmega
  module Crypto
    module AesCbc
      def aes_cbc_cipher
        OpenSSL::Cipher::AES.new(128, :CBC)
      end

      def aes_cbc_encrypt(key, data)
        cipher = aes_cbc_cipher
        cipher.encrypt
        cipher.padding = 0
        cipher.key = key
        return cipher.update(data) + cipher.final
      end

      def aes_cbc_decrypt(key, data)
        cipher = aes_cbc_cipher
        cipher.decrypt
        cipher.padding = 0
        cipher.key = key
        return cipher.update(data) + cipher.final
      end

      def aes_cbc_mac(key, data, iv)
        cipher = aes_cbc_cipher
        cipher.encrypt
        cipher.padding = 0
        cipher.iv = iv if iv
        cipher.key = key

        n = 0
        mac = nil

        loop do
          block = data[n..n+15]
          break if !block or block.empty?
          block << "\x0"*(16-block.size) if block.size < 16
          n += 16
          mac = cipher.update(block)
        end

        return mac
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rmega-0.2.2 lib/rmega/crypto/aes_cbc.rb
rmega-0.2.1 lib/rmega/crypto/aes_cbc.rb
rmega-0.2.0 lib/rmega/crypto/aes_cbc.rb