Module crypto/sha256
SHA-256 hash function (FIPS 180-4), pure Yo implementation.
Example
{ sha256_hex } :: import("std/crypto/sha256");
digest := sha256_hex(data); // "2cf24dba..."
Stability
unstable — the algorithm is fixed by FIPS 180-4 and the vectors are
tested (tests/crypto/sha256.test.yo, plus the streaming-equals-
one-shot check in tests/crypto/digest.test.yo), so what is unstable is
the SHAPE, not the answer. Every digest is reachable two ways — the
inherent finish() -> Array(u8, 32), the canonical one, and the
Digest trait's allocating finish_bytes() -> ArrayList(u8). Both
consume the hasher, so a second finish is a use after move (E0901).
There is no reset. A
Sha224 sibling (the same core with a different IV and a truncated
output) would be additive.
Types
A streaming SHA-256 hasher: 32-byte digest, 64-byte block, big-endian
throughout — RustCrypto's Sha256.
Feed it with update (any number of times, any chunk sizes) and finish
with finish. Splitting the input differently cannot change the
digest; the hasher buffers a partial block internally.
A move-only value: update lends it (&mut), finish consumes it,
so one hasher hashes one message. Use sha256 / sha256_hex when the
whole message is already in hand.
Fields
| Name | Type | Description |
|---|---|---|
_h | Array(u32, 8) | |
_buf | Array(u8, 64) | |
_buflen | usize | |
_total | u64 |
Trait Implementations
impl(Sha256, ...)
new : (Sha256) fn() -> Sha256A hasher seeded with the FIPS 180-4 initial state, ready for update.
Returns: Sha256
update : (Sha256) fn(self : &mut Sha256, data : &ArrayList(u8)) -> unitfinish : (Sha256) fn(self : Sha256) -> Array(u8, 32)Append the FIPS padding and the message-length field, fold the last block in, and return the 32-byte digest.
Consumes the hasher, so one hasher hashes one message: a second
finish is a use after move (E0901), as with RustCrypto's
finalize(self).
Parameters
| Name | Type | Notes |
|---|---|---|
self | Sha256 |
Returns: Array(u8, 32)
impl(Sha256, Digest(...))
new : (Sha256) fn() -> Sha256A hasher seeded with the FIPS 180-4 initial state, ready for update.
Returns: Sha256
update : (Sha256) fn(self : &mut Sha256, data : &ArrayList(u8)) -> unitdigest_size : (Sha256) fn() -> usizeThe digest length in bytes (32/64/20/16).
Returns: usize
block_size : (Sha256) fn() -> usizeThe compression block size in bytes (64, or 128 for SHA-512) — what HMAC pads keys to.
Returns: usize
Functions
SHA-256 of data in one call — the 32-byte digest.
Exactly a fresh Sha256, one update(data), then finish(), so use it
whenever the whole message is in memory; reach for the Sha256 type only to hash in
pieces.
Parameters
| Name | Type | Notes |
|---|---|---|
data | &ArrayList(u8) |
Returns: Array(u8, usize(32))
SHA-256 of data as 64 lowercase hex characters — the spelling
checksums are usually published in.
Convenience over sha256 + hex_encode; it allocates the String.
Compare hex digests with care: use constant_time_eq
(std/crypto/hmac) when the comparison is a security decision.
Parameters
| Name | Type | Notes |
|---|---|---|
data | &ArrayList(u8) |
Returns: String