Functional Weave
Code in Python

crypto.sha1

The SHA-1 digest of a byte string (FIPS 180-4), for HMAC-SHA1 in HOTP/TOTP codes; not for new signatures.

1.0.0 · published 2026-10-10 by charlie · Anterra

Pinned by 14 tests, run in TypeScript, Python and Rust.

What it does

`sha1(utf8Encode("abc"))` is the 20 bytes `a9993e364706816aba3e25717850c26c9cd0d89d` (print them with `hexEncode`). This is SHA-1 exactly as FIPS 180-4 (and RFC 3174) defines it.

**SHA-1 is broken for collisions** (SHAttered, 2017; chosen-prefix collisions, 2020). Do not use it for new signatures, certificates, content addressing or anything where two different inputs with the same digest would hurt. Use `crypto.sha256`.

For example

  • sha1() → 218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9 FIPS 180-4 example: the empty message
  • sha1(97, 98, 99) → 169, 153, 62, 54, 71, 6, 129, 106, 186, 62, 37, 113, 120, 80, 194, 108, 156, 208, 216, 157 FIPS 180-4 / RFC 3174 example: "abc", one block
  • sha1(97, 98, 99, 100, 98, 99, 100, 101, 99, 100, 101, 102, 100, 101, 102, 103, 101, 102, 103, 104, 102, 103, 104, 105, 103, 104, 105, 106, 104, 105, 106, 107, 105, 106, 107, 108, 106, …) → 132, 152, 62, 68, 28, 59, 210, 110, 186, 174, 74, 161, 249, 81, 41, 229, 229, 70, 112, 241 FIPS 180-4 / RFC 3174 example: the 448-bit message, whose padding needs a second block

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def sha1(bytes: Sequence[int]) -> List[int]
bytesint[]the message, each an integer from 0 to 255; hash text with encoding.utf8 first
returnsint[]the 20-byte digest; encoding.hex prints it the way sha1sum does

Your code names it in one line, in the file that uses it

from fune.crypto.sha1 import sha1  # crypto.sha1@^1
impl/python.py · 18 lines · open · raw
import builtins
import hashlib
from typing import List, Sequence


def sha1(bytes: Sequence[int]) -> List[int]:
    """The SHA-1 digest of a byte string (FIPS 180-4), from hashlib.

    Only for HMAC-SHA1 in HOTP/TOTP; SHA-1 is broken for collisions. The byte
    values are checked here so a bad value fails with the same words as in
    TypeScript and Rust.
    """
    if isinstance(bytes, (str, dict)) or not hasattr(bytes, "__len__"):
        raise TypeError("bytes must be a list of integers from 0 to 255")
    for b in bytes:
        if type(b) is not int or b < 0 or b > 255:
            raise ValueError("bytes must be a list of integers from 0 to 255")
    return list(hashlib.sha1(builtins.bytes(bytes)).digest())

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add crypto.sha1
Download for Python crypto.sha1-1.0.0-python.fune · 8,531 bytes sha256 37b147f1e73c1de7dc96abb1be6cf1fcd1af1d1fd5792265234b5076c718902b

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./crypto.sha1-1.0.0-python.fune, or fetch it from a terminal with fune pull crypto.sha1@1.0.0:python.

The whole function, every language, is one file too: crypto.sha1-1.0.0.fune, 14,410 bytes, sha256 49a7ecb36cfc652b1c92199ff09ea726775cc58ad6963fe54362f2610765f121. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before crypto.sha1

after — your function gets the result and the arguments, and returns the final result.

# fune: after crypto.sha1

replace — it requires no other capability, so there is no dependency to replace.

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show crypto.sha1 --steps.

