Sha256: f9843e91cb6105b078872a6645cec0132e8d9e6bb13763ae6b35d54a1c280ef1

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require_relative '../data_conversion'

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

      include TeLogger::TeLogHelper
      teLogger_tag :j_hmac

      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)

        teLogger.debug "Config : #{@config.inspect}"
        begin
          macAlgo = to_jce_spec(@config)
          teLogger.debug "Mac algo : #{macAlgo}"
          @hmac = javax.crypto.Mac.getInstance(to_jce_spec(@config))
          @hmac.init(@config.key.to_jce_secret_key)
        rescue Exception => ex
          raise HMACEngineException, ex
        end

      end

      def hmac_update(val)
        @hmac.update(to_java_bytes(val)) if not_empty?(val)
      end

      def hmac_final
        @hmac.doFinal 
      end

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

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


      private
      def to_jce_spec(config)
        res = []
        res << "HMAC"

        salgo = config.digest.to_s
        if salgo =~ /_/
          res << salgo.gsub("_","-").upcase
        else
          res << salgo.upcase
        end
       
        res.join

      end


    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ccrypto-java-0.1.0 lib/ccrypto/java/engines/hmac_engine.rb