Functional Weave
Code in TypeScript

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.

export function sha1(bytes: readonly number[]): readonly number[]
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

import { sha1 } from "#fune/crypto.sha1@^1";
impl/typescript.ts · 87 lines · open · raw
/**
 * The SHA-1 digest of a byte string (FIPS 180-4 section 6.1).
 *
 * Only for HMAC-SHA1 in HOTP/TOTP: SHA-1 is broken for collisions. Pure,
 * synchronous code with no node:crypto or crypto.subtle, so it runs in the
 * browser too. Arithmetic is on signed 32-bit integers (`| 0`), which wrap
 * exactly as the standard's mod 2^32 additions require.
 */
export function sha1(bytes: readonly number[]): readonly number[] {
  if (!Array.isArray(bytes) && !(bytes instanceof Uint8Array)) {
    throw new TypeError("bytes must be a list of integers from 0 to 255");
  }
  const length = bytes.length;
  for (let i = 0; i < length; i++) {
    const b = bytes[i];
    if (typeof b !== "number" || !Number.isInteger(b) || b < 0 || b > 255) {
      throw new RangeError("bytes must be a list of integers from 0 to 255");
    }
  }

  // Padding (section 5.1.1): a 1 bit, zeros, then the length in bits as a
  // 64-bit big-endian integer, to a multiple of 64 bytes.
  const blocks = Math.floor((length + 8) / 64) + 1;
  const padded = new Uint8Array(blocks * 64);
  for (let i = 0; i < length; i++) padded[i] = bytes[i];
  padded[length] = 0x80;
  const bitsHigh = Math.floor(length / 0x20000000);
  const bitsLow = (length * 8) >>> 0;
  const end = padded.length;
  padded[end - 8] = bitsHigh >>> 24;
  padded[end - 7] = (bitsHigh >>> 16) & 255;
  padded[end - 6] = (bitsHigh >>> 8) & 255;
  padded[end - 5] = bitsHigh & 255;
  padded[end - 4] = bitsLow >>> 24;
  padded[end - 3] = (bitsLow >>> 16) & 255;
  padded[end - 2] = (bitsLow >>> 8) & 255;
  padded[end - 1] = bitsLow & 255;

  const h = new Int32Array([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]);
  const w = new Int32Array(80);
  for (let block = 0; block < blocks; block++) {
    const base = block * 64;
    for (let t = 0; t < 16; t++) {
      const j = base + t * 4;
      w[t] = (padded[j] << 24) | (padded[j + 1] << 16) | (padded[j + 2] << 8) | padded[j + 3];
    }
    for (let t = 16; t < 80; t++) {
      const x = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
      w[t] = (x << 1) | (x >>> 31);
    }
    let a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
    for (let t = 0; t < 80; t++) {
      let f: number;
      let k: number;
      if (t < 20) {
        f = (b & c) | (~b & d);
        k = 0x5a827999;
      } else if (t < 40) {
        f = b ^ c ^ d;
        k = 0x6ed9eba1;
      } else if (t < 60) {
        f = (b & c) | (b & d) | (c & d);
        k = 0x8f1bbcdc | 0;
      } else {
        f = b ^ c ^ d;
        k = 0xca62c1d6 | 0;
      }
      const temp = (((a << 5) | (a >>> 27)) + f + e + k + w[t]) | 0;
      e = d;
      d = c;
      c = (b << 30) | (b >>> 2);
      b = a;
      a = temp;
    }
    h[0] = (h[0] + a) | 0;
    h[1] = (h[1] + b) | 0;
    h[2] = (h[2] + c) | 0;
    h[3] = (h[3] + d) | 0;
    h[4] = (h[4] + e) | 0;
  }

  const out: number[] = [];
  for (let i = 0; i < 5; i++) {
    out.push((h[i] >>> 24) & 255, (h[i] >>> 16) & 255, (h[i] >>> 8) & 255, h[i] & 255);
  }
  return out;
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the TypeScript 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 TypeScript crypto.sha1-1.0.0-typescript.fune · 10,888 bytes sha256 9ffc92ed8fca0607e31e3075ee34d6f938349bc1965004d1f648ade3fb97808d

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

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