Class: COSE::Algorithm::HMAC

Inherits:
Base
  • Object
show all
Defined in:
lib/cose/algorithm/hmac.rb

Constant Summary collapse

BYTE_LENGTH =
8

Instance Attribute Summary collapse

Attributes inherited from Base

#id, #name

Instance Method Summary collapse

Constructor Details

#initialize(*args, hash_function:, tag_length:) ⇒ HMAC

Returns a new instance of HMAC.



13
14
15
16
17
18
# File 'lib/cose/algorithm/hmac.rb', line 13

def initialize(*args, hash_function:, tag_length:)
  super(*args)

  @hash_function = hash_function
  @tag_length = tag_length
end

Instance Attribute Details

#hash_functionObject (readonly)

Returns the value of attribute hash_function.



11
12
13
# File 'lib/cose/algorithm/hmac.rb', line 11

def hash_function
  @hash_function
end

#tag_lengthObject (readonly)

Returns the value of attribute tag_length.



11
12
13
# File 'lib/cose/algorithm/hmac.rb', line 11

def tag_length
  @tag_length
end

Instance Method Details

#mac(key, to_be_signed) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/cose/algorithm/hmac.rb', line 20

def mac(key, to_be_signed)
  mac = OpenSSL::HMAC.digest(hash_function, key, to_be_signed)

  if tag_bytesize && tag_bytesize < mac.bytesize
    mac.byteslice(0, tag_bytesize)
  else
    mac
  end
end