Description
A Python implementation of JSON Web Signatures draft 02
Also now works on Python 3.3+ as well as Python 2.7+. However, it's a naive conversion to support both Python 2 and Python 3 so there may well be hidden bugs.
python-jws alternatives and similar packages
Based on the "Others" category.
Alternatively, view python-jws alternatives based on common mentions on social networks and blogs.
-
jose
Python implementation of the Javascript Object Signing and Encryption (JOSE) framework (https://datatracker.ietf.org/wg/jose/charter/)
CodeRabbit: AI Code Reviews for Developers

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of python-jws or a related project?
README
python-jws
๐จ This is Unmaintained ๐จ
This library is unmaintained and you should probably use https://github.com/latchset/jwcrypto instead.
For historical purposes, here are the docs
A Python implementation of JSON Web Signatures draft 02
Also now works on Python 3.3+ as well as Python 2.7+. However, it's a naive conversion to support both Python 2 and Python 3 so there may well be hidden bugs.
Installing
$ pip install jws
Algorithms
The JWS spec reserves several algorithms for cryptographic signing. Out of the 9, this library currently supports 7:
HMAC โ native
- HS256 โ HMAC using SHA-256 hash algorithm
- HS384 โ HMAC using SHA-384 hash algorithm
- HS512 โ HMAC using SHA-512 hash algorithm
RSA โ requires pycrypto >= 2.5: pip install pycrypto
- RS256 โ RSA using SHA-256 hash algorithm
ECDSA โ requires ecdsa lib: pip install ecdsa
- ES256 โ ECDSA using P-256 curve and SHA-256 hash algorithm
- ES384 โ ECDSA using P-384 curve and SHA-384 hash algorithm
- ES512 โ ECDSA using P-521 curve and SHA-512 hash algorithm
There is also a mechanism for extending functionality by adding your own algorithms without cracking open the whole codebase. See the advanced usage section for an example.
For RSA and ECDSA, all crypto libraries are lazily loaded so you won't need the dependencies unless you try to use the functionality.
Usage
Let's check out some examples.
>>> import jws
>>> header = { 'alg': 'HS256' }
>>> payload = { 'claim': 'JSON is the raddest.', 'iss': 'brianb' }
>>> signature = jws.sign(header, payload, 'secret')
>>> jws.verify(header, payload, signature, 'secret')
True
>>> jws.verify(header, payload, signature, 'badbadbad')
Traceback (most recent call last):
...
jws.exceptions.SignatureError: Could not validate signature
Now with a real key!
>>> import ecdsa
>>> sk256 = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p)
>>> vk = sk256.get_verifying_key()
>>> header = { 'alg': 'ES256' }
>>> sig = jws.sign(header, payload, sk256)
>>> jws.verify(header, payload, sig, vk)
True
Advanced Usage
Make this file
# file: sillycrypto.py
import jws
from jws.algos import AlgorithmBase, SignatureError
class FXUY(AlgorithmBase):
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
def sign(self, msg, key):
return 'verysecure' * self.x + key * self.y
def verify(self, msg, sig, key):
if sig != self.sign(msg, key):
raise SignatureError('nope')
return True
jws.algos.CUSTOM += [
# a regular expression with two named matching groups. (x and y)
# named groups will be sent to the class constructor
(r'^F(?P<x>\d)U(?P<y>\d{2})$', FXUY),
]
And in an interpreter:
>>> import jws
>>> header = { 'alg': 'F7U12' }
>>> payload = { 'claim': 'wutt' }
>>> sig = jws.sign(header, payload, '<trollface>')
Traceback (most recent call last):
....
jws.exceptions.AlgorithmNotImplemented: "F7U12" not implemented.
>>>
>>> import sillycrypto
>>> sig = jws.sign(header, payload, '<trollface>')
>>> jws.verify(header, payload, sig, '<trollface>')
True
>>> jws.verify(header, payload, sig, 'y u no verify?')
Traceback (most recent call last):
....
jws.exceptions.SignatureError: nope
Other Stuff
Check out https://github.com/brianloveswords/python-jws/blob/master/examples/minijwt.py for a 14-line implemention of JWT.
See https://github.com/brianloveswords/python-jws/blob/master/examples/ragecrypto.py for a rage-comic inspired cryptography extension.
TODO
- Write about all the rad stuff that can be done around headers (as extensible as crypto algos)
- Pull in JWK support
Tests
use nosetests
License
MIT
*Note that all licence references and agreements mentioned in the python-jws README section above
are relevant to that project's source code only.