Skip to content

Commit d8e684e

Browse files
authored
Merge pull request #82 from JohnDoee/bugfix/siglength
fixed invalid JWT signatures generated
2 parents e50e3f8 + 7e9535b commit d8e684e

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

python/py_vapid/jwt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ def sign(claims, key):
8383
token = "{}.{}".format(header, claims)
8484
rsig = key.sign(token.encode('utf8'), ec.ECDSA(hashes.SHA256()))
8585
(r, s) = utils.decode_dss_signature(rsig)
86-
sig = b64urlencode(num_to_bytes(r) + num_to_bytes(s))
86+
sig = b64urlencode(num_to_bytes(r, 32) + num_to_bytes(s, 32))
8787
return "{}.{}".format(token, sig)

python/py_vapid/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ def b64urlencode(data):
2626
return base64.urlsafe_b64encode(data).replace(b'=', b'').decode('utf8')
2727

2828

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

0 commit comments

Comments
 (0)