silk.crypto.blake2s.

Portable BLAKE2s-256 state for the unkeyed 32-byte cryptographic digest of RFC 7693.

When to use#

Use Blake2s256 where a format names BLAKE2s with a 32-byte digest, such as Node's blake2s256 hash. Use silk.crypto.blake2b for BLAKE2b and silk.crypto.sha2 where a protocol names SHA-2.

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 32 bytes are not supported. If an update makes the message longer than 2^64 - 1 bytes, the program traps.

Examples#

Hash a short message#

silk
import silk.crypto.blake2s {Blake2s256}

pub fn main() -> i32 {
  let digest = Blake2s256.hash(b"abc")
  // BLAKE2s-256("abc") is 508c5e8c...5982.
  if digest[0] != 0x50 || digest[31] != 0x82 {
    return 1
  }
  return 0
}

Import as Blake2s256 with import silk.crypto.blake2s { Blake2s256 }.

Public declarations: 1.

Blake2s256#

silk
pub struct Blake2s256

An incremental unkeyed BLAKE2s state with a 32-byte digest (RFC 7693).

Details#

Give input in parts with Blake2s256.update and get the digest with Blake2s256.finish, or hash one slice with Blake2s256.hash. The state uses only inline storage and allocates no memory.

Gotchas#

Keyed hashing, salts, personalization and digest lengths other than 32 bytes are not supported. If an update makes the message longer than 2^64 - 1 bytes, the program traps.

Implementation Blake2s256#

silk
impl Blake2s256

Associated function make#

silk
pub fn make() -> Blake2s256

Creates an empty BLAKE2s-256 state.

Method update#

silk
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.

Method finish#

silk
pub fn finish(self: Self) -> [u8; 32]

Consumes this state and returns its 32-byte digest.

Associated function hash#

silk
pub fn hash(bytes: &[u8]) -> [u8; 32]

Returns the 32-byte BLAKE2s digest of bytes.

Implementation Digest for Blake2s256#

silk
impl Digest for Blake2s256

Operation make#

silk
make: Blake2s256.make

Returns a BLAKE2s-256 state for an empty message.

Operation update#

silk
update: Blake2s256.update

Adds bytes to the end of the message.

Operation blockLength#

silk
fn blockLength(self: &Self) -> usize

Returns 64, the BLAKE2s-256 block length in bytes.

Operation digestLength#

silk
fn digestLength(self: &Self) -> usize

Returns 32, the BLAKE2s-256 digest length in bytes.

Operation objectIdentifier#

silk
fn objectIdentifier(self: &Self) -> &[u8]

Returns the content bytes of the BLAKE2s-256 object identifier, 1.3.6.1.4.1.1722.12.2.2.8.

Operation copy#

silk
fn copy(self: &Self) -> Self

Returns an independent copy of the state.

Operation finishInto#

silk
fn finishInto(self: Self, output: &mut [u8]) -> ()

Writes the 32-byte BLAKE2s-256 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.

View the source of this page