From 679e8f5b825be0d4f6c5ee77bff639efcaeac873 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 8 Aug 2015 09:59:48 -0500 Subject: refactor integer to bytes as utils.int_to_bytes --- src/cryptography/utils.py | 13 +++++++++++++ src/cryptography/x509.py | 6 +----- 2 files changed, 14 insertions(+), 5 deletions(-) (limited to 'src') 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") -- cgit v1.2.3