Module crypto/sha512

crypto/sha512
Stability: unstable — the FIPS vectors pass (`tests/crypto/digest.test.yo`), but this module carries a deviation that becomes a promise the moment it is frozen: the spec's message-length field is 128 bits and this implementation tracks a `u64` byte count, so inputs are capped at 2^61 bytes and the high half of the length field is derived rather than counted. Nothing in reach of a Yo program hits that, yet "SHA-512 as specified" and "SHA-512 up to two exbibytes" are different contracts. SHA-384 — the same compression function with a different IV and a truncated output — is also absent, and adding it may well change how this file is organised. The `finish` / `finish_bytes` double surface and the single-use `finish` are the `std/crypto/digest` questions. — stable modules only change additively; this one may still change.

SHA-512 (FIPS 180-4) — streaming hasher + one-shot helpers (plans/archive/STD_API_AUDIT.md §7 P0 item 7).

64-bit words, 128-byte blocks, 80 rounds. The message-length field is 128 bits in the spec; this implementation tracks the total as a u64 BYTE count (caps input at 2^61 bytes — two exbibytes) and writes the high 64 bits of the bit length as total >> 61.

{ sha512_hex } :: import("std/crypto/sha512");
digest := sha512_hex(data); // 128-char lowercase hex

Stability

unstable — the FIPS vectors pass (tests/crypto/digest.test.yo), but this module carries a deviation that becomes a promise the moment it is frozen: the spec's message-length field is 128 bits and this implementation tracks a u64 byte count, so inputs are capped at 2^61 bytes and the high half of the length field is derived rather than counted. Nothing in reach of a Yo program hits that, yet "SHA-512 as specified" and "SHA-512 up to two exbibytes" are different contracts. SHA-384 — the same compression function with a different IV and a truncated output — is also absent, and adding it may well change how this file is organised. The finish / finish_bytes double surface and the single-use finish are the std/crypto/digest questions.

Types

Sha512 object
Sha512

SHA-512 streaming state (the Sha256 skeleton at 64-bit width).

Fields

NameTypeDescription
_hArray(u64, 8)
_bufArray(u8, 128)
_buflenusize
_totalu64

Trait Implementations

impl(Sha512, ...)
new : (Sha512) fn() -> Sha512

A hasher seeded with the FIPS 180-4 SHA-512 initial state.

Returns: Sha512

update : (Sha512) fn(self : Sha512, data : ArrayList(u8)) -> Sha512

Absorb data and return the same hasher, so calls chain. O(len). Buffers a partial 128-byte block internally, so how the input is split cannot change the digest.

Parameters

NameTypeNotes
selfSha512
dataArrayList(u8)

Returns: Sha512

finish : (Sha512) fn(self : Sha512) -> Array(u8, 64)

Append the padding and the 128-bit length field, fold the last block in, and return the 64-byte digest.

Single use — the padding goes into the hasher's own buffer, so a second finish returns a digest of the padded state rather than of the message. One hasher, one message.

Parameters

NameTypeNotes
selfSha512

Returns: Array(u8, 64)

impl(Sha512, Digest(...))
new : (Sha512) fn() -> Sha512

A hasher seeded with the FIPS 180-4 SHA-512 initial state.

Returns: Sha512

update : (Sha512) fn(self : Sha512, data : ArrayList(u8)) -> Sha512

Absorb data and return the same hasher, so calls chain. O(len). Buffers a partial 128-byte block internally, so how the input is split cannot change the digest.

Parameters

NameTypeNotes
selfSha512
dataArrayList(u8)

Returns: Sha512

digest_size : (Sha512) fn() -> usize

The digest length in bytes (32/64/20/16).

Returns: usize

block_size : (Sha512) fn() -> usize

The compression block size in bytes (64, or 128 for SHA-512) — what HMAC pads keys to.

Returns: usize

finish_bytes : (Sha512) fn(self : Sha512) -> ArrayList(u8)

Finalise and return the digest as bytes.

Parameters

NameTypeNotes
selfSha512

Returns: ArrayList(u8)

Methods
finish_hex : (Sha512) fn(self : Sha512) -> String

Finalise and return the lowercase hex digest.

Parameters

NameTypeNotes
selfSha512

Returns: String

Functions

sha512 function
fn(data : ArrayList(u8)) -> Array(u8, usize(64))

SHA-512 of data in one call — the 64-byte digest. Exactly Sha512.new().update(data).finish().

Parameters

NameTypeNotes
dataArrayList(u8)

Returns: Array(u8, usize(64))

sha512_hex function
fn(data : ArrayList(u8)) -> String

SHA-512 of data as 128 lowercase hex characters. Allocates the String; compare security-relevant digests with constant_time_eq (std/crypto/hmac).

Parameters

NameTypeNotes
dataArrayList(u8)

Returns: String