From cbddc9897b579f1b44e0c4cd5cd868fa7c6dd06d Mon Sep 17 00:00:00 2001 From: Mark Adams Date: Sun, 14 Dec 2014 22:31:29 -0600 Subject: Added optimization for Python 3 to use int.from_bytes instead of Python code --- src/cryptography/hazmat/primitives/serialization.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py index 455c8a91..8a4c8bd8 100644 --- a/src/cryptography/hazmat/primitives/serialization.py +++ b/src/cryptography/hazmat/primitives/serialization.py @@ -6,6 +6,7 @@ from __future__ import absolute_import, division, print_function import base64 import struct +import sys import warnings from cryptography import utils @@ -88,10 +89,15 @@ def _read_next_string(data): 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 -- cgit v1.2.3