Class: Ccrypto::Ruby::HMACEngine

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

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_hex, included, #to_b64, #to_hex, #to_int_array

Constructor Details

#initialize(*args, &block) ⇒ HMACEngine

Returns a new instance of HMACEngine.

Raises:

  • (HMACEngineException)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ccrypto/ruby/engines/hmac_engine.rb', line 10

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

Instance Method Details

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ccrypto/ruby/engines/hmac_engine.rb', line 34

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

#hmac_finalObject



30
31
32
# File 'lib/ccrypto/ruby/engines/hmac_engine.rb', line 30

def hmac_final
  @hmac.digest
end

#hmac_update(val) ⇒ Object



26
27
28
# File 'lib/ccrypto/ruby/engines/hmac_engine.rb', line 26

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