Skip to Content
oxcrypto

JWS

Sign and verify JWS compact tokens with HMAC, RSA, ECDSA and EdDSA.

from oxcrypto.jose.jws import JWS, unverified_header
from oxcrypto.signature.ecdsa import ECDSA

key = ECDSA.P256.generate()
token = JWS.ES256.sign(b'{"sub":"alice"}', key, header={"kid": "1"})
assert unverified_header(token) == {"alg": "ES256", "kid": "1"}
assert JWS.ES256.verify(token, key.public_key())
assert JWS.ES256.decode(token, key.public_key()) == b'{"sub":"alice"}'
SelectorKeyAlgorithm
HS256, HS384, HS512bytes, at least 32, 48 or 64 bytesHMAC with SHA-2
RS256, RS384, RS512RSAPrivateKey / RSAPublicKey, at least 2048 bitsRSASSA-PKCS1-v1_5
PS256, PS384, PS512RSAPrivateKey / RSAPublicKey, at least 2048 bitsRSASSA-PSS, MGF1 with the same hash, salt length equal to the hash size
ES256, ES384, ES512ECDSAPrivateKey / ECDSAPublicKey on P-256, P-384, P-521ECDSA, fixed-size `r
EDDSAEd25519PrivateKey / Ed448PrivateKey and their public keys (pure variants)Ed25519 or Ed448 (RFC 8037)

sign(payload, key, *, header=None) adds alg itself; a header that sets alg or crit raises ValueError. verify(token, key) returns False for a bad signature, a malformed token, alg: none or another algorithm, and raises TypeError for a key of the wrong type. decode returns the payload and raises InvalidTag instead of returning False.

ECDSA and EdDSA signatures come from the oxcrypto ecdsa, ed25519 and ed448 implementations; ECDSA nonces are deterministic (RFC 6979). Ed448 is experimental, see Ed448.

Tests use RFC 7515 Appendix A.1-A.4 and RFC 8037 Appendix A.4.

Zeroization

Note

HMAC secrets are Python bytes and are not wiped. Signing keys follow the zeroization notes of their signature family.

On this page