nupp.mac
Keyed hashes, for bytes somebody could have tampered with.
Same shape as nupp.digest, plus a key.
const mac = nupp.mac
print(mac.hexDigest("hmac-sha256", key, "hello"))
local running = mac.newMac("hmac-sha256", key)
running:write("hel")
running:write("lo")
print(running:hexDigest())The key is raw bytes. A context is an io.Writer, as a digest is. Finalizing consumes the context; create another context to authenticate another message.
Built-in algorithms#
| Name | Output | Construction |
|---|---|---|
hmac-sha256 |
32 bytes | HMAC over SHA-256 |
hmac-sha256 names the HMAC construction over SHA-256. Smithy's httpChecksum trait does not define MAC algorithms. AWS KMS uses HMAC_SHA_256 for this construction, while AWS4-HMAC-SHA256 identifies the complete SigV4 signing scheme.
A host may supply its own catalog through nupp.mac.spi, chosen by priority when this module initializes. It is read once, at load.
Submodules
| Module | Description |
|---|---|
nupp.mac.spi | Implementation interfaces for keyed authentication algorithm catalogs. |
Module contents
Types
| Type | Kind | Description |
|---|---|---|
Algorithm | record | Immutable MAC metadata and a keyed context factory. |
Mac | interface | An owned keyed context with consuming digest finalization. |
Functions
| Function | Kind | Description |
|---|---|---|
algorithm | function | Requires an available MAC descriptor. |
algorithms | function | Lists available canonical MAC names in ascending order. |
digest | function | Computes a raw message authentication code. |
hexDigest | function | Computes a lowercase hexadecimal message authentication code. |
lookup | function | Looks up MAC metadata before initializing a keyed context. |
newMac | function | Creates independent owned state initialized with a key. |
Types#
Algorithmrecord#
record Algorithm ...Immutable MAC metadata and a keyed context factory.
Members
| Name | Kind | Description |
|---|---|---|
name | field | Canonical authentication algorithm name. |
digestSize | field | Fixed authentication output size in bytes. |
implementation | field | |
newMac | method | Creates independent owned state initialized with the raw-byte key. |
Macinterface#
interface Mac is Writer ...An owned keyed context with consuming digest finalization.
Members
| Name | Kind | Description |
|---|---|---|
close | method | Consumes an unfinished context and releases provider state without suspension. |
algorithm | method | Returns the canonical algorithm name retained at creation. |
digestSize | method | Returns the fixed output size in bytes. |
write | method | Absorbs raw bytes without retaining a borrowed input. |
writeSpan | method | Absorbs a borrowed byte view without retaining it. |
flush | method | Nothing is buffered behind a digest, so this only returns true. |
digest | method | Consumes this context and returns raw bytes, or writes the fixed-size prefix of destination and returns its byte count. |
hexDigest | method | Consumes this context and returns lowercase hexadecimal, always closing state. |
closemethod#
Consumes an unfinished context and releases provider state without suspension.
Arguments
| Name | Type | Description |
|---|---|---|
takes self | Mac |
Returns
| Type | Description |
|---|---|
nil |
algorithmmethod#
Returns the canonical algorithm name retained at creation.
Arguments
| Name | Type | Description |
|---|---|---|
self | Mac |
Returns
| Type | Description |
|---|---|
string |
digestSizemethod#
digestSize: function(self: Digest): integerReturns the fixed output size in bytes.
Arguments
| Name | Type | Description |
|---|---|---|
self | Mac |
Returns
| Type | Description |
|---|---|
integer |
writemethod#
write: function(exclusive self: Digest, bytes: string): (boolean, string?)Absorbs raw bytes without retaining a borrowed input.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive self | Mac | |
bytes | string |
Returns
| Type | Description |
|---|---|
boolean | true, since a digest takes every byte it is given |
string? |
writeSpanmethod#
Absorbs a borrowed byte view without retaining it.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive self | Mac | |
borrows bytes | span.ByteSpan |
Returns
| Type | Description |
|---|---|
integer? | how many bytes it absorbed, which is all of them |
string? |
flushmethod#
flush: function(self: Digest): (boolean, string?)Nothing is buffered behind a digest, so this only returns true.
Arguments
| Name | Type | Description |
|---|---|---|
self | Mac |
Returns
| Type | Description |
|---|---|
boolean | |
string? |
digestmethod#
digest: function(takes self: Digest): string
& function(takes self: Digest, exclusive destination: span.ByteWriteSpan): integerConsumes this context and returns raw bytes, or writes the fixed-size prefix of destination and returns its byte count. Always closes state.
Arguments
| Name | Type | Description |
|---|---|---|
takes self | Mac | |
exclusive destination | span.ByteWriteSpan |
Returns
| Type | Description |
|---|---|
integer |
Functions#
algorithmfunction#
Requires an available MAC descriptor.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical MAC algorithm name |
Returns
| Type | Description |
|---|---|
Algorithm | the selected descriptor |
Raises
| Type | Condition |
|---|---|
string | when the algorithm is unavailable |
algorithmsfunction#
function algorithms(): {string}Lists available canonical MAC names in ascending order.
Returns
| Type | Description |
|---|---|
{string} | the available names |
digestfunction#
Computes a raw message authentication code.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical MAC algorithm name |
key | string | the raw-byte secret key |
bytes | string | the message bytes |
Returns
| Type | Description |
|---|---|
string | the raw fixed-size authentication code |
Raises
| Type | Condition |
|---|---|
string | when lookup or a provider operation fails |
hexDigestfunction#
Computes a lowercase hexadecimal message authentication code.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical MAC algorithm name |
key | string | the raw-byte secret key |
bytes | string | the message bytes |
Returns
| Type | Description |
|---|---|
string | the lowercase hexadecimal authentication code |
Raises
| Type | Condition |
|---|---|
string | when lookup or a provider operation fails |
lookupfunction#
Looks up MAC metadata before initializing a keyed context.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical MAC algorithm name |
Returns
| Type | Description |
|---|---|
Algorithm? | the descriptor, or nil when unavailable |
newMacfunction#
Creates independent owned state initialized with a key.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical MAC algorithm name |
key | string | the raw-byte secret key |
Returns
| Type | Description |
|---|---|
Mac | an unfinished keyed context |
Raises
| Type | Condition |
|---|---|
string | when lookup or provider initialization fails |