aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2015-06-28 00:20:32 -0400
committerAlex Gaynor <alex.gaynor@gmail.com>2015-06-28 00:20:32 -0400
commitab78ba262b7bee75cfc1fc22922e1b6d57b3614e (patch)
tree72be2dd18a517886197982773c57c98de77f857f
parent5432f818890a67a8c6c53ebdea47fd9dd7ec1ae9 (diff)
parentbb81b34c675e0bbf2b768ca408c7aeb0fa90a7da (diff)
downloadcryptography-ab78ba262b7bee75cfc1fc22922e1b6d57b3614e.tar.gz
cryptography-ab78ba262b7bee75cfc1fc22922e1b6d57b3614e.tar.bz2
cryptography-ab78ba262b7bee75cfc1fc22922e1b6d57b3614e.zip
Merge pull request #2081 from reaperhulk/move-int-from-bytes
move int_from_bytes so we can use it elsewhere
-rw-r--r--src/cryptography/hazmat/primitives/serialization.py32
-rw-r--r--src/cryptography/utils.py21
2 files changed, 30 insertions, 23 deletions
diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py
index 9fbc32b1..098b31dc 100644
--- a/src/cryptography/hazmat/primitives/serialization.py
+++ b/src/cryptography/hazmat/primitives/serialization.py
@@ -122,8 +122,12 @@ def _load_ssh_ecdsa_public_key(expected_key_type, decoded_data, backend):
if len(data) != 1 + 2 * ((curve.key_size + 7) // 8):
raise ValueError("Malformed key bytes")
- x = _int_from_bytes(data[1:1 + (curve.key_size + 7) // 8], byteorder='big')
- y = _int_from_bytes(data[1 + (curve.key_size + 7) // 8:], byteorder='big')
+ x = utils.int_from_bytes(
+ data[1:1 + (curve.key_size + 7) // 8], byteorder='big'
+ )
+ y = utils.int_from_bytes(
+ data[1 + (curve.key_size + 7) // 8:], byteorder='big'
+ )
return ec.EllipticCurvePublicNumbers(x, y, curve).public_key(backend)
@@ -145,27 +149,9 @@ def _read_next_mpint(data):
"""
mpint_data, rest = _read_next_string(data)
- return _int_from_bytes(mpint_data, byteorder='big', signed=False), rest
-
-
-if hasattr(int, "from_bytes"):
- _int_from_bytes = int.from_bytes
-else:
- def _int_from_bytes(data, byteorder, signed=False):
- assert byteorder == 'big'
- assert not signed
-
- if len(data) % 4 != 0:
- data = (b'\x00' * (4 - (len(data) % 4))) + data
-
- result = 0
-
- while len(data) > 0:
- digit, = struct.unpack('>I', data[:4])
- result = (result << 32) + digit
- data = data[4:]
-
- return result
+ return (
+ utils.int_from_bytes(mpint_data, byteorder='big', signed=False), rest
+ )
class Encoding(Enum):
diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py
index 0bf8c0ea..24afe612 100644
--- a/src/cryptography/utils.py
+++ b/src/cryptography/utils.py
@@ -6,6 +6,7 @@ from __future__ import absolute_import, division, print_function
import abc
import inspect
+import struct
import sys
import warnings
@@ -25,6 +26,26 @@ def register_interface(iface):
return register_decorator
+if hasattr(int, "from_bytes"):
+ int_from_bytes = int.from_bytes
+else:
+ def int_from_bytes(data, byteorder, signed=False):
+ assert byteorder == 'big'
+ assert not signed
+
+ if len(data) % 4 != 0:
+ data = (b'\x00' * (4 - (len(data) % 4))) + data
+
+ result = 0
+
+ while len(data) > 0:
+ digit, = struct.unpack('>I', data[:4])
+ result = (result << 32) + digit
+ data = data[4:]
+
+ return result
+
+
class InterfaceNotImplemented(Exception):
pass