const BlockSize
BlockSize is SHA-1's block size in bytes. HMAC's key padding depends on it.
Package sha1 implements SHA-1 (FIPS 180-1, RFC 3174) as a pure, reusable package: one-shot, streaming, HMAC-SHA1, and...
gno.land/p/moul/x/daily/sha1/v0SHA-1 (FIPS 180-1, RFC 3174) Sum, SumHex, Hex, Verify, New,
Digest.Write, Digest.Sum, Digest.Reset, HMAC, HMACHex, GitBlobID.
Forty hex digits from any amount of input. The message is compressed 64 bytes
at a time into five 32-bit words through 80 rounds, and the whole algorithm is
those rounds plus one padding rule: a 0x80 byte, zeros up to 56 bytes mod 64,
then the length in bits as a big-endian uint64.
1import "gno.land/p/moul/x/daily/sha1/v0"
2
3sha1.SumHex("abc") // "a9993e364706816aba3e25717850c26c9cd0d89d"
4sha1.GitBlobID("hello world\n") // "3b18e512dba79e4c8300dd08aeb37f8e728b8dad"
5sha1.HMACHex("Jefe", "what do ya want for nothing?")
Digest.Sum is not destructive: it pads a copy, so a digest can be read,
written to again, and read again. That is the only reason the streaming form is
worth having, and there is a test asserting chunked equals one-shot.
GitBlobID hashes blob <len>\x00<content>, which is what git hash-object
does. The header is the entire difference between a git id and a content hash,
and the empty file's e69de29b... is the one digest a git user recognises.
Almost every bug in a hand-written SHA-1 lives in the padding rule, so the tests walk lengths 54, 55, 56, 57, 63, 64, 65, 119 and 128 across the boundary rather than hashing one long string and hoping.
⚠️ SHA-1 is broken as a collision-resistant hash. SHAttered (2017) produced two different PDFs with the same digest and a chosen-prefix collision followed in 2020. Never use it to decide that two documents are the same document, and never build a new signature scheme on it. It is still the correct answer where an external format specifies it: git object ids, HMAC-SHA1 in TOTP and older AWS signatures, legacy checksum interop.
Live demo: r/moul/x/daily/sha1demo
· render it at /r/moul/x/daily/sha1demo/v0.
Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.
🧪 Highly experimental — potentially vibe-coded. Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: DISCLAIMER.
Package sha1 implements SHA-1 (FIPS 180-1, RFC 3174) as a pure, reusable package: one-shot, streaming, HMAC-SHA1, and git object ids.
SHA-1 compresses the message 64 bytes at a time into five 32-bit words, through 80 rounds that rotate, add and mix a round constant chosen per quarter. The whole algorithm is those 80 rounds plus a padding rule: a single 0x80 byte, zeros up to 56 bytes mod 64, then the message length in bits as a big-endian uint64. Almost every bug in a hand-written SHA-1 is in that padding rule, which is why the tests walk every length across the boundary.
SHA-1 is BROKEN as a collision-resistant hash: SHAttered (2017) produced two different PDFs with the same digest, and a chosen-prefix collision followed in 2020. Never use it to decide that two documents are the same document, and never use it for a new signature scheme. It is still correct, and still the only answer, where an external format specifies it: git object ids, HMAC-SHA1 in TOTP and older AWS signatures, and legacy checksum interop.
A live demo of this package is at r/moul/x/daily/sha1demo(/r/moul/x/daily/sha1demo/v0).
GitBlobID returns the git object id of content: SHA-1 over the header "blob <len>\x00" followed by the bytes. It matches `git hash-object`.
HMAC returns the HMAC-SHA1 of msg under key (RFC 2104).
A key longer than BlockSize is hashed down to 20 bytes first, which is why an 80-byte key and the SHA-1 of that key are the same key.
HMACHex returns the HMAC-SHA1 of msg under key as lowercase hex.
Hex renders a digest as 40 lowercase hex digits.
Sum returns the SHA-1 digest of s.
SumHex returns the SHA-1 digest of s as 40 lowercase hex digits.
Verify reports whether s hashes to hexDigest, which may be in either case.
This is an equality check on a digest, not an authentication primitive: see the package doc on why SHA-1 must not decide that two documents match.
New returns a Digest ready to hash a fresh message.
Digest is a streaming SHA-1 state: feed it with Write, read it with Sum.
Sum is NOT destructive. It finalizes a copy, so a Digest can be read and then written to again, and reading it twice gives the same answer.
Reset restarts the digest as if nothing had been written.
Sum returns the digest of everything written so far, without finalizing the receiver: padding is applied to a copy.
Write adds s to the message. Any number of calls is equivalent to one call with the concatenation.