SHA-1 hashing helpers for whole-input and incremental hashing.
- For byte-source requirements, see Accepted sources.
- Direct hashing and incremental hashing agree for the same valid byte sequence, before finalization.
- The vector abc is A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D.
- See Digest format.
ShaCounter
ShaCounter
(-- counter) Creates fresh incremental SHA-1 state for an empty input.- Initializes the five-word SHA-1 state, pending byte count zero, and total bit count zero.
Remarks
- The counter uses fixed In-place storage and defines no
INIT,DIE, or allocation-release method. - Copying it copies the current hashing state.
Fields
state: fiveNat32chaining words, initially the standard SHA-1 initialization values.buffer: fixedNat864 array for the pending block.bufferProcessed:Int32count of pending bytes; append maintains0 .. 63.bitSize:Nat64accumulated input bit count.
append
(source --) Appends a source chunk to the incremental state.- For byte-source requirements, see Accepted sources.
- Copies input bytes into the pending buffer and transforms every complete 64-byte block. Later changes to the source do not change appended data.
finish
(-- digest) Finalizes the accumulated input and returns its SHA-1 digest.- Padding includes the accumulated bit count.
- finish mutates the chaining state and working buffer. Call it once after the last append; it does not reset the counter, and repeated finish or append-after-finish does not continue the original message correctly. Use a fresh
ShaCounterfor a new message. To preserve an unfinished state, copy the counter and finish the copy.
Accepted sources
sha1 and ShaCounter.append convert inputs with toSpan.
- Supply a source that
toSpanconverts to a contiguousNat8span; its size must count bytes.Text,String,StringView,Nat8Array, byte spans, and nonempty homogeneous byte lists are supported. - Wider-element arrays are not byte sources for this API, even when
toSpanaccepts them; adapt them explicitly to bytes first. - A raw address alone and the empty
()are not supported input forms. For empty input, use emptyTextor a typed empty byte container or span.
Digest format
The digest has fresh fixed Nat8 20 array storage; it is not a String, a standard-library Array object, or a view into the input or counter. Bytes are emitted in standard SHA-1 digest order.
- No separate allocation-release operation is required for the fixed array.
Sha1Internal
Sha1Internal
(-- helpers) Provides the public low-level SHA-1 schedule, round, padding, and block-transform operations.Remarks
Sha1Internalis public and callable. It is an implementation helper; callers do not need it forsha1orShaCounter.
Remarks
m
(x i -- word) Updates slot i & 15 of a mutable view of a 16-word Nat32 schedule and returns the new word.iisNat32. The update XORs slotsi,i + 2,i + 8, andi + 13modulo 16, then rotates the result left by one.
r
(a b c d e f word --) Performs one SHA-1 round and updates the supplied registers.bandemust be mutable references to distinctNat32registers. Other numeric inputs areNat32.fconsumes threeNat32values and returns oneNat32round value through a call boundary. The round writese + rol(a, 5) + f(b, c, d) + wordtoeand rotatesbleft by 30, using wrapping arithmetic.
finish
(count bitSize buffer state -- digest) Pads the pending input and emits a fresh fixed 20-byte digest.countis anInt32in0 .. 63;bitSizeisNat64;bufferis a mutable 64-byteNat8array; andstateis a mutable view of fiveNat32words.- State must already represent complete blocks,
bitSizeis the total pre-padding bit count, and the firstcountbuffer bytes are the remaining tail. The method mutates both buffer and state. - The result has fresh fixed
Nat820 array storage in standard SHA-1 digest order.
Examples
Incremental and direct hashing agree
The runtime example compares the digest arrays with algorithm's collection = overload; its output is TRUE.
"String" use
"algorithm" use
"control" use
"sha1" use
{} Int32 {} [
direct: "abc" sha1;
counter: ShaCounter;
"ab" @counter.append
"c" @counter.append
incremental: @counter.finish;
(direct incremental = LF) printList
0
] "main" exportFunction
Expected Output
TRUE
Digest bytes of "abc"
"String" use
"control" use
"sha1" use
{} Int32 {} [
digest: "abc" sha1;
out: String;
20 [
byte: i digest @;
i 0 > [" " @out.catString] when
byte 16n8 < ["0" @out.catString] when
byte @out.catHex
] times
@out.getStringView print
0
] "main" exportFunction
Expected Output
A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D
See also
- Span: Contiguous non-owning view of items of the same schema.
- murmurHash: Murmur3 32-bit hashing helpers over raw byte storage.