Class: Ccrypto::Java::HMACEngine

Inherits:
Object
  • Object
show all
Includes:
DataConversion, TR::CondUtils, TeLogger::TeLogHelper
Defined in:
lib/ccrypto/java/engines/hmac_engine.rb

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_hex, included, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str

Constructor Details

#initialize(*args, &block) ⇒ HMACEngine

Returns a new instance of HMACEngine.

Raises:

  • (HMACEngineException)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 13

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

Instance Method Details

#hmac_digest(val, output = :binary) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 41

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

#hmac_finalObject



37
38
39
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 37

def hmac_final
  @hmac.doFinal 
end

#hmac_update(val) ⇒ Object



33
34
35
# File 'lib/ccrypto/java/engines/hmac_engine.rb', line 33

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