Skip to content
CryoCryo home
StdlibText and data

hash

Message digests, apart from the encodings since a digest cannot be decoded. Not auto-imported.

sha256 is FIPS 180-4 and sha1 is RFC 3174, the latter for protocols that name it, such as the WebSocket handshake, and not as a security primitive. Each has one shape: Sha256 (Sha1) hashes a message that arrives in pieces, Sha256::digest (Sha1::digest) one held whole, and either gives a Sha256Digest (Sha1Digest). hmac keys either one into a message authentication code (RFC 2104): HmacSha256 and HmacSha1, with a verify that compares a received code in constant time.

The Hash trait and the hashers a hash table keys with are core::hash: they allocate nothing and are the language's, where these are digests of a whole message.

import std::hash::{ Sha256, Sha256Digest };

function main() -> int {
    const digest: Sha256Digest = Sha256::digest(Str::new("abc").as_bytes());
    // ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
    return if (digest.to_hex().length() == 64) { 0 } else { 1 };
}
module hash::hmac;
module hash::sha1;
module hash::sha256;

export std::hash::hmac::{ HmacSha1, HmacSha256 };
export std::hash::sha1::{ Sha1, Sha1Digest };
export std::hash::sha256::{ Sha256, Sha256Digest };
FileWhat it holdsImport
sha256Sha256, Sha256Digest — FIPS 180-4import std::hash::sha256;
sha1Sha1, Sha1Digest — RFC 3174, for the WebSocket handshakeimport std::hash::sha1;
hmacHmacSha256, HmacSha1 — RFC 2104import std::hash::hmac;

std::hash re-exports every type in the table, so import std::hash::{ Sha256 }; is the usual spelling.