nupp.digest

Cryptographic hashes: SHA-256 and the rest.

One call for a whole string, or a context you feed in pieces and then consume.

const digest = nupp.digest

print(digest.hexDigest("sha256", "hello"))

local running = digest.newDigest("sha256")

running:write("hel")
running:write("lo")

print(running:hexDigest())

A context is an io.Writer, so anything that fills a writer fills a digest: reader:transferTo(running) hashes a whole file or response body without holding it.

Finalizing consumes the context, so a digest is taken once and the binding is spent afterward. Create another context to digest another message. digest() returns raw bytes, digest(destination) writes them into a writable byte span you already have, and hexDigest() returns lowercase hexadecimal. Dropping a context that was never finalized closes its provider state.

MD5 and SHA-1 are here for talking to things that already use them, and for nothing else.

A host may supply its own catalog through nupp.digest.spi, chosen by priority when this module initializes. Its entries override the built-ins of the same name, the ones it does not name stay available, and a descriptor whose size disagrees with its algorithm is refused. The catalog is read once, at load, so no context operation resolves a service.

Submodules

ModuleDescription
nupp.digest.spiImplementation interfaces for nupp.digest algorithm catalogs.

Module contents

Types

TypeKindDescription
AlgorithmrecordImmutable algorithm metadata and a factory for independent digest contexts.
DigestinterfaceAn owned incremental digest, consumed by either finalization overload.

Functions

FunctionKindDescription
algorithmfunctionRequires an available algorithm descriptor.
algorithmsfunctionLists canonical available names in ascending order.
digestfunctionComputes a raw digest.
hexDigestfunctionComputes a lowercase hexadecimal digest.
lookupfunctionLooks up an algorithm without constructing mutable provider state.
newDigestfunctionCreates fresh incremental digest state.

Types#

Algorithmrecord#

record Algorithm ...

Immutable algorithm metadata and a factory for independent digest contexts.

Members

NameKindDescription
namefieldCanonical algorithm name.
digestSizefieldNumber of output bytes, available before creating mutable state.
implementationfield
newDigestmethodCreates independent owned state for this algorithm.

namefield#

name: string
@readonly

Canonical algorithm name.

digestSizefield#

digestSize: integer
@readonly

Number of output bytes, available before creating mutable state.

implementationfield#

implementation: provider.Algorithm
@private

newDigestmethod#

newDigest: function newDigest(self): Digest

Creates independent owned state for this algorithm.

Arguments
NameTypeDescription
selfany
Returns
TypeDescription
Digest

the unfinished digest context

Raises
TypeCondition
string

when provider initialization fails

Digestinterface#

interface Digest is Writer ...

An owned incremental digest, consumed by either finalization overload.

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 selfDigest
Returns
TypeDescription
nil

algorithmmethod#

algorithm: function(self: Digest): string

Returns the canonical algorithm name retained at creation.

Arguments
NameTypeDescription
selfDigest
Returns
TypeDescription
string

digestSizemethod#

digestSize: function(self: Digest): integer

Returns the fixed output size in bytes.

Arguments
NameTypeDescription
selfDigest
Returns
TypeDescription
integer

writemethod#

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

Absorbs raw bytes without retaining a borrowed input.

Arguments
NameTypeDescription
exclusive selfDigest
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 selfDigest
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
selfDigest
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 selfDigest
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 selfDigest
Returns
TypeDescription
string

Functions#

algorithmfunction#

function algorithm(name: string): Algorithm

Requires an available algorithm descriptor.

Arguments

NameTypeDescription
namestring

the canonical algorithm name

Returns

TypeDescription
Algorithm

the selected descriptor

Raises

TypeCondition
string

when the algorithm is unavailable

algorithmsfunction#

function algorithms(): {string}

Lists canonical available names in ascending order.

Returns

TypeDescription
{string}

the available canonical names

digestfunction#

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

Computes a raw digest.

Arguments

NameTypeDescription
namestring

the canonical algorithm name

bytesstring

the message bytes

Returns

TypeDescription
string

the raw fixed-size digest bytes

Raises

TypeCondition
string

when lookup or a provider operation fails

hexDigestfunction#

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

Computes a lowercase hexadecimal digest.

Arguments

NameTypeDescription
namestring

the canonical algorithm name

bytesstring

the message bytes

Returns

TypeDescription
string

the lowercase hexadecimal digest

Raises

TypeCondition
string

when lookup or a provider operation fails

lookupfunction#

function lookup(name: string): Algorithm?

Looks up an algorithm without constructing mutable provider state.

Arguments

NameTypeDescription
namestring

the canonical algorithm name

Returns

TypeDescription
Algorithm?

the descriptor, or nil when unavailable

newDigestfunction#

function newDigest(name: string): Digest

Creates fresh incremental digest state.

Arguments

NameTypeDescription
namestring

the canonical algorithm name

Returns

TypeDescription
Digest

an owned unfinished digest

Raises

TypeCondition
string

when the algorithm is unavailable or cannot be initialized