crypto.hmac-sha1
HMAC-SHA1 of a message under a secret key (RFC 2104, RFC 2202), the MAC under HOTP and TOTP codes.
1.0.0 · published 2026-10-10 by charlie · Anterra
Pinned by 14 tests, run in TypeScript, Python and Rust.
What it does
`hmacSha1(key, message)` is the 20-byte HMAC-SHA1 tag of `message` under `key`, as RFC 2104 defines HMAC and RFC 2202 pins it for SHA-1. It is the MAC underneath HOTP (RFC 4226) and TOTP (RFC 6238) one-time codes, which is why it is here: use `auth.totp` for those.
For anything new, sign with `crypto.hmac-sha256`. HMAC-SHA1 is not broken (HMAC does not rely on the hash resisting collisions), but SHA-1 is, and a reviewer should not have to make that argument for your webhook signatures.
For example
hmac_sha1(11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 72, 105, 32, 84, 104, 101, 114, 101)→ 182, 23, 49, 134, 85, 5, 114, 100, 226, 139, 192, 182, 251, 55, 140, 142, 241, 70, 190, 0 RFC 2202 test case 1: a 20-byte key of 0x0bhmac_sha1(74, 101, 102, 101, 119, 104, 97, 116, 32, 100, 111, 32, 121, 97, 32, 119, 97, 110, 116, 32, 102, 111, 114, 32, 110, 111, 116, 104, 105, 110, 103, 63)→ 239, 252, 223, 106, 229, 235, 47, 162, 210, 116, 22, 213, 241, 132, 223, 156, 37, 154, 124, 121 RFC 2202 test case 2: a key shorter than the outputhmac_sha1(170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221,…)→ 18, 93, 115, 66, 185, 172, 17, 205, 145, 163, 154, 244, 138, 161, 123, 79, 99, 241, 117, 211 RFC 2202 test case 3: 50 bytes of 0xdd under 20 bytes of 0xaa
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 hmac_sha1(key: Sequence[int], message: Sequence[int]) -> List[int]
| key | int[] | the secret, any length; one longer than 64 bytes is hashed first, as RFC 2104 says |
| message | int[] | the bytes to authenticate |
| returns | int[] | the 20-byte tag; compare tags with crypto.constant-time-equal, never == |
Your code names it in one line, in the file that uses it
from fune.crypto.hmac_sha1 import hmac_sha1 # crypto.hmac-sha1@^1
import hashlib
import hmac
from typing import Any, List, Sequence
def _check_bytes(value: Any, name: str) -> bytes:
if isinstance(value, (str, dict)) or not hasattr(value, "__len__"):
raise TypeError("%s must be a list of integers from 0 to 255" % name)
for b in value:
if type(b) is not int or b < 0 or b > 255:
raise ValueError("%s must be a list of integers from 0 to 255" % name)
return bytes(value)
def hmac_sha1(key: Sequence[int], message: Sequence[int]) -> List[int]:
"""HMAC-SHA1 (RFC 2104) from the standard library's hmac module.
The byte values are checked here so a bad value fails with the same words
as in TypeScript and Rust.
"""
k = _check_bytes(key, "key")
m = _check_bytes(message, "message")
return list(hmac.new(k, m, hashlib.sha1).digest())Install
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 1 dependency, 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.hmac-sha1
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./crypto.hmac-sha1-1.0.0-python.fune, or fetch it from a terminal with fune pull crypto.hmac-sha1@1.0.0:python.
The whole function, every language, is one file too: crypto.hmac-sha1-1.0.0.fune, 12,950 bytes, sha256 c025a900a96b3914007b946f536fa5839afd5973c2f91af62a195dc9c7b9215e. 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.hmac-sha1
after — your function gets the result and the arguments, and returns the final result.
# fune: after crypto.hmac-sha1
replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.
# fune: replace crypto.sha1 in crypto.hmac-sha1
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.hmac-sha1 --steps.
# fune: step crypto.hmac-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.
| Case | Arguments | Expected | |
|---|---|---|---|
| RFC 2202 test case 1: a 20-byte key of 0x0b | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 72, 105, 32, 84, 104, 101, 114, 101 | → | 182, 23, 49, 134, 85, 5, 114, 100, 226, 139, 192, 182, 251, 55, 140, 142, 241, 70, 190, 0 |
| RFC 2202 test case 2: a key shorter than the output | 74, 101, 102, 101, 119, 104, 97, 116, 32, 100, 111, 32, 121, 97, 32, 119, 97, 110, 116, 32, 102, 111, 114, 32, 110, 111, 116, 104, 105, 110, 103, 63 | → | 239, 252, 223, 106, 229, 235, 47, 162, 210, 116, 22, 213, 241, 132, 223, 156, 37, 154, 124, 121 |
| RFC 2202 test case 3: 50 bytes of 0xdd under 20 bytes of 0xaa | 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221,… | → | 18, 93, 115, 66, 185, 172, 17, 205, 145, 163, 154, 244, 138, 161, 123, 79, 99, 241, 117, 211 |
| RFC 2202 test case 4: a 25-byte counting key | 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, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205… | → | 76, 144, 7, 244, 2, 98, 80, 198, 188, 132, 20, 249, 191, 80, 200, 108, 45, 114, 53, 218 |
| RFC 2202 test case 5: the full tag (the RFC also lists its 96-bit truncation) | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 84, 101, 115, 116, 32, 87, 105, 116, 104, 32, 84, 114, 117, 110, 99, 97, 116, 105, 111, 110 | → | 76, 26, 3, 66, 75, 85, 224, 127, 231, 242, 123, 225, 213, 139, 185, 50, 74, 154, 90, 4 |
| RFC 2202 test case 6: an 80-byte key is hashed first | 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,… | → | 170, 74, 229, 225, 82, 114, 208, 14, 149, 112, 86, 55, 206, 138, 59, 85, 237, 64, 33, 18 |
| RFC 2202 test case 7: an 80-byte key and a 73-byte message | 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,… | → | 232, 233, 157, 15, 69, 35, 125, 120, 109, 107, 186, 167, 150, 92, 120, 8, 187, 255, 26, 145 |
| the empty key and the empty message | , | → | 251, 219, 29, 27, 24, 170, 108, 8, 50, 75, 125, 100, 183, 31, 183, 99, 112, 105, 14, 29 |
| a key of exactly one block (64 bytes) is used as is, not hashed (Python hmac 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… | → | 84, 136, 177, 216, 225, 14, 125, 188, 45, 185, 27, 231, 129, 163, 1, 21, 146, 226, 88, 166 |
| a key one byte over the block is hashed (Python hmac 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… | → | 13, 95, 37, 153, 157, 45, 13, 176, 228, 60, 65, 172, 218, 216, 142, 72, 30, 121, 86, 216 |
Show the other 4 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| RFC 4226 Appendix D: the HOTP secret at counter 0 (its HMAC is published) | 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 0, 0, 0, 0, 0, 0, 0, 0 | → | 204, 147, 207, 24, 80, 141, 148, 147, 76, 100, 182, 93, 139, 167, 102, 127, 183, 205, 228, 176 |
| a key byte above 255 | 1, 256, 1 | → | error: key must be a list of integers from 0 to 255 |
| a fractional message byte | 1, 0.5 | → | error: message must be a list of integers from 0 to 255 |
| a message given as text rather than bytes | 1, Hi There | → | error: message 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
Bytes in and out are lists of integers 0 to 255; a text secret or message goes through `encoding.utf8` first. The construction is `H((K xor opad) || H((K xor ipad) || message))` with the key zero-padded to SHA-1's 64-byte block, or first hashed to 20 bytes when it is longer than 64 (RFC 2202 test cases 6 and 7, with an 80-byte key, check that rule).
**Compare tags with `crypto.constant-time-equal`**, never with `==`.
TypeScript and Rust build on `crypto.sha1`, so they run anywhere, the browser included; Python uses the standard library's `hmac`. The vectors are all seven test cases of RFC 2202 section 3, plus block-boundary keys with Python's `hmac` as the reference. Test case 5's RFC also lists a 96-bit truncation; the vector holds the full 20-byte digest.
Source: RFC 2104 (https://www.rfc-editor.org/rfc/rfc2104); RFC 2202, Test Cases for HMAC-MD5 and HMAC-SHA-1, section 3 (https://www.rfc-editor.org/rfc/rfc2202).
Files
| Path | Bytes |
|---|---|
| README.md | 1,445 |
| impl/python.py | 836 |
| impl/rust.rs | 1,755 |
| impl/typescript.ts | 1,305 |
| vectors.json | 5,731 |