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
| Module | Description |
|---|---|
nupp.digest.spi | Implementation interfaces for nupp.digest algorithm catalogs. |
Module contents
Types
| Type | Kind | Description |
|---|---|---|
Algorithm | record | Immutable algorithm metadata and a factory for independent digest contexts. |
Digest | interface | An owned incremental digest, consumed by either finalization overload. |
Functions
| Function | Kind | Description |
|---|---|---|
algorithm | function | Requires an available algorithm descriptor. |
algorithms | function | Lists canonical available names in ascending order. |
digest | function | Computes a raw digest. |
hexDigest | function | Computes a lowercase hexadecimal digest. |
lookup | function | Looks up an algorithm without constructing mutable provider state. |
newDigest | function | Creates fresh incremental digest state. |
Types#
Algorithmrecord#
record Algorithm ...Immutable algorithm metadata and a factory for independent digest contexts.
Members
| Name | Kind | Description |
|---|---|---|
name | field | Canonical algorithm name. |
digestSize | field | Number of output bytes, available before creating mutable state. |
implementation | field | |
newDigest | method | Creates independent owned state for this algorithm. |
digestSizefield#
digestSize: integerNumber of output bytes, available before creating mutable state.
Digestinterface#
interface Digest is Writer ...An owned incremental digest, consumed by either finalization overload.
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 | Digest |
Returns
| Type | Description |
|---|---|
nil |
algorithmmethod#
Returns the canonical algorithm name retained at creation.
Arguments
| Name | Type | Description |
|---|---|---|
self | Digest |
Returns
| Type | Description |
|---|---|
string |
digestSizemethod#
digestSize: function(self: Digest): integerReturns the fixed output size in bytes.
Arguments
| Name | Type | Description |
|---|---|---|
self | Digest |
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 | Digest | |
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 | Digest | |
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 | Digest |
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 | Digest | |
exclusive destination | span.ByteWriteSpan |
Returns
| Type | Description |
|---|---|
integer |
Functions#
algorithmfunction#
Requires an available algorithm descriptor.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical algorithm name |
Returns
| Type | Description |
|---|---|
Algorithm | the selected descriptor |
Raises
| Type | Condition |
|---|---|
string | when the algorithm is unavailable |
algorithmsfunction#
function algorithms(): {string}Lists canonical available names in ascending order.
Returns
| Type | Description |
|---|---|
{string} | the available canonical names |
digestfunction#
function digest(name: string, bytes: string): stringComputes a raw digest.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical algorithm name |
bytes | string | the message bytes |
Returns
| Type | Description |
|---|---|
string | the raw fixed-size digest bytes |
Raises
| Type | Condition |
|---|---|
string | when lookup or a provider operation fails |
hexDigestfunction#
function hexDigest(name: string, bytes: string): stringComputes a lowercase hexadecimal digest.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical algorithm name |
bytes | string | the message bytes |
Returns
| Type | Description |
|---|---|
string | the lowercase hexadecimal digest |
Raises
| Type | Condition |
|---|---|
string | when lookup or a provider operation fails |
lookupfunction#
Looks up an algorithm without constructing mutable provider state.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical algorithm name |
Returns
| Type | Description |
|---|---|
Algorithm? | the descriptor, or nil when unavailable |
newDigestfunction#
Creates fresh incremental digest state.
Arguments
| Name | Type | Description |
|---|---|---|
name | string | the canonical algorithm name |
Returns
| Type | Description |
|---|---|
Digest | an owned unfinished digest |
Raises
| Type | Condition |
|---|---|
string | when the algorithm is unavailable or cannot be initialized |