sha1
import std::hash::sha1; · source
SHA-1, as RFC 3174 defines it, in pure Cryo, with the same shape as sha256.
SHA-1 is cryptographically broken for collision resistance. It ships here only because protocols name it — the WebSocket handshake (RFC 6455) hard-codes it for the
Sec-WebSocket-Acceptvalue. Do not use it as a security primitive. Having a pure Cryo implementation is what keeps plainws://free of any external crypto dependency — no OpenSSL link required.
For a cryptographic hash, use sha256. For hashing in the non-cryptographic sense — map keys, fingerprints — use core::hash. For key material, tokens, and nonces, use random::SecureRng.
Types
Sha1
type struct Sha1 {
private state: [u32; 5];
private block: [u8; 64];
private buffered: i64;
private message_length: u64;
}
A computation in progress: update feeds the message in pieces and finish gives the digest; Sha1::digest hashes one message held whole.
Constructors
static new() -> Sha1;
static new() -> Sha1;A computation over the empty message.
Static functions
static digest(data: &Slice<u8>) -> Sha1Digest;
static digest(data: &Slice<u8>) -> Sha1Digest;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) -> Sha1Digest;
finish(mut this) -> Sha1Digest;Close the message and give its digest.
Sha1Digest
type struct Sha1Digest with [Eq] {
private bytes: [u8; 20];
}
The hash's 20 bytes. It displays, and to_hex writes it, as 40 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 40 lowercase hexadecimal digits.
Trait implementations
implement trait Display for struct Sha1Digest
Constants
const DIGEST_LENGTH: i64 = 20;
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 Sha1 {}A SHA-1 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.
Sha1.state: [u32; 5]Sha1.block: [u8; 64]Sha1.buffered: i64Sha1.message_length: u64type struct Sha1Digest with [Eq] {}A SHA-1 digest: the hash's 20 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.
Sha1Digest.bytes: [u8; 20]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 = 20;const BLOCK_LENGTH: i64 = 64;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 WebSocket<S> {}A WebSocket connection over the buffered async transport S - TcpStream for ws://, TlsStream for wss://. Owns the connection and records which side it is, which decides the frame masking: a client masks what it sends and refuses masked frames, a server the reverse.
type struct Sha1 {}A SHA-1 computation in progress: the message so far, fed in order by update, and closed by finish.
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.
type struct Sha1Digest with [Eq] {}A SHA-1 digest: the hash's 20 bytes, most significant byte of the first word first.
std::hash::sha1mut &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 referencetype 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