Sha256: ea6a5e99f004ad7d1433a92864b3f1f5d6bdcdb83d411eb0813fc5555eaffab9

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

module Xmlenc
  module Algorithms
    class AESCBC
      class << self
        def [](size)
          new(size)
        end
      end

      def initialize(size)
        @size = size
      end

      def setup(key = nil)
        @cipher= nil
        @iv    = nil
        @key   = key || cipher.random_key
        self
      end

      def decrypt(cipher_value, options = {})
        cipher.decrypt
        cipher.padding = 0
        cipher.key     = @key
        cipher.iv      = cipher_value[0...iv_len]
        result         = cipher.update(cipher_value[iv_len..-1]) << cipher.final

        padding_size   = result.last.unpack('c').first
        result[0...-padding_size]
      end

      def encrypt(data, options = {})
        cipher.encrypt
        cipher.key = @key
        cipher.iv  = iv
        iv << cipher.update(data) << cipher.final
      end

      def key
        @key
      end

      private

      def iv
        @iv ||= cipher.random_iv
      end

      def iv_len
        cipher.iv_len
      end

      def cipher
        @cipher ||= OpenSSL::Cipher.new("aes-#{@size}-cbc")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
xmlenc-0.8.0 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.7.1 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.7.0 lib/xmlenc/algorithms/aes_cbc.rb