class

Beryl::Crypto::V1::HashBasedMessageAuthenticationCode(D)

Inherits Digest < Reference < Object

HMAC (RFC 2104, FIPS 198-1): a message authentication code built on the hash function D. Someone who holds the key can produce the tag for a message; nobody else can produce or alter one.

D is any hash of this shard, for example HMAC(SHA256). The instance streams like a ::Digest: update any number of times, then final for the tag or verify to check a tag received from outside.

tag = Beryl::Crypto::V1::HMAC(Beryl::Crypto::V1::SHA256).authenticate("message", key: "secret key")

authentication = Beryl::Crypto::V1::HMAC(Beryl::Crypto::V1::SHA256).new("secret key")
authentication.update("mess").update("age")
authentication.verify(tag) # => true

The key is never kept: construction turns it into two keyed hash states and wipes the working copies. Checking a tag takes the same time whatever the key, the message or the candidate tag hold.

Constants

MINIMUM_TRUNCATED_TAG_BYTESIZE = 10

The shortest truncated tag verify accepts, in bytes: 80 bits (RFC 2104 §5).

Constructors

new(key : Bytes | String)

Creates an authentication under key. A key longer than the hash's block is hashed first, as RFC 2104 §2 requires; the key's length is the only thing about it that decides any step.

Source

Class methods

authenticate(message : Bytes | String, *, key : Bytes | String) : Bytes

Returns the authentication tag of message under key in one call.

Source
verify(message : Bytes | String, authentication_tag : Bytes, *, key : Bytes | String) : Bool

Returns whether authentication_tag is the tag of message under key, comparing in constant time.

Source

Instance methods

digest_size

Returns the number of bytes in a full authentication tag.

Source
dup

Returns an independent authentication under the same key that continues from the point this one has reached.

Source
verify(authentication_tag : Bytes, *, truncated_to_bytesize : Int32 = digest_size) : Bool

Finishes the authentication and returns whether authentication_tag is the tag of everything passed to update.

The comparison runs in constant time, and a tag of the wrong length is refused only after the same hashing and the same comparison as a tag of the right length.

truncated_to_bytesize checks a tag that the protocol cuts to its leading bytes (RFC 2104 §5). It must be at least half the full tag and at least MINIMUM_TRUNCATED_TAG_BYTESIZE; anything shorter raises TruncatedTagTooShort.

Source