Algorithms & Keys
Supported JWS Algorithms¶
These are the JWS signature algorithms that can be used to sign and verify a JWT/JWS with SuperJWT:
algAlgorithm |
ktyKey Type |
__class__Key Class |
SuperJWT Support |
Reference |
|---|---|---|---|---|
HS256HMAC using SHA-256 |
octOctet Sequence |
OctKeysymmetric |
RFC 7518 |
|
HS384HMAC using SHA-384 |
octOctet Sequence |
OctKeysymmetric |
RFC 7518 | |
HS512HMAC using SHA-512 |
octOctet Sequence |
OctKeysymmetric |
RFC 7518 | |
RS256RSASSA-PKCS1-v1_5 using SHA-256 |
RSARSA |
RSAKeyasymmetric |
RFC 7518 | |
RS384RSASSA-PKCS1-v1_5 using SHA-384 |
RSARSA |
RSAKeyasymmetric |
RFC 7518 | |
RS512RSASSA-PKCS1-v1_5 using SHA-512 |
RSARSA |
RSAKeyasymmetric |
RFC 7518 | |
PS256RSASSA-PSS using SHA-256 and MGF1 with SHA-256 |
RSARSA |
RSAKeyasymmetric |
RFC 7518 | |
PS384RSASSA-PSS using SHA-384 and MGF1 with SHA-384 |
RSARSA |
RSAKeyasymmetric |
RFC 7518 | |
PS512RSASSA-PSS using SHA-512 and MGF1 with SHA-512 |
RSARSA |
RSAKeyasymmetric |
RFC 7518 | |
ES256ECDSA using secp256r1 curve and SHA-256 |
ECElliptic Curve |
ECKeyasymmetric |
RFC 7518 | |
ES256KECDSA using secp256k1 curve and SHA-256 |
ECElliptic Curve |
ECKeyasymmetric |
RFC 8812 | |
ES384ECDSA using secp384r1 curve and SHA-384 |
ECElliptic Curve |
ECKeyasymmetric |
RFC 7518 | |
ES512ECDSA using secp521r1 curve and SHA-512 |
ECElliptic Curve |
ECKeyasymmetric |
RFC 7518 | |
EdDSAEdDSA signature algorithms (deprecated, use Ed25519 or Ed448) |
OKPOctet Key Pair |
OKPKeyasymmetric |
RFC 8037 (deprecated) |
|
Ed25519EdDSA using Ed25519 curve |
OKPOctet Key Pair |
OKPKeyasymmetric |
RFC 9864 | |
Ed448EdDSA using Ed448 curve |
OKPOctet Key Pair |
OKPKeyasymmetric |
RFC 9864 |
Installation Requirement
Asymmetric algorithms require the cryptography library. You can install it with:
Which algorithm to choose? See What is best, and why?
TL;DR: use Ed25519 for asymmetric, HS256 for symmetric!
How to Generate Keys¶
Secret Key 🔄¶
for a symmetric algorithm
Uses the same secret key for encoding and decoding a JWT. The secret key is a random byte sequence. Compatible with HMAC signature algorithms: HS256, HS384, and HS512.
View code
from superjwt import Alg
# generate a 64 bytes (512 bits) secret key for HMAC+SHA512
jws_alg = Alg.HS512.get_instance()
key = jws_alg.generate_key()
print(key.private_key)
#> b'58ff0cd9d90969b968d2b41b3ca1fa3f99f501faabeca16cd7715139b827c99ba676b68a76cc5a75a08105220833167878b30d32e4963a10f069ee79e7413f69'
from superjwt import OctKey
# generate a 42-byte hex string key (default)
key = OctKey.generate(42, human_readable=True)
print(key.private_key)
#> b'b7f5bea63d48a2cf545b6f1495f07d6f1314a2f71951c2d95c7cf53a3ecdcc7af22c8b649cfa161d1658'
# generate a 42-byte raw bytes key
key = OctKey.generate(42, human_readable=False)
print(key.private_key)
#> b'o\xb7C\xb8\xb9\xd5\x97\x19\xd5<\x8a\x8a\x89s\xa73t\xf0\x93\xbc\xcc\xb8@\xe2\xe5\x85\xa0\xbcb\x05\xb5Y\xe5\xf2\xad\x902a5[Y\xa2'
Key Pair 🔀¶
for an asymmetric algorithm
| Key Type | Header (Start) | Footer (End) |
|---|---|---|
| Private Key (PKCS#8) | -----BEGIN PRIVATE KEY----- |
-----END PRIVATE KEY----- |
| Public Key (PEM) | -----BEGIN PUBLIC KEY----- |
-----END PUBLIC KEY----- |
RSA Key Pair¶
Compatible with RSA signature algorithms: RS256, RS384, RS512, PS256, PS384, and PS512.
View code
from superjwt import RSAKey
key = RSAKey.generate(2048)
print(key.private_key)
#> b'-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9...CQZaE0rgg8=\n-----END PRIVATE KEY-----\n'
print(key.public_key)
#> b'-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQ...IDAQAB\n-----END PUBLIC KEY-----\n'
from superjwt import RSAKey
key = RSAKey.generate(3072)
print(key.private_key)
#> b'-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9...CQZaE0rgg8=\n-----END PRIVATE KEY-----\n'
print(key.public_key)
#> b'-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQ...IDAQAB\n-----END PUBLIC KEY-----\n'
from superjwt import RSAKey
key = RSAKey.generate(4096)
print(key.private_key)
#> b'-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9...CQZaE0rgg8=\n-----END PRIVATE KEY-----\n'
print(key.public_key)
#> b'-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQ...IDAQAB\n-----END PUBLIC KEY-----\n'
RSA Key Sizes
- 2048 bits: A widely accepted minimum standard for many years, but it will be deprecated in 2030.
- 3072 bits: Offers a higher security level and is recommended for strong, long-term security.
- 4096 bits: Provides even greater long-term security but requires significantly more processing power.
Elliptic Curve (ECDSA)¶
Compatible with these ECDSA signature algorithms: ES256, ES256K, ES384, and ES512.
View code
from superjwt import Alg
jws_alg = Alg.ES256.get_instance()
key = jws_alg.generate_key()
print(key.private_key)
#> b'-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGAgEGC...aOcF4E+n9/wc\n-----END PRIVATE KEY-----\n'
print(key.public_key)
#> b'-----BEGIN PUBLIC KEY-----\nMFkwEwYHKo...jnBeBPp/f8HA==\n-----END PUBLIC KEY-----\n'
from superjwt import Alg
jws_alg = Alg.ES256K.get_instance()
key = jws_alg.generate_key()
print(key.private_key)
#> b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM...QYnLhTnBnCKLQMe\n-----END PRIVATE KEY-----\n'
print(key.public_key)
#> b'-----BEGIN PUBLIC KEY-----\nMFkwEwYHKo...jnBeBPp/f8HA==\n-----END PUBLIC KEY-----\n'
from superjwt import Alg
jws_alg = Alg.ES384.get_instance()
key = jws_alg.generate_key()
print(key.private_key)
#> b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM...QYnLhTnBnCKLQMe\n-----END PRIVATE KEY-----\n'
print(key.public_key)
#> b'-----BEGIN PUBLIC KEY-----\nMFkwEwYHKo...jnBeBPp/f8HA==\n-----END PUBLIC KEY-----\n'
from superjwt import Alg
jws_alg = Alg.ES512.get_instance()
key = jws_alg.generate_key()
print(key.private_key)
#> b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM...QYnLhTnBnCKLQMe\n-----END PRIVATE KEY-----\n'
print(key.public_key)
#> b'-----BEGIN PUBLIC KEY-----\nMFkwEwYHKo...jnBeBPp/f8HA==\n-----END PUBLIC KEY-----\n'
Octet Key Pair (EdDSA)¶
Compatible with these EdDSA signature algorithms: Ed25519 and Ed448.
View code
from superjwt import Alg
jws_alg = Alg.Ed25519.get_instance()
key = jws_alg.generate_key()
print(key.private_key)
#> b'-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2...tRRQq/r9bcmGe2ODBBz\n-----END PRIVATE KEY-----\n'
print(key.public_key)
#> b'-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwA...aL4MNzotMRLDwHw=\n-----END PUBLIC KEY-----\n'
from superjwt import Alg
jws_alg = Alg.Ed448.get_instance()
key = jws_alg.generate_key()
print(key.private_key)
#> b'-----BEGIN PRIVATE KEY-----\nMEcCAQAwBQYDK2...CHJRpieeRB4RR6VZA==\n-----END PRIVATE KEY-----\n'
print(key.public_key)
#> b'-----BEGIN PUBLIC KEY-----\nMEMwBQYDK2VxA...Rle7Dh0PZxkvBa6A\n-----END PUBLIC KEY-----\n'