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 };
| File | What it holds | Import |
|---|---|---|
sha256 | Sha256, Sha256Digest — FIPS 180-4 | import std::hash::sha256; |
sha1 | Sha1, Sha1Digest — RFC 3174, for the WebSocket handshake | import std::hash::sha1; |
hmac | HmacSha256, HmacSha1 — RFC 2104 | import std::hash::hmac; |
std::hash re-exports every type in the table, so import std::hash::{ Sha256 }; is the usual spelling.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct Sha256 {}A SHA-256 computation in progress: the message so far, fed in order by update, and closed by finish.
type struct Sha256Digest with [Eq] {}A SHA-256 digest: the hash's 32 bytes, most significant byte of the first word first.
std::hash::sha256function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function main() -> intconst name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const digest: Sha256Digeststatic Sha256::digest(data: &Slice<u8>) -> Sha256DigestThe digest of data, held whole.
type struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
Str::as_bytes(&this) -> &Slice<u8>Underlying byte view. Use this for byte-granularity algorithms (hashing, equality, substring search).
std::collections::strreturn value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
if (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
const digest: Sha256DigestSha256Digest::to_hex(&this) -> StringThe digest as 64 lowercase hexadecimal digits.
std::hash::sha256if (condition) { ... } else { ... }The branch taken when every preceding if condition is false; else if chains another condition. In expression form the else arm is mandatory, so the expression always has a value.
module collections::array;
module { a::b; a::c; }
internal module a::detail;In a directory's _module.cryo aggregator, declares a submodule. A module is public; internal module keeps it and everything it declares to its own package. The line is structure only: reaching the child's names still takes an import or an export.
namespace std::hash::hmac;hmac: keyed message authentication codes (RFC 2104) over SHA-256 and SHA-1.
module pageexport a::b::Name;
export a::b::{ X, Y as Z };Passes a name this module did not declare on to its importers, exactly as if they had imported the same path - how an aggregator presents a flat surface over its files. Every hop is explicit, and there is no glob form.
language referencetype struct HmacSha1 {}HMAC-SHA-1 (RFC 2104 over RFC 3174), for protocols that name it. Its shape is HmacSha256's; it gives a Sha1Digest.
type struct HmacSha256 {}HMAC-SHA-256 (RFC 2104 over FIPS 180-4), computed over a message that arrives in pieces. Any key length is accepted; one longer than 64 bytes is hashed first, as the RFC says.
std::hash::hmactype struct Sha1 {}A SHA-1 computation in progress: the message so far, fed in order by update, and closed by finish.
type struct Sha1Digest with [Eq] {}A SHA-1 digest: the hash's 20 bytes, most significant byte of the first word first.
std::hash::sha1type trait Hash {
hash<H>(&this, hasher: mut &H) -> void
where H: Hasher;
}A type whose values can be hashed, so they can key a HashMap or sit in a HashSet. Values that are equal under Eq must feed the same bytes. A type takes it with with [Hash] on its declaration, or by implementing hash by hand.