summaryrefslogtreecommitdiff
path: root/src/cryptography/utils.py
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2015-06-27 22:28:53 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2015-06-27 22:28:53 -0500
commitbb81b34c675e0bbf2b768ca408c7aeb0fa90a7da (patch)
tree976dfd3581b073b425396dda40e312d626107f47 /src/cryptography/utils.py
parent6e355b5dede37e9e344a1fbb210a4838ead6a3ec (diff)
downloadcryptography-bb81b34c675e0bbf2b768ca408c7aeb0fa90a7da.tar.gz
move int_from_bytes so we can use it elsewhere
Diffstat (limited to 'src/cryptography/utils.py')
-rw-r--r--src/cryptography/utils.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py
index 0bf8c0ea1..24afe612d 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