aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2015-08-08 09:59:48 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2015-08-08 10:22:35 -0500
commit679e8f5b825be0d4f6c5ee77bff639efcaeac873 (patch)
tree798c30076e2ed48bbefae377c05475f120632106 /src
parent73cd392198ed800dcd20f67d838f8a451e2bdf8c (diff)
downloadcryptography-679e8f5b825be0d4f6c5ee77bff639efcaeac873.tar.gz
cryptography-679e8f5b825be0d4f6c5ee77bff639efcaeac873.tar.bz2
cryptography-679e8f5b825be0d4f6c5ee77bff639efcaeac873.zip
refactor integer to bytes as utils.int_to_bytes
Diffstat (limited to 'src')
-rw-r--r--src/cryptography/utils.py13
-rw-r--r--src/cryptography/x509.py6
2 files changed, 14 insertions, 5 deletions
diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py
index 24afe612..4ca4a7a2 100644
--- a/src/cryptography/utils.py
+++ b/src/cryptography/utils.py
@@ -5,6 +5,7 @@
from __future__ import absolute_import, division, print_function
import abc
+import binascii
import inspect
import struct
import sys
@@ -46,6 +47,18 @@ else:
return result
+if hasattr(int, "to_bytes"):
+ int_to_bytes = int.to_bytes
+else:
+ def int_to_bytes(integer, byteorder, signed=False):
+ assert byteorder == 'big'
+ assert not signed
+
+ hex_string = '%x' % integer
+ n = len(hex_string)
+ return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
+
+
class InterfaceNotImplemented(Exception):
pass
diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py
index f54eccf4..3fff218a 100644
--- a/src/cryptography/x509.py
+++ b/src/cryptography/x509.py
@@ -5,7 +5,6 @@
from __future__ import absolute_import, division, print_function
import abc
-import binascii
import datetime
import hashlib
import ipaddress
@@ -699,10 +698,7 @@ class SubjectKeyIdentifier(object):
for bit in spki.getComponentByName("subjectPublicKey"):
bits = bits << 1 | bit
- # convert the integer to bytes
- hex_string = '%x' % bits
- n = len(hex_string)
- data = binascii.unhexlify(hex_string.zfill(n + (n & 1)))
+ data = utils.int_to_bytes(bits, "big")
return cls(hashlib.sha1(data).digest())
digest = utils.read_only_property("_digest")