Sha256: 81d8ec8a2f1eae83e0c3ac6135a9eec8f268c5cecb33741817cd379a18b0fbdc

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module Xmlenc
  module Algorithms
    class AESGCM
      AUTH_TAG_LEN = 16

      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]
        cipher.auth_tag = cipher_value[-AUTH_TAG_LEN..-1]
        cipher.update(cipher_value[iv_len..-(AUTH_TAG_LEN + 1)]) << cipher.final
      end

      def encrypt(data, options = {})
        cipher.encrypt
        cipher.key       = @key
        cipher.iv        = iv
        cipher.auth_data = ''
        iv << (cipher.update(data) << cipher.final) << cipher.auth_tag
      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}-gcm")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xmlenc-0.8.0 lib/xmlenc/algorithms/aes_gcm.rb