Skip to content

Commit

Permalink
fix: use 'int.to_bytes' and 'int.from_bytes' for py3 (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
arithmetic1728 committed Nov 1, 2021
1 parent 194c64a commit bd0ccc5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 10 additions & 0 deletions google/auth/_helpers.py
Expand Up @@ -17,6 +17,7 @@
import base64
import calendar
import datetime
import sys

import six
from six.moves import urllib
Expand Down Expand Up @@ -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)
18 changes: 15 additions & 3 deletions google/auth/crypt/es256.py
Expand Up @@ -53,8 +53,16 @@ def verify(self, message, signature):
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)
Expand Down Expand Up @@ -121,7 +129,11 @@ def sign(self, message):

# 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):
Expand Down

0 comments on commit bd0ccc5

Please sign in to comment.