Silk

silk/sha2

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.

Portable SHA-2 states for six fixed-output cryptographic digest algorithms.

When to use

Use Sha224 or Sha256 for a 28-byte or 32-byte digest with 32-bit processing. Use Sha384, Sha512, Sha512_224, or Sha512_256 for SHA-512-family processing. Use silk.hash only for collection keys. It is not a cryptographic hash.

Details

Each actor supports incremental input through update, consuming finalization through finish, and one-shot input through hash. The states use only inline storage and allocate no memory. Each digest uses the standardized big-endian byte order. Sha256.checkpoint and Sha384.checkpoint also return a digest without consuming the streaming state. Use these operations for exact protocol transcript boundaries.

Gotchas

If an update exceeds the algorithm's standard bit-length domain, the program traps. finish consumes its state, so the program cannot update or finish that state again.

Examples

Compare incremental and one-shot SHA-256

import silk.sha2 {Sha256}

pub fn main() -> i32 {
  let message: [u8; 3] = [97, 98, 99]
  let mut state = Sha256.make()
  state.update(&message)
  let incremental = state.finish()
  let oneShot = Sha256.hash(&message)
  if incremental[0] != 186 || oneShot[0] != 186 {
    return 1
  }
  return 0
}

Import as Sha256 with import silk.sha2 { Sha256 }.

Public declarations: 6.

Sha224

pub struct Sha224

An allocation-free incremental SHA-224 state that returns a 28-byte digest.

Associated function Sha224.make

pub fn make() -> Sha224

Creates an empty SHA-224 state.

Method Sha224.update

pub fn update<'life0, 'life1>(self: &'life0 mut Sha224, bytes: &'life1 [u8]) -> ()

Adds bytes to this SHA-224 state in source order. An empty slice does not change the state. If the complete message exceeds the 64-bit bit-length domain, the program traps.

Method Sha224.finish

pub fn finish(self: Sha224) -> Array<u8, 28>

Consumes this SHA-224 state and returns its 28-byte digest in big-endian byte order.

Associated function Sha224.hash

pub fn hash<'life0>(bytes: &'life0 [u8]) -> Array<u8, 28>

Returns the 28-byte SHA-224 digest of bytes. If the message is too long, the program traps.

Sha256

pub struct Sha256

An allocation-free incremental SHA-256 state that returns a 32-byte digest.

Associated function Sha256.make

pub fn make() -> Sha256

Creates an empty SHA-256 state.

Method Sha256.update

pub fn update<'life0, 'life1>(self: &'life0 mut Sha256, bytes: &'life1 [u8]) -> ()

Adds bytes to this SHA-256 state in source order. An empty slice does not change the state. If the complete message exceeds the 64-bit bit-length domain, the program traps.

Method Sha256.checkpoint

pub fn checkpoint<'life0>(self: &'life0 Sha256) -> Array<u8, 32>

Returns the SHA-256 digest of all bytes supplied so far without consuming this state.

Details

The operation copies the fixed inline state. A later update or checkpoint continues from the same original byte sequence.

Method Sha256.finish

pub fn finish(self: Sha256) -> Array<u8, 32>

Consumes this SHA-256 state and returns its 32-byte digest in big-endian byte order.

Associated function Sha256.hash

pub fn hash<'life0>(bytes: &'life0 [u8]) -> Array<u8, 32>

Returns the 32-byte SHA-256 digest of bytes. If the message is too long, the program traps.

Sha384

pub struct Sha384

An allocation-free incremental SHA-384 state that returns a 48-byte digest.

Associated function Sha384.make

pub fn make() -> Sha384

Creates an empty SHA-384 state.

Method Sha384.update

pub fn update<'life0, 'life1>(self: &'life0 mut Sha384, bytes: &'life1 [u8]) -> ()

Adds bytes to this SHA-384 state in source order. An empty slice does not change the state. If the complete message exceeds the 128-bit bit-length domain, the program traps.

Method Sha384.checkpoint

pub fn checkpoint<'life0>(self: &'life0 Sha384) -> Array<u8, 48>

Returns the SHA-384 digest of all bytes supplied so far without consuming this state.

Details

The operation copies the fixed inline state. A later update or checkpoint continues from the same original byte sequence.

Method Sha384.finish

pub fn finish(self: Sha384) -> Array<u8, 48>

Consumes this SHA-384 state and returns its 48-byte digest in big-endian byte order.

Associated function Sha384.hash

pub fn hash<'life0>(bytes: &'life0 [u8]) -> Array<u8, 48>

Returns the 48-byte SHA-384 digest of bytes. If the message is too long, the program traps.

Sha512

pub struct Sha512

An allocation-free incremental SHA-512 state that returns a 64-byte digest.

Associated function Sha512.make

pub fn make() -> Sha512

Creates an empty SHA-512 state.

Method Sha512.update

pub fn update<'life0, 'life1>(self: &'life0 mut Sha512, bytes: &'life1 [u8]) -> ()

Adds bytes to this SHA-512 state in source order. An empty slice does not change the state. If the complete message exceeds the 128-bit bit-length domain, the program traps.

Method Sha512.finish

pub fn finish(self: Sha512) -> Array<u8, 64>

Consumes this SHA-512 state and returns its 64-byte digest in big-endian byte order.

Associated function Sha512.hash

pub fn hash<'life0>(bytes: &'life0 [u8]) -> Array<u8, 64>

Returns the 64-byte SHA-512 digest of bytes. If the message is too long, the program traps.

Sha512_224

pub struct Sha512_224

An allocation-free incremental SHA-512/224 state that returns a 28-byte digest.

Associated function Sha512_224.make

pub fn make() -> Sha512_224

Creates an empty SHA-512/224 state.

Method Sha512_224.update

pub fn update<'life0, 'life1>(self: &'life0 mut Sha512_224, bytes: &'life1 [u8]) -> ()

Adds bytes to this SHA-512/224 state in source order. An empty slice does not change the state. If the complete message exceeds the 128-bit bit-length domain, the program traps.

Method Sha512_224.finish

pub fn finish(self: Sha512_224) -> Array<u8, 28>

Consumes this SHA-512/224 state and returns its 28-byte digest in big-endian byte order.

Associated function Sha512_224.hash

pub fn hash<'life0>(bytes: &'life0 [u8]) -> Array<u8, 28>

Returns the 28-byte SHA-512/224 digest of bytes. If the message is too long, the program traps.

Sha512_256

pub struct Sha512_256

An allocation-free incremental SHA-512/256 state that returns a 32-byte digest.

Associated function Sha512_256.make

pub fn make() -> Sha512_256

Creates an empty SHA-512/256 state.

Method Sha512_256.update

pub fn update<'life0, 'life1>(self: &'life0 mut Sha512_256, bytes: &'life1 [u8]) -> ()

Adds bytes to this SHA-512/256 state in source order. An empty slice does not change the state. If the complete message exceeds the 128-bit bit-length domain, the program traps.

Method Sha512_256.finish

pub fn finish(self: Sha512_256) -> Array<u8, 32>

Consumes this SHA-512/256 state and returns its 32-byte digest in big-endian byte order.

Associated function Sha512_256.hash

pub fn hash<'life0>(bytes: &'life0 [u8]) -> Array<u8, 32>

Returns the 32-byte SHA-512/256 digest of bytes. If the message is too long, the program traps.

On this page