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

ModuleDescription
nupp.mac.spiImplementation interfaces for keyed authentication algorithm catalogs.

Module contents

Types

TypeKindDescription
AlgorithmrecordImmutable MAC metadata and a keyed context factory.
MacinterfaceAn owned keyed context with consuming digest finalization.

Functions

FunctionKindDescription
algorithmfunctionRequires an available MAC descriptor.
algorithmsfunctionLists available canonical MAC names in ascending order.
digestfunctionComputes a raw message authentication code.
hexDigestfunctionComputes a lowercase hexadecimal message authentication code.
lookupfunctionLooks up MAC metadata before initializing a keyed context.
newMacfunctionCreates independent owned state initialized with a key.

Types#

Algorithmrecord#

record Algorithm ...

Immutable MAC metadata and a keyed context factory.

Members

NameKindDescription
namefieldCanonical authentication algorithm name.
digestSizefieldFixed authentication output size in bytes.
implementationfield
newMacmethodCreates independent owned state initialized with the raw-byte key.

namefield#

name: string
@readonly

Canonical authentication algorithm name.

digestSizefield#

digestSize: integer
@readonly

Fixed authentication output size in bytes.

implementationfield#

implementation: provider.Algorithm
@private

newMacmethod#

newMac: function newMac(self, key: string): Mac

Creates independent owned state initialized with the raw-byte key.

Arguments
NameTypeDescription
selfany
keystring
Returns
TypeDescription
Mac

the unfinished keyed context

Raises
TypeCondition
string

when provider initialization fails

Macinterface#

interface Mac is Writer ...

An owned keyed context with consuming digest finalization.

Members

NameKindDescription
closemethodConsumes an unfinished context and releases provider state without suspension.
algorithmmethodReturns the canonical algorithm name retained at creation.
digestSizemethodReturns the fixed output size in bytes.
writemethodAbsorbs raw bytes without retaining a borrowed input.
writeSpanmethodAbsorbs a borrowed byte view without retaining it.
flushmethodNothing is buffered behind a digest, so this only returns true.
digestmethodConsumes this context and returns raw bytes, or writes the fixed-size prefix of destination and returns its byte count.
hexDigestmethodConsumes this context and returns lowercase hexadecimal, always closing state.

closemethod#

close: @nosuspend function(takes self: Digest): nil

Consumes an unfinished context and releases provider state without suspension.

Arguments
NameTypeDescription
takes selfMac
Returns
TypeDescription
nil

algorithmmethod#

algorithm: function(self: Digest): string

Returns the canonical algorithm name retained at creation.

Arguments
NameTypeDescription
selfMac
Returns
TypeDescription
string

digestSizemethod#

digestSize: function(self: Digest): integer

Returns the fixed output size in bytes.

Arguments
NameTypeDescription
selfMac
Returns
TypeDescription
integer

writemethod#

write: function(exclusive self: Digest, bytes: string): (boolean, string?)

Absorbs raw bytes without retaining a borrowed input.

Arguments
NameTypeDescription
exclusive selfMac
bytesstring
Returns
TypeDescription
boolean

true, since a digest takes every byte it is given

string?

writeSpanmethod#

writeSpan: function(exclusive self: Digest, borrows bytes: span.ByteSpan): (integer?, string?)

Absorbs a borrowed byte view without retaining it.

Arguments
NameTypeDescription
exclusive selfMac
borrows bytesspan.ByteSpan
Returns
TypeDescription
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
NameTypeDescription
selfMac
Returns
TypeDescription
boolean
string?

digestmethod#

digest: function(takes self: Digest): string
& function(takes self: Digest, exclusive destination: span.ByteWriteSpan): integer

Consumes this context and returns raw bytes, or writes the fixed-size prefix of destination and returns its byte count. Always closes state.

Arguments
NameTypeDescription
takes selfMac
exclusive destinationspan.ByteWriteSpan
Returns
TypeDescription
integer

hexDigestmethod#

hexDigest: function(takes self: Digest): string

Consumes this context and returns lowercase hexadecimal, always closing state.

Arguments
NameTypeDescription
takes selfMac
Returns
TypeDescription
string

Functions#

algorithmfunction#

function algorithm(name: string): Algorithm

Requires an available MAC descriptor.

Arguments

NameTypeDescription
namestring

the canonical MAC algorithm name

Returns

TypeDescription
Algorithm

the selected descriptor

Raises

TypeCondition
string

when the algorithm is unavailable

algorithmsfunction#

function algorithms(): {string}

Lists available canonical MAC names in ascending order.

Returns

TypeDescription
{string}

the available names

digestfunction#

function digest(name: string, key: string, bytes: string): string

Computes a raw message authentication code.

Arguments

NameTypeDescription
namestring

the canonical MAC algorithm name

keystring

the raw-byte secret key

bytesstring

the message bytes

Returns

TypeDescription
string

the raw fixed-size authentication code

Raises

TypeCondition
string

when lookup or a provider operation fails

hexDigestfunction#

function hexDigest(name: string, key: string, bytes: string): string

Computes a lowercase hexadecimal message authentication code.

Arguments

NameTypeDescription
namestring

the canonical MAC algorithm name

keystring

the raw-byte secret key

bytesstring

the message bytes

Returns

TypeDescription
string

the lowercase hexadecimal authentication code

Raises

TypeCondition
string

when lookup or a provider operation fails

lookupfunction#

function lookup(name: string): Algorithm?

Looks up MAC metadata before initializing a keyed context.

Arguments

NameTypeDescription
namestring

the canonical MAC algorithm name

Returns

TypeDescription
Algorithm?

the descriptor, or nil when unavailable

newMacfunction#

function newMac(name: string, key: string): Mac

Creates independent owned state initialized with a key.

Arguments

NameTypeDescription
namestring

the canonical MAC algorithm name

keystring

the raw-byte secret key

Returns

TypeDescription
Mac

an unfinished keyed context

Raises

TypeCondition
string

when lookup or provider initialization fails