- Python wrapper for:
- Encode and decode JWT
- Support HS256, HS384, HS512
- encode datetime.timedelta to float (now + delta)
- encode datetime.datetime with timezone (example datetime.now(timezone.utc))
pip install rsjwt
from datetime import timedelta
import rsjwt
v = rsjwt.JWT("my-secret")
data = {
"exp": timedelta(hours=8),
"s": "123",
"a": ["123", 123],
"m": {"a": 1},
}
token = v.encode(data)
assert isinstance(token, str)
td = v.decode(token)
assert td["a"] == data["a"]
assert td["s"] == data["s"]
assert td["m"] == data["m"]
assert isinstance(td["exp"], float)
% hatch run bench:py
───────────────────────────────────────────────────────────────────────────────── bench.py3.9 ─────────────────────────────────────────────────────────────────────────────────
Python: 3.9.13 (v3.9.13:6de2ca5339, May 17 2022, 11:37:23)
[Clang 13.0.0 (clang-1300.0.29.30)]
Algorithm: HS256
Iterations: 1000000
| package | secs | n |
| ------------ | ------- | ------ |
| rsjwt | 2.2350 | 1.000 |
| pyjwt | 9.4796 | 4.241 |
| authlib | 12.0257 | 5.381 |
| python-jose | 14.7699 | 6.608 |
| jwcrypto | 61.5658 | 27.546 |
──────────────────────────────────────────────────────────────────────────────── bench.py3.13 ─────────────────────────────────────────────────────────────────────────────────
Python: 3.13.0 (main, Oct 7 2024, 05:02:14) [Clang 16.0.0 (clang-1600.0.26.4)]
Algorithm: HS256
Iterations: 1000000
| package | secs | n |
| ------------ | ------- | ------ |
| rsjwt | 2.1902 | 1.000 |
| pyjwt | 6.2054 | 2.833 |
| authlib | 7.2337 | 3.303 |
| jwcrypto | 41.4919 | 18.944 |
cargo fmt
cargo clippy
maturin develop
or use hatch envs:
hatch run fmt
hatch run check
hatch run build
hatch run test