Key derivation and public-key signatures.
PBKDF2 runs natively because interpreting thousands of HMAC rounds per
call would be prohibitively slow. The signature functions cover the
JWT algorithms: RSA (RS256) signing and verification, and ECDSA
P-256 (ES256) verification. For plain hashes see ::hot::hash, for
HMAC see ::hot::hmac, for Ed25519 see ::hot::hash/ed25519-verify.
Functions
ecdsa-p256-sha256-verify
fn (x: Bytes, y: Bytes, message: Str | Bytes, signature: Bytes): Bool
Verify an ECDSA P-256 / SHA-256 (ES256) signature against an EC
public key. x and y are the 32-byte big-endian curve coordinates —
exactly the JWK x/y fields after base64url decoding. signature is
the 64-byte r || s form that JWS uses (not DER). Returns false for a
bad signature; errors if x or y is not 32 bytes.
Example
::crypto ::hot::crypto
::b64 ::hot::base64
jwk {x: "f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU", y: "x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0"}
header "eyJhbGciOiJSUzI1NiJ9"
payload "eyJzdWIiOiIxIn0"
signature ::b64/decode-url("c2lnbmF0dXJl")
x ::b64/decode-url(jwk.x)
y ::b64/decode-url(jwk.y)
valid ::crypto/ecdsa-p256-sha256-verify(x, y, `${header}.${payload}`, signature)
pbkdf2-hmac-sha256
fn (password: Str | Bytes, salt: Str | Bytes, iterations: Int): Bytes
fn (password: Str | Bytes, salt: Str | Bytes, iterations: Int, length: Int): Bytes
PBKDF2 key derivation using HMAC-SHA256 (RFC 2898). This is the Hi()
function from SCRAM-SHA-256 (RFC 5802/7677), used by Postgres and other
SASL-based protocols to derive the salted password.
Returns the derived key as Bytes. The default length is 32 bytes
(the SHA-256 output size); pass length to derive more or fewer.
Example
::crypto ::hot::crypto
::hex ::hot::hex
key ::crypto/pbkdf2-hmac-sha256("password", "salt", 1)
::hex/encode(key)
// "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b"
// SCRAM-SHA-256 SaltedPassword (RFC 7677 test vector)
::b64 ::hot::base64
salt ::b64/decode("W22ZaJ0SNY7soEsUEjb6gQ==")
salted ::crypto/pbkdf2-hmac-sha256("pencil", salt, 4096)
rsa-sha256-sign
fn (private-key-pem: Str, message: Str | Bytes): Bytes
Sign a message with RSASSA-PKCS1-v1_5 / SHA-256 (the JWT RS256
algorithm). Returns the raw signature Bytes — base64url-encode them for
a JWT. Accepts PKCS#8 (BEGIN PRIVATE KEY, e.g. Google service
accounts) and PKCS#1 (BEGIN RSA PRIVATE KEY, e.g. GitHub Apps) PEM
private keys.
Example
::crypto ::hot::crypto
::b64 ::hot::base64
private-key-pem ::hot::ctx/get("jwt.private.key")
header "eyJhbGciOiJSUzI1NiJ9"
payload "eyJzdWIiOiIxIn0"
signature ::crypto/rsa-sha256-sign(private-key-pem, `${header}.${payload}`)
jwt `${header}.${payload}.${::b64/encode-url(signature)}`
rsa-sha256-verify
fn (n: Bytes, e: Bytes, message: Str | Bytes, signature: Bytes): Bool
Verify an RSASSA-PKCS1-v1_5 / SHA-256 (RS256) signature against RSA
public key components. n (modulus) and e (exponent) are big-endian
Bytes — exactly the JWKS n/e fields after base64url decoding.
Returns false for a bad signature.
Example
::crypto ::hot::crypto
::b64 ::hot::base64
jwk {n: "sXchDaQebHnPiGvyDOAT4saGEUetSyo9MKLOoWFsueri23bOdgWp4Dy1WlU", e: "AQAB"}
header "eyJhbGciOiJSUzI1NiJ9"
payload "eyJzdWIiOiIxIn0"
signature ::b64/decode-url("c2lnbmF0dXJl")
n ::b64/decode-url(jwk.n)
e ::b64/decode-url(jwk.e)
valid ::crypto/rsa-sha256-verify(n, e, `${header}.${payload}`, signature)