Sha256: 40ce116ca04c174d0eb9d20e04e36e6acc1d264cf8cc7faf33ca15a8c51d2fa1

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

require_relative '../data_conversion'

module Ccrypto
  module Ruby
    class HMACEngine
      include TR::CondUtils
      include DataConversion

      def initialize(*args,&block)
        @config = args.first

        raise HMACEngineException, "HMAC config is expected" if not @config.is_a?(Ccrypto::HMACConfig) 

        raise HMACEngineException, "Signing key is required" if is_empty?(@config.key)
        raise HMACEngineException, "Secret key as signing key is required. Given #{@config.key.class}" if not @config.key.is_a?(Ccrypto::SecretKey)

        raise HMACEngineException, "Digest '#{@config.digest}' is not supported" if not DigestEngine.is_supported?(@config.digest)
       
        dig = DigestEngine.digest(@config.digest)

        @hmac = OpenSSL::HMAC.new(@config.key.to_bin, dig.native_digest_engine)

      end

      def hmac_update(val)
        @hmac.update(val) 
      end

      def hmac_final
        @hmac.digest
      end

      def hmac_digest(val, output = :binary)
        hmac_update(val)
        res = hmac_final

        @hmac.reset

        case output
        when :hex
          to_hex(res)
        when :b64
          to_b64(res)
        else
          res
        end
      end


    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ccrypto-ruby-0.1.2 lib/ccrypto/ruby/engines/hmac_engine.rb
ccrypto-ruby-0.1.1 lib/ccrypto/ruby/engines/hmac_engine.rb
ccrypto-ruby-0.1.0 lib/ccrypto/ruby/engines/hmac_engine.rb