Silk

silk/sha1

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 streaming SHA-1 with a fixed 20-byte digest.

When to use

Use Sha1 only for legacy interoperability that requires SHA-1. Use SHA-2 or SHA-3 for new collision-resistant uses. Use silk/hash only for collection keys, not cryptographic digests.

Details

make starts a streaming state. update absorbs each byte in call order. finish consumes the state and returns the standard digest byte order. hash performs the same lifecycle in one call. These operations do not allocate.

Gotchas

SHA-1 is cryptographically broken because practical collision attacks exist. Do not use SHA-1 for signatures, certificates, content integrity, or other new collision-resistant designs.

Examples

Hash one message with both lifecycles

import silk.sha1 {Sha1}

pub fn main() -> i32 {
  let message: [u8; 3] = [97, 98, 99]
  let oneShot = Sha1.hash(&message)

  let mut state = Sha1.make()
  state.update(&message)
  let streamed = state.finish()

  if oneShot[0] != 169 {
    return 1
  }
  if streamed[19] != 157 {
    return 1
  }
  return 0
}

Import as Sha1 with import silk.sha1 { Sha1 }.

Public declarations: 1.

Sha1

pub struct Sha1

A streaming SHA-1 state that returns a 20-byte digest for legacy interoperability.

When to use

Use this type only when an existing protocol or data format requires SHA-1. Use SHA-2 or SHA-3 for new collision-resistant uses.

Details

The state is stack-storable and does not allocate. It accepts any message division across update calls. finish consumes the state.

Gotchas

SHA-1 is cryptographically broken because practical collision attacks exist. Do not use it for new signatures, certificates, or content-integrity designs.

Associated function Sha1.make

pub fn make() -> Sha1

Creates an empty SHA-1 streaming state without allocation.

Method Sha1.update

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

Absorbs bytes after all bytes supplied by earlier calls.

Details

An empty slice does not change the state. This operation does not allocate. Any division of one message across calls produces the same digest.

Gotchas

If the cumulative message bit length does not fit in u64, this operation traps before it absorbs bytes from this call.

Method Sha1.finish

pub fn finish(self: Sha1) -> Array<u8, 20>

Consumes the streaming state and returns its 20-byte SHA-1 digest.

Details

This operation applies standard SHA-1 padding and returns the digest in standard byte order. It does not allocate. The consumed state cannot accept more bytes or produce another digest.

Associated function Sha1.hash

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

Returns the 20-byte SHA-1 digest of bytes without allocation.

When to use

Use this function when the complete message is available in one slice. Use make and update when the message arrives in parts.

Details

This function uses the same streaming and consuming finalization lifecycle as direct state operations. It returns the digest in standard byte order.

Gotchas

If the message bit length does not fit in u64, this operation traps.

On this page