Added optimization for Python 3 to use int.from_bytes instead of Python code
diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py
index 455c8a9..8a4c8bd 100644
--- a/src/cryptography/hazmat/primitives/serialization.py
+++ b/src/cryptography/hazmat/primitives/serialization.py
@@ -6,6 +6,7 @@
import base64
import struct
+import sys
import warnings
from cryptography import utils
@@ -88,10 +89,15 @@
def _read_next_mpint(data):
+ """Reads the next mpint from the data. Currently, all mpints are
+ interpreted as unsigned."""
mpint_data, rest = _read_next_string(data)
+ if sys.version_info >= (3, 2):
+ # If we're using >= 3.2, use int.from_bytes for identical results.
+ return int.from_bytes(mpint_data, byteorder='big', signed=False), rest
+
if len(mpint_data) % 4 != 0:
- # Pad the bytes with 0x00 to a block size of 4
mpint_data = (b'\x00' * (4 - (len(mpint_data) % 4))) + mpint_data
result = 0