core:digest
Cryptographic hash functions.
This module provides the [Digest] trait — a common interface for
incremental hashing — and concrete implementations. Currently only
[Sha256] (SHA-256, FIPS 180-4 / RFC 6234) is provided; the trait keeps
the module name stable as more algorithms are added.
The synopsis covers the one-shot sha256. Incremental hashing via the trait:
use { Sha256, Digest } from "core:digest";
let mut h = Sha256::new();
h.update(&first_chunk);
h.update(&second_chunk);
let digest = h.finalize();
Message bytes are accepted from any AsByteSlice source: a ByteSlice,
ByteList, ByteArray, plain List<u8>, or a String (its UTF-8 bytes).
Synopsis
let digest = sha256(&"wado");
assert digest.to_hex() == "fe3d12dc082eed509c3869ed16f0c209f2fbaeee3a41aa09f0033798efd67052";
Functions
pub fn sha256<S: AsByteSlice>(data: &S) -> ByteList
Hash a complete message with SHA-256, returning the 32-byte digest.
Accepts any byte source via AsByteSlice (a String hashes its UTF-8
bytes). Render the result as hex with to_hex, e.g. sha256(&data).to_hex().
Traits
pub trait Digest
A streaming message-digest algorithm.
Feed message bytes with update (any number of times),
then call finalize exactly once to obtain the digest.
A value must not be used after finalize.
fn update<S: AsByteSlice>(&mut self, data: &S)
Absorb data into the running hash state.
fn finalize(&mut self) -> ByteList
Consume the buffered padding, returning the digest as raw bytes.
Structs
pub struct Sha256
Incremental SHA-256 hasher (FIPS 180-4 / RFC 6234).
Produces a 32-byte digest. Use [Sha256::new] then the [Digest] trait
methods, or the free [sha256] helper for one-shot hashing.
Fields are private.
pub fn new() -> Sha256
Create a fresh SHA-256 state initialized with the standard IV.
