Functional Weave
Code in TypeScript

crypto.hmac-sha1@1.0.0

README.md

1,445 bytes · view raw

# crypto.hmac-sha1

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

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