Skip to content

Commit bd0ccc5

Browse files
fix: use 'int.to_bytes' and 'int.from_bytes' for py3 (#904)
1 parent 194c64a commit bd0ccc5

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

google/auth/_helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import base64
1818
import calendar
1919
import datetime
20+
import sys
2021

2122
import six
2223
from six.moves import urllib
@@ -233,3 +234,12 @@ def unpadded_urlsafe_b64encode(value):
233234
Union[str|bytes]: The encoded value
234235
"""
235236
return base64.urlsafe_b64encode(value).rstrip(b"=")
237+
238+
239+
def is_python_3():
240+
"""Check if the Python interpreter is Python 2 or 3.
241+
242+
Returns:
243+
bool: True if the Python interpreter is Python 3 and False otherwise.
244+
"""
245+
return sys.version_info > (3, 0)

google/auth/crypt/es256.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@ def verify(self, message, signature):
5353
sig_bytes = _helpers.to_bytes(signature)
5454
if len(sig_bytes) != 64:
5555
return False
56-
r = utils.int_from_bytes(sig_bytes[:32], byteorder="big")
57-
s = utils.int_from_bytes(sig_bytes[32:], byteorder="big")
56+
r = (
57+
int.from_bytes(sig_bytes[:32], byteorder="big")
58+
if _helpers.is_python_3()
59+
else utils.int_from_bytes(sig_bytes[:32], byteorder="big")
60+
)
61+
s = (
62+
int.from_bytes(sig_bytes[32:], byteorder="big")
63+
if _helpers.is_python_3()
64+
else utils.int_from_bytes(sig_bytes[32:], byteorder="big")
65+
)
5866
asn1_sig = encode_dss_signature(r, s)
5967

6068
message = _helpers.to_bytes(message)
@@ -121,7 +129,11 @@ def sign(self, message):
121129

122130
# Convert ASN1 encoded signature to (r||s) raw signature.
123131
(r, s) = decode_dss_signature(asn1_signature)
124-
return utils.int_to_bytes(r, 32) + utils.int_to_bytes(s, 32)
132+
return (
133+
(r.to_bytes(32, byteorder="big") + s.to_bytes(32, byteorder="big"))
134+
if _helpers.is_python_3()
135+
else (utils.int_to_bytes(r, 32) + utils.int_to_bytes(s, 32))
136+
)
125137

126138
@classmethod
127139
def from_string(cls, key, key_id=None):

0 commit comments

Comments
 (0)