Skip to content

Commit 26d410f

Browse files
brentrubrentru
authored andcommitted
update transform.py and lint
1 parent f599178 commit 26d410f

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

adafruit_rsa/transform.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
1919
From bytes to a number, number to bytes, etc.
2020
"""
21+
22+
# from __future__ import absolute_import
23+
2124
from struct import pack
22-
from adafruit_binascii import hexlify
25+
import adafruit_binascii as binascii
2326

2427
from adafruit_rsa._compat import byte, is_integer
2528
from adafruit_rsa import common, machine_size
@@ -37,7 +40,7 @@ def bytes2int(raw_bytes):
3740
3841
"""
3942

40-
return int(hexlify(raw_bytes), 16)
43+
return int(binascii.hexlify(raw_bytes), 16)
4144

4245

4346
def _int2bytes(number, block_size=None):
@@ -133,14 +136,11 @@ def bytes_leading(raw_bytes, needle=b'\x00'):
133136
def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
134137
"""
135138
Convert an unsigned integer to bytes (base-256 representation)::
136-
137139
Does not preserve leading zeros if you don't specify a chunk size or
138140
fill size.
139-
140141
.. NOTE:
141142
You must not specify both fill_size and chunk_size. Only one
142143
of them is allowed.
143-
144144
:param number:
145145
Integer value
146146
:param fill_size:
@@ -172,7 +172,7 @@ def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
172172
raise ValueError("You can either fill or pad chunks, but not both")
173173

174174
# Ensure these are integers.
175-
is_integer(number)
175+
assert number & 1 == 0, "Number must be an unsigned integer, not a float."
176176

177177
raw_bytes = b''
178178

@@ -197,9 +197,10 @@ def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
197197
"Need %d bytes for number, but fill size is %d" %
198198
(length, fill_size)
199199
)
200-
raw_bytes = b'\x00' + raw_bytes
200+
raw_bytes = "% {}s".format(fill_size).encode() % raw_bytes
201201
elif chunk_size and chunk_size > 0:
202202
remainder = length % chunk_size
203203
if remainder:
204-
raw_bytes = b'\x00' + raw_bytes
204+
padding_size = chunk_size - remainder
205+
raw_bytes = "% {}s".format(length + padding_size).encode() % raw_bytes
205206
return raw_bytes

0 commit comments

Comments
 (0)