Skip to content

Commit bf39b44

Browse files
authored
Replace int_from_bytes() with builtin int.from_bytes() (jpadilla#549)
Follows upstream cryptography commit: pyca/cryptography@5528a31 Since Python 3.2, this bytes to an int is a native feature.
1 parent 0683b79 commit bf39b44

File tree

3 files changed

+4
-29
lines changed

3 files changed

+4
-29
lines changed

jwt/algorithms.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
load_pem_public_key,
4242
load_ssh_public_key,
4343
)
44-
from cryptography.utils import int_from_bytes
4544

4645
has_crypto = True
4746
except ImportError:
@@ -466,8 +465,8 @@ def from_jwk(jwk):
466465
raise InvalidKeyError(f"Invalid curve: {curve}")
467466

468467
public_numbers = ec.EllipticCurvePublicNumbers(
469-
x=int_from_bytes(x, "big"),
470-
y=int_from_bytes(y, "big"),
468+
x=int.from_bytes(x, byteorder="big"),
469+
y=int.from_bytes(y, byteorder="big"),
471470
curve=curve_obj,
472471
)
473472

@@ -481,7 +480,7 @@ def from_jwk(jwk):
481480
)
482481

483482
return ec.EllipticCurvePrivateNumbers(
484-
int_from_bytes(d, "big"), public_numbers
483+
int.from_bytes(d, byteorder="big"), public_numbers
485484
).private_key()
486485

487486
class RSAPSSAlgorithm(RSAAlgorithm):

tests/keys/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
import os
33

44
from jwt.utils import base64url_decode
5-
from tests.utils import int_from_bytes
65

76
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
87

98

109
def decode_value(val):
1110
decoded = base64url_decode(val)
12-
return int_from_bytes(decoded, "big")
11+
return int.from_bytes(decoded, byteorder="big")
1312

1413

1514
def load_hmac_key():

tests/utils.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import struct
32
from calendar import timegm
43
from datetime import datetime
54

@@ -12,25 +11,3 @@ def key_path(key_name):
1211
return os.path.join(
1312
os.path.dirname(os.path.realpath(__file__)), "keys", key_name
1413
)
15-
16-
17-
# Borrowed from `cryptography`
18-
if hasattr(int, "from_bytes"):
19-
int_from_bytes = int.from_bytes
20-
else:
21-
22-
def int_from_bytes(data, byteorder, signed=False):
23-
assert byteorder == "big"
24-
assert not signed
25-
26-
if len(data) % 4 != 0:
27-
data = (b"\x00" * (4 - (len(data) % 4))) + data
28-
29-
result = 0
30-
31-
while len(data) > 0:
32-
(digit,) = struct.unpack(">I", data[:4])
33-
result = (result << 32) + digit
34-
data = data[4:]
35-
36-
return result

0 commit comments

Comments
 (0)