Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v0 source pure

Package sha1 implements SHA-1 (FIPS 180-1, RFC 3174) as a pure, reusable package: one-shot, streaming, HMAC-SHA1, and...

Readme View source

gno.land/p/moul/x/daily/sha1/v0

SHA-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.

Overview

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).

Constants 2

const BlockSize

1const BlockSize = 64
source

BlockSize is SHA-1's block size in bytes. HMAC's key padding depends on it.

const Size

1const Size = 20
source

Size is the length of a SHA-1 digest in bytes.

Functions 8

func GitBlobID

1func GitBlobID(content string) string
source

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`.

func HMAC

1func HMAC(key, msg string) [Size]byte
source

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.

func HMACHex

1func HMACHex(key, msg string) string
source

HMACHex returns the HMAC-SHA1 of msg under key as lowercase hex.

func Hex

1func Hex(sum [Size]byte) string
source

Hex renders a digest as 40 lowercase hex digits.

func Sum

1func Sum(s string) [Size]byte
source

Sum returns the SHA-1 digest of s.

func SumHex

1func SumHex(s string) string
source

SumHex returns the SHA-1 digest of s as 40 lowercase hex digits.

func Verify

1func Verify(s, hexDigest string) bool
source

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.

func New

1func New() *Digest
source

New returns a Digest ready to hash a fresh message.

Types 1

type Digest

struct
1type Digest struct {
2	h   [5]uint32
3	buf [BlockSize]byte
4	n   int    // bytes currently in buf
5	len uint64 // total bytes written
6}
source

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.

Methods on Digest

func Reset

method on Digest
1func (d *Digest) Reset()
source

Reset restarts the digest as if nothing had been written.

func Sum

method on Digest
1func (d *Digest) Sum() [Size]byte
source

Sum returns the digest of everything written so far, without finalizing the receiver: padding is applied to a copy.

func Write

method on Digest
1func (d *Digest) Write(s string)
source

Write adds s to the message. Any number of calls is equivalent to one call with the concatenation.

Dependencies

Imports 1

/p/moul/x/daily/sha1/v0

Source Files 3