silk.crypto.blake2b.
Portable BLAKE2b states for the unkeyed 64-byte and 32-byte cryptographic digests of RFC 7693.
When to use#
Use Blake2b512 where a format names BLAKE2b with a 64-byte digest, such as prehashed
minisign signatures, and Blake2b256 where it names a 32-byte digest, such as libsodium's
crypto_generichash. Use silk.crypto.sha2 where a protocol names SHA-2. Use
silk.collections.hash only for collection keys. It is not a cryptographic hash.
Details#
The state supports incremental input through update, consuming finalization through finish,
and one-shot input through hash. It uses only inline storage and allocates no memory. The
digest is the little-endian encoding of the final state words, as RFC 7693 specifies.
Gotchas#
Keyed hashing, salts, personalization and digest lengths other than 64 and 32 bytes are not
supported.
If an update makes the message longer than 2^128 - 1 bytes, the program traps. finish
consumes its state, so the program cannot update or finish that state again.
Examples#
Hash a short message#
import silk.crypto.blake2b {Blake2b512}
pub fn main() -> i32 {
let message: [u8; 3] = [97, 98, 99]
let digest = Blake2b512.hash(&message)
if digest[0] != 186 || digest[63] != 35 {
return 1
}
return 0
}Import as Blake2b512 with import silk.crypto.blake2b { Blake2b512 }.
Public declarations: 2.
Blake2b512#
pub struct Blake2b512An incremental unkeyed BLAKE2b state with a 64-byte digest (RFC 7693).
When to use#
Use it where a format names BLAKE2b with a 64-byte digest, such as prehashed minisign
signatures. Use silk.crypto.sha2 where a protocol names SHA-2. Use silk.collections.hash
only for collection keys. It is not a cryptographic hash.
Details#
Give input in parts with Blake2b512.update and get the digest with Blake2b512.finish,
or hash one slice with Blake2b512.hash. The state uses only inline storage and allocates no
memory. The digest is the little-endian encoding of the final state words.
Gotchas#
Keyed hashing, salts, personalization and digest lengths other than 64 and 32 bytes are not supported. If an update makes the message longer than 2^128 - 1 bytes, the program traps.
Implementation Blake2b512#
impl Blake2b512Associated function make#
pub fn make() -> Blake2b512Creates an empty BLAKE2b-512 state.
Method update#
pub fn update(self: &mut Self, bytes: &[u8]) -> ()Adds bytes to this state in source order. An empty slice does not change the state.
Details#
A full block is compressed only when more input follows it, because the final block is compressed with the finalization flag. It allocates no memory.
Gotchas#
If an update makes the message longer than 2^128 - 1 bytes, the program traps.
Method finish#
pub fn finish(self: Self) -> [u8; 64]Consumes this state and returns its 64-byte digest.
Gotchas#
The state is consumed, so the program cannot update or finish it again. Use
Blake2b512.hash for one slice.
Associated function hash#
pub fn hash(bytes: &[u8]) -> [u8; 64]Returns the 64-byte BLAKE2b digest of bytes.
Blake2b256#
pub struct Blake2b256An incremental unkeyed BLAKE2b state with a 32-byte digest (RFC 7693), the default of
libsodium's crypto_generichash.
When to use#
Use it where a format names BLAKE2b with a 32-byte digest, such as the checksum of a minisign secret key. It is not BLAKE2b-512 cut short: the digest length is part of the parameter block, so the two digests differ in every byte.
Implementation Blake2b256#
impl Blake2b256Associated function make#
pub fn make() -> Blake2b256Creates an empty BLAKE2b-256 state.
Method update#
pub fn update(self: &mut Self, bytes: &[u8]) -> ()Adds bytes to this state in source order, as Blake2b512.update does.
Method finish#
pub fn finish(self: Self) -> [u8; 32]Consumes this state and returns its 32-byte digest.
Associated function hash#
pub fn hash(bytes: &[u8]) -> [u8; 32]Returns the 32-byte BLAKE2b digest of bytes.
Implementation Digest for Blake2b512#
impl Digest for Blake2b512Operation make#
make: Blake2b512.makeReturns a BLAKE2b-512 state for an empty message.
Operation update#
update: Blake2b512.updateAdds bytes to the end of the message.
Operation blockLength#
fn blockLength(self: &Self) -> usizeReturns 128, the BLAKE2b-512 block length in bytes.
Operation digestLength#
fn digestLength(self: &Self) -> usizeReturns 64, the BLAKE2b-512 digest length in bytes.
Operation objectIdentifier#
fn objectIdentifier(self: &Self) -> &[u8]Returns the content bytes of the BLAKE2b-512 object identifier, 1.3.6.1.4.1.1722.12.2.1.16.
Operation copy#
fn copy(self: &Self) -> SelfReturns an independent copy of the state.
Operation finishInto#
fn finishInto(self: Self, output: &mut [u8]) -> ()Writes the 64-byte BLAKE2b-512 digest to the start of output and consumes the state.
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, x86_64-pc-windows-msvc, wasm64-unknown-unknown.