sha256
import std::hash::sha256; · source
SHA-256, as FIPS 180-4 defines it, in pure Cryo.
Types
Sha256
type struct Sha256 {
private state: [u32; 8];
private block: [u8; 64];
private buffered: i64;
private message_length: u64;
}
A computation in progress. update feeds the message in pieces, in order, and finish consumes the computation and gives the digest. Sha256::digest hashes one message held whole.
import std::hash::sha256::{ Sha256, Sha256Digest };
import std::fmt::{ println };
function main() -> int {
const digest: Sha256Digest = Sha256::digest(Str::new("abc").as_bytes());
println(f"{digest}"); // ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
mut hasher: Sha256 = Sha256::new();
hasher.update(Str::new("a").as_bytes());
hasher.update(Str::new("bc").as_bytes());
const same: Sha256Digest = hasher.finish(); // equal to `digest`
return if (same == digest) { 0 } else { 1 };
}
Constructors
static new() -> Sha256;
static new() -> Sha256;A computation over the empty message.
Static functions
static digest(data: &Slice<u8>) -> Sha256Digest;
static digest(data: &Slice<u8>) -> Sha256Digest;The digest of data, held whole.
Methods
update(mut &this, data: &Slice<u8>) -> void;
update(mut &this, data: &Slice<u8>) -> void;Append data to the message.
finish(mut this) -> Sha256Digest;
finish(mut this) -> Sha256Digest;Close the message and give its digest.
Sha256Digest
type struct Sha256Digest with [Eq] {
private bytes: [u8; 32];
}
The hash's 32 bytes in the order the standard writes them, most significant byte of the first word first. It displays, and to_hex writes it, as 64 lowercase hexadecimal digits.
Methods
as_bytes(&this) -> &Slice<u8>;
as_bytes(&this) -> &Slice<u8>;The digest's bytes.
to_hex(&this) -> String;
to_hex(&this) -> String;The digest as 64 lowercase hexadecimal digits.
Trait implementations
implement trait Display for struct Sha256Digest
Constants
const DIGEST_LENGTH: i64 = 32;
const BLOCK_LENGTH: i64 = 64;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Sha256 {}A SHA-256 computation in progress: the message so far, fed in order by update, and closed by finish.
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
Sha256.state: [u32; 8]Sha256.block: [u8; 64]Sha256.buffered: i64Sha256.message_length: u64import 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::strconst digest: Sha256Digestmut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut hasher: Sha256mut hasher: Sha256const same: Sha256Digestreturn 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 same: Sha256Digestif (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.
type struct Sha256Digest with [Eq] {}A SHA-256 digest: the hash's 32 bytes, most significant byte of the first word first.
type struct Point with [Eq, Clone, Debug] { .. }Asks the compiler to generate implementations of the listed traits for the type, each working through its fields or variants in declaration order. The derivable traits are Eq, Ord, Hash, Clone, Debug, Default, ToValue and FromValue, and the text formats' FromJson, ToJson, FromYaml, ToYaml, FromToml and ToToml. The clause comes after the name and its parameters, before where.
type trait Eq<Rhs = This> {
equals(&this, other: &Rhs) -> boolean;
}Types that support value equality with a value of Rhs, which is the type itself unless an implementation names another: implement trait Eq for Str compares two Strs, and implement trait Eq<string> for Str a Str with a string.
Sha256Digest.bytes: [u8; 32]const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const DIGEST_LENGTH: i64 = 32;const BLOCK_LENGTH: i64 = 64;static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
(parameter) data: &Slice<u8>type struct Slice<T> {}The elements are reached through ptr, declared read-only: through a mut &Slice<T> it reads as a writable pointer, through a &Slice<T> as the read-only one it is.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
mut thisA by-value receiver the body may also reassign. The receiver is moved into the method and consumed; the caller's value may not be used afterwards.
language referenceSha256Digest::to_hex(&this) -> StringThe digest as 64 lowercase hexadecimal digits.
std::hash::sha256type struct String<A = GlobalAllocator> {}Owned, growable UTF-8 text. It dereferences to Str, the read-only view most text functions take, so every Str method reads a String too (s.contains(...), s.split(...)); as_str hands the view out.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Display {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
to_string(&this) -> String;
}How a value reads to a person: what {x} in an f-string and println write. Implement it for a type a user sees.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference