Class: RbNaCl::HMAC::SHA512256

Inherits:
Auth
  • Object
show all
Extended by:
Sodium
Defined in:
lib/rbnacl/hmac/sha512256.rb

Overview

Computes an authenticator as HMAC-SHA-512 truncated to 256-bits

The authenticator can be used at a later time to verify the provenance of the message by recomputing the HMAC over the message and then comparing it to the provided authenticator. The class provides methods for generating signatures and also has a constant-time implementation for checking them.

This is a secret key authenticator, i.e. anyone who can verify signatures can also create them.

Constant Summary

Constants inherited from Auth

Auth::BYTES, Auth::KEYBYTES

Instance Method Summary collapse

Methods included from Sodium

primitive, sodium_constant, sodium_function, sodium_function_with_return_code, sodium_primitive, sodium_type

Methods inherited from Auth

auth, #auth, #key_bytes, key_bytes, #primitive, #tag_bytes, tag_bytes, verify, #verify

Constructor Details

#initialize(key) ⇒ SHA512256

Create instance without checking key length

RFC 2104 HMAC The key for HMAC can be of any length.

see https://tools.ietf.org/html/rfc2104#section-3


43
44
45
46
47
48
49
# File 'lib/rbnacl/hmac/sha512256.rb', line 43

def initialize(key)
  @key = Util.check_hmac_key(key, "#{self.class} key")
  @state = State.new
  @authenticator = Util.zeros(tag_bytes)

  self.class.auth_hmacsha512256_init(@state, key, key.bytesize)
end

Instance Method Details

#digestString

Return the authenticator, as raw bytes

Returns:

  • (String)

    The authenticator, as raw bytes


64
65
66
# File 'lib/rbnacl/hmac/sha512256.rb', line 64

def digest
  @authenticator
end

#hexdigestString

Return the authenticator, as hex string

Returns:

  • (String)

    The authenticator, as hex string


71
72
73
# File 'lib/rbnacl/hmac/sha512256.rb', line 71

def hexdigest
  @authenticator.unpack("H*").last
end

#update(message) ⇒ Object

Compute authenticator for message


54
55
56
57
58
59
# File 'lib/rbnacl/hmac/sha512256.rb', line 54

def update(message)
  self.class.auth_hmacsha512256_update(@state, message, message.bytesize)
  self.class.auth_hmacsha512256_final(@state.clone, @authenticator)

  hexdigest
end