Skip to content

Commit

Permalink
Merge pull request #82 from JohnDoee/bugfix/siglength
Browse files Browse the repository at this point in the history
fixed invalid JWT signatures generated
  • Loading branch information
jrconlin authored May 26, 2020
2 parents e50e3f8 + 7e9535b commit d8e684e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/py_vapid/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ def sign(claims, key):
token = "{}.{}".format(header, claims)
rsig = key.sign(token.encode('utf8'), ec.ECDSA(hashes.SHA256()))
(r, s) = utils.decode_dss_signature(rsig)
sig = b64urlencode(num_to_bytes(r) + num_to_bytes(s))
sig = b64urlencode(num_to_bytes(r, 32) + num_to_bytes(s, 32))
return "{}.{}".format(token, sig)
7 changes: 5 additions & 2 deletions python/py_vapid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ def b64urlencode(data):
return base64.urlsafe_b64encode(data).replace(b'=', b'').decode('utf8')


def num_to_bytes(n):
def num_to_bytes(n, pad_to):
"""Returns the byte representation of an integer, in big-endian order.
:param n: The integer to encode.
:type n: int
:param pad_to: Expected length of result, zeropad if necessary.
:type pad_to: int
:returns bytes
"""
h = '%x' % n
return binascii.unhexlify('0' * (len(h) % 2) + h)
r = binascii.unhexlify('0' * (len(h) % 2) + h)
return b'\x00' * (pad_to - len(r)) + r

0 comments on commit d8e684e

Please sign in to comment.