Wado

core:digest

Cryptographic hash functions, and [hmac] to key one.

The [Digest] trait is the common interface for incremental hashing, and [Sha256] (FIPS 180-4 / RFC 6234) is so far its one implementation. The trait keeps the module name stable as more algorithms are added.

Message bytes are accepted from any AsByteSlice source: a ByteSlice, ByteList, ByteArray, or a String (its UTF-8 bytes).

Synopsis

// One shot, when the whole message is in hand.
let digest = sha256(&"wado");
assert digest.to_hex() == "fe3d12dc082eed509c3869ed16f0c209f2fbaeee3a41aa09f0033798efd67052";

// Incremental, through the trait, when it arrives in pieces.
let mut h = Sha256::new();
h.update(&"wa");
h.update(&"do");
assert h.finalize() == digest;

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().

pub fn hmac<H: Digest, K: AsByteSlice, M: AsByteSlice>(hasher: H, key: &K, message: &M) -> ByteList

Keyed message authentication over any [Digest] (HMAC, RFC 2104): H((K ^ opad) || H((K ^ ipad) || message)).

Pass a fresh hasher of the algorithm to key. A key of any length works: the RFC's own reduction to one block applies.

Check the result with eq_constant_time, never ==.

pub fn hmac_sha256<K: AsByteSlice, M: AsByteSlice>(key: &K, message: &M) -> ByteList

Key a complete message with HMAC-SHA256, returning the 32-byte MAC. The one-shot counterpart to [hmac], as [sha256] is to [Sha256].

Traits

pub trait Digest with ()

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 block_len(&self) -> i32

The algorithm's compression block width in bytes, 64 for SHA-256. [hmac] pads its key to it.

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.

impl Digest for Sha256

fn block_len(&self) -> i32
fn update<S: AsByteSlice>(&mut self, data: &S)
fn finalize(&mut self) -> ByteList