aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOfek Lev <ofekmeister@gmail.com>2017-04-05 19:48:04 -0400
committerPaul Kehrer <paul.l.kehrer@gmail.com>2017-04-06 07:48:04 +0800
commiteba67506bb813f900c102fe62bb9bc01acfb53d6 (patch)
tree154d6e11eb3e826475e96305316068e13c623ff4 /src
parent854e5fac6ebda05a218b661887ba0ed371d8c269 (diff)
downloadcryptography-eba67506bb813f900c102fe62bb9bc01acfb53d6.tar.gz
cryptography-eba67506bb813f900c102fe62bb9bc01acfb53d6.tar.bz2
cryptography-eba67506bb813f900c102fe62bb9bc01acfb53d6.zip
minor int_to_bytes performance increase (#3490)
* minor int_to_bytes performance increase * why is Python 2.6 supported anyway... * keep py2k version
Diffstat (limited to 'src')
-rw-r--r--src/cryptography/utils.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py
index ab3c84e1..6d304856 100644
--- a/src/cryptography/utils.py
+++ b/src/cryptography/utils.py
@@ -51,13 +51,19 @@ else:
return int(bytes(data).encode('hex'), 16)
-def int_to_bytes(integer, length=None):
- hex_string = '%x' % integer
- if length is None:
- n = len(hex_string)
- else:
- n = length * 2
- return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
+if hasattr(int, "to_bytes"):
+ def int_to_bytes(integer, length=None):
+ return integer.to_bytes(
+ length or (integer.bit_length() + 7) // 8 or 1, 'big'
+ )
+else:
+ def int_to_bytes(integer, length=None):
+ hex_string = '%x' % integer
+ if length is None:
+ n = len(hex_string)
+ else:
+ n = length * 2
+ return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
class InterfaceNotImplemented(Exception):