silk.crypto.sha1.
Compute SHA-1 digests (20 bytes) for old protocols and file formats.
When to use#
Use Sha1 only when an existing format needs SHA-1, such as a Git object ID or the WebSocket
handshake. For new designs, use SHA-256 from silk.crypto.sha2 or SHA-3 from silk.crypto.sha3. For the keys
of a hash map, use silk.collections.hash.
Details#
Sha1.hash returns the digest of one slice. For a message that arrives in parts, call
Sha1.make, then Sha1.update for each part, then Sha1.finish. Nothing allocates.
Gotchas#
SHA-1 is broken: an attacker can make two different messages with the same digest. Do not use it for signatures, certificates, or checks against tampering.
Examples#
Hash a message in one call and in parts#
import silk.crypto.sha1 { Sha1 }
pub fn main() -> i32 {
let oneShot = Sha1.hash(b"abc")
let mut state = Sha1.make()
state.update(b"a")
state.update(b"bc")
let streamed = state.finish()
// SHA-1("abc") is a9993e36...d89d.
if oneShot[0] != 0xa9 || oneShot[19] != 0x9d || streamed[0] != oneShot[0] {
return 1
}
return 0
}Import as Sha1 with import silk.crypto.sha1 { Sha1 }.
Public declarations: 1.
Sha1#
pub struct Sha1A streaming SHA-1 state that returns a 20-byte digest.
Call Sha1.update for each part of the message, then Sha1.finish. The state is a
fixed-size value and does not allocate.
Gotchas#
SHA-1 is broken. Use it only where an existing format needs it.
Implementation Sha1#
impl Sha1Associated function make#
pub fn make() -> SelfReturns a SHA-1 state for an empty message.
Method update#
pub fn update(self: &mut Self, bytes: &[u8]) -> ()Adds bytes to the end of the message.
Any split of the message into parts gives the same digest. An empty slice changes nothing.
Gotchas#
The program traps if the full message is longer than 2^64 - 1 bits.
Method finish#
pub fn finish(self: Self) -> [u8; 20]Returns the 20-byte SHA-1 digest of the message and consumes the state.
Associated function hash#
pub fn hash(bytes: &[u8]) -> [u8; 20]Returns the 20-byte SHA-1 digest of bytes.
Gotchas#
The program traps if bytes is longer than 2^64 - 1 bits.
Category: Cryptography.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.