Module crypto/sha1

crypto/sha1
Stability: unstable — and the open question here is whether the module should exist in this form at all, not how it computes. The vectors are right and tested, but SHA-1 has no dedicated test file: its cases live in `tests/crypto/digest.test.yo` beside the trait's. More importantly, nothing in the API marks it as the legacy-compatibility algorithm the module header says it is — a new caller reaching for `sha1_hex` gets no signal — and whether std keeps shipping it plainly, gates it, or moves it out has not been decided. The `finish` / `finish_bytes` double surface and the single-use `finish` are the `std/crypto/digest` questions. — stable modules only change additively; this one may still change.

SHA-1 (FIPS 180-4) — streaming hasher + one-shot helpers (plans/archive/STD_API_AUDIT.md §7 P0 item 7).

SHA-1 is cryptographically BROKEN for collision resistance (SHAttered, 2017) — do not use it for signatures or content addressing of untrusted input. It remains required by existing protocols (git object ids, HMAC-SHA1 in OAuth 1 / AWS SigV2-era APIs), which is why std ships it.

{ sha1_hex } :: import("std/crypto/sha1");
digest := sha1_hex(data); // 40-char lowercase hex

Stability

unstable — and the open question here is whether the module should exist in this form at all, not how it computes. The vectors are right and tested, but SHA-1 has no dedicated test file: its cases live in tests/crypto/digest.test.yo beside the trait's. More importantly, nothing in the API marks it as the legacy-compatibility algorithm the module header says it is — a new caller reaching for sha1_hex gets no signal — and whether std keeps shipping it plainly, gates it, or moves it out has not been decided. The finish / finish_bytes double surface and the single-use finish are the std/crypto/digest questions.

Types

Sha1 object
Sha1

SHA-1 streaming state (the Sha256 skeleton).

Fields

NameTypeDescription
_hArray(u32, 5)
_bufArray(u8, 64)
_buflenusize
_totalu64

Trait Implementations

impl(Sha1, ...)
new : (Sha1) fn() -> Sha1

A hasher seeded with the SHA-1 initial state.

Returns: Sha1

update : (Sha1) fn(self : Sha1, data : ArrayList(u8)) -> Sha1

Absorb data and return the same hasher, so calls chain. O(len). Buffers a partial 64-byte block, so how the input is split cannot change the digest.

Parameters

NameTypeNotes
selfSha1
dataArrayList(u8)

Returns: Sha1

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

Append the padding and the big-endian bit length, fold the last block in, and return the 20-byte digest.

Single use — the padding is written into the hasher's own buffer, so a second finish digests the padded state rather than the message.

Parameters

NameTypeNotes
selfSha1

Returns: Array(u8, 20)

impl(Sha1, Digest(...))
new : (Sha1) fn() -> Sha1

A hasher seeded with the SHA-1 initial state.

Returns: Sha1

update : (Sha1) fn(self : Sha1, data : ArrayList(u8)) -> Sha1

Absorb data and return the same hasher, so calls chain. O(len). Buffers a partial 64-byte block, so how the input is split cannot change the digest.

Parameters

NameTypeNotes
selfSha1
dataArrayList(u8)

Returns: Sha1

digest_size : (Sha1) fn() -> usize

The digest length in bytes (32/64/20/16).

Returns: usize

block_size : (Sha1) fn() -> usize

The compression block size in bytes (64, or 128 for SHA-512) — what HMAC pads keys to.

Returns: usize

finish_bytes : (Sha1) fn(self : Sha1) -> ArrayList(u8)

Finalise and return the digest as bytes.

Parameters

NameTypeNotes
selfSha1

Returns: ArrayList(u8)

Methods
finish_hex : (Sha1) fn(self : Sha1) -> String

Finalise and return the lowercase hex digest.

Parameters

NameTypeNotes
selfSha1

Returns: String

Functions

sha1 function
fn(data : ArrayList(u8)) -> Array(u8, usize(20))

SHA-1 of data in one call — the 20-byte digest. Exactly Sha1.new().update(data).finish().

Collision-broken (SHAttered, 2017): never use this to identify or sign content an attacker can influence. sha256 is the replacement. This is here for protocols that specify SHA-1 — git object ids, HMAC-SHA1 in OAuth 1 and TOTP.

Parameters

NameTypeNotes
dataArrayList(u8)

Returns: Array(u8, usize(20))

sha1_hex function
fn(data : ArrayList(u8)) -> String

SHA-1 of data as 40 lowercase hex characters — git's object-id spelling. Same collision caveat as sha1.

Parameters

NameTypeNotes
dataArrayList(u8)

Returns: String