Sha256: 7d1f8df9955b4ca297c963ffd85a221ef560b73b3c7c25d7ae4cc9163219b5f3

Contents?: true

Size: 1.11 KB

Versions: 15

Compression:

Stored size: 1.11 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::Cipher.new("aes-#{@size}-cbc")
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
xmlenc-0.6.9 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.6.8 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.6.6 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.6.5 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.6.4 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.6.3 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.6.2 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.6.1 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.6.0 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.5.0 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.4.1 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.4.0 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.3.0 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.2.1 lib/xmlenc/algorithms/aes_cbc.rb
xmlenc-0.2.0 lib/xmlenc/algorithms/aes_cbc.rb