aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarithmetic1728 <58957152+arithmetic1728@users.noreply.github.com>2021-11-01 10:22:26 -0700
committerGitHub <noreply@github.com>2021-11-01 10:22:26 -0700
commitbd0ccc5fe77d55f7a19f5278d6b60587c393ee3c (patch)
treeacc18447e4e9c8df9bb8b6c18cdaa4a0d1a7d4cd
parent194c64acbcbed95ffcffe00ec09ca10da1081dd7 (diff)
downloadgoogle-auth-library-python-bd0ccc5fe77d55f7a19f5278d6b60587c393ee3c.tar.gz
fix: use 'int.to_bytes' and 'int.from_bytes' for py3 (#904)
-rw-r--r--google/auth/_helpers.py10
-rw-r--r--google/auth/crypt/es256.py18
2 files changed, 25 insertions, 3 deletions
diff --git a/google/auth/_helpers.py b/google/auth/_helpers.py
index b239fcd..1b08ab8 100644
--- a/google/auth/_helpers.py
+++ b/google/auth/_helpers.py
@@ -17,6 +17,7 @@
import base64
import calendar
import datetime
+import sys
import six
from six.moves import urllib
@@ -233,3 +234,12 @@ def unpadded_urlsafe_b64encode(value):
Union[str|bytes]: The encoded value
"""
return base64.urlsafe_b64encode(value).rstrip(b"=")
+
+
+def is_python_3():
+ """Check if the Python interpreter is Python 2 or 3.
+
+ Returns:
+ bool: True if the Python interpreter is Python 3 and False otherwise.
+ """
+ return sys.version_info > (3, 0)
diff --git a/google/auth/crypt/es256.py b/google/auth/crypt/es256.py
index c6d6176..42823a7 100644
--- a/google/auth/crypt/es256.py
+++ b/google/auth/crypt/es256.py
@@ -53,8 +53,16 @@ class ES256Verifier(base.Verifier):
sig_bytes = _helpers.to_bytes(signature)
if len(sig_bytes) != 64:
return False
- r = utils.int_from_bytes(sig_bytes[:32], byteorder="big")
- s = utils.int_from_bytes(sig_bytes[32:], byteorder="big")
+ r = (
+ int.from_bytes(sig_bytes[:32], byteorder="big")
+ if _helpers.is_python_3()
+ else utils.int_from_bytes(sig_bytes[:32], byteorder="big")
+ )
+ s = (
+ int.from_bytes(sig_bytes[32:], byteorder="big")
+ if _helpers.is_python_3()
+ else utils.int_from_bytes(sig_bytes[32:], byteorder="big")
+ )
asn1_sig = encode_dss_signature(r, s)
message = _helpers.to_bytes(message)
@@ -121,7 +129,11 @@ class ES256Signer(base.Signer, base.FromServiceAccountMixin):
# Convert ASN1 encoded signature to (r||s) raw signature.
(r, s) = decode_dss_signature(asn1_signature)
- return utils.int_to_bytes(r, 32) + utils.int_to_bytes(s, 32)
+ return (
+ (r.to_bytes(32, byteorder="big") + s.to_bytes(32, byteorder="big"))
+ if _helpers.is_python_3()
+ else (utils.int_to_bytes(r, 32) + utils.int_to_bytes(s, 32))
+ )
@classmethod
def from_string(cls, key, key_id=None):