# fune: step crypto.sha1 after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
FIPS 180-4 example: the empty message → 218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9
FIPS 180-4 / RFC 3174 example: "abc", one block 97, 98, 99 → 169, 153, 62, 54, 71, 6, 129, 106, 186, 62, 37, 113, 120, 80, 194, 108, 156, 208, 216, 157
FIPS 180-4 / RFC 3174 example: the 448-bit message, whose padding needs a second block 97, 98, 99, 100, 98, 99, 100, 101, 99, 100, 101, 102, 100, 101, 102, 103, 101, 102, 103, 104, 102, 103, 104, 105, 103, 104, 105, 106, 104, 105, 106, 107, 105, 106, 107, 108, 106, … → 132, 152, 62, 68, 28, 59, 210, 110, 186, 174, 74, 161, 249, 81, 41, 229, 229, 70, 112, 241
FIPS 180-4 example: the 896-bit message 97, 98, 99, 100, 101, 102, 103, 104, 98, 99, 100, 101, 102, 103, 104, 105, 99, 100, 101, 102, 103, 104, 105, 106, 100, 101, 102, 103, 104, 105, 106, 107, 101, 102, 103, 104, 105, … → 164, 155, 36, 70, 160, 44, 100, 91, 244, 25, 249, 149, 182, 112, 145, 37, 58, 4, 162, 89
the pangram 84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, … → 47, 212, 225, 198, 122, 45, 40, 252, 237, 132, 158, 225, 187, 118, 231, 57, 27, 147, 235, 18
55 bytes, the most that fits one block with its padding (hashlib as the reference) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 4… → 138, 226, 212, 103, 41, 207, 230, 143, 249, 39, 175, 94, 236, 156, 125, 27, 102, 214, 90, 194
56 bytes, one too many for one block (hashlib as the reference) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 4… → 99, 110, 46, 198, 152, 218, 201, 3, 73, 142, 100, 139, 210, 243, 175, 100, 29, 60, 136, 203
64 bytes, exactly one block of message (hashlib as the reference) 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235,… → 79, 3, 62, 108, 83, 25, 114, 168, 192, 155, 51, 136, 12, 43, 1, 16, 64, 33, 196, 56
119 bytes, the most that fits two blocks (hashlib as the reference) 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 182, 189, 196, 203, 210, 217, 224, 231, 238, 245, 252, 3, 10, 17, … → 180, 164, 230, 144, 96, 208, 177, 231, 232, 235, 191, 112, 65, 164, 33, 28, 99, 67, 139, 87
a single zero byte is not the empty message 0 → 91, 169, 60, 157, 176, 207, 249, 63, 82, 181, 33, 215, 66, 14, 67, 246, 237, 162, 120, 79
Show the other 4 tests
CaseArgumentsExpected
256 is not a byte 1, 256 → error: bytes must be a list of integers from 0 to 255
a fraction is not a byte 1.5 → error: bytes must be a list of integers from 0 to 255
a negative value is not a byte -1 → error: bytes must be a list of integers from 0 to 255
a character is not a byte: encode text with encoding.utf8 first a → error: bytes must be a list of integers from 0 to 255

Verification

Verified by the operator, not the sandbox 14 test vectors declared; none run by the registry.

Verified locally by the registry's operator, not by the sandbox. This version was published with FUNE_VERIFY_MODE=inline-dev (the operator's import of the first-party registry, or a development server): the server validated the package - its manifest, signatures, imports, safety lint and test rules - but the registry did not run its test vectors in its sandbox. The operator runs them with `fune verify --all` before importing.

Signed by the registry with key fw-9dc887227da47575 (ed25519), over its integrity and this report's digest. The full report · JSON

More from the author

It is in the registry only because HOTP (RFC 4226) and TOTP (RFC 6238) are defined over HMAC-SHA1, and nearly every authenticator app (Google Authenticator, Microsoft Authenticator, 1Password, Authy) still defaults to it. HMAC's security does not rest on collision resistance, so HMAC-SHA1 is still sound for that job (RFC 6151 section 4 says the same of HMAC-MD5). Use it through `crypto.hmac-sha1` or `auth.totp`.

Bytes in and out are lists of integers 0 to 255, as everywhere in the registry. TypeScript and Rust are plain code with no `node:crypto` or `crypto.subtle`; Python uses the standard library's `hashlib`.

The vectors are the published examples ("abc" and the 448-bit message from FIPS 180-4's example values and RFC 3174 section 7.3), the empty string, and messages of 55, 56, 64 and 119 bytes either side of the points where padding needs another block, with Python's `hashlib` as the reference.

Source: FIPS 180-4, Secure Hash Standard, section 6.1 (https://csrc.nist.gov/pubs/fips/180-4/upd1/final); RFC 3174, US Secure Hash Algorithm 1 (https://www.rfc-editor.org/rfc/rfc3174).

Files

PathBytes
README.md1,541
impl/python.py726
impl/rust.rs2,662
impl/typescript.ts3,012
vectors.json4,699