1616if sys .version_info >= (3 ,0 ):
1717
1818 def compat26Str (x ): return x
19-
20- # Python 3 requires bytes instead of bytearrays for HMAC
21-
19+
20+ # Python 3.3 requires bytes instead of bytearrays for HMAC
2221 # So, python 2.6 requires strings, python 3 requires 'bytes',
23- # and python 2.7 can handle bytearrays...
24- def compatHMAC (x ): return bytes (x )
22+ # and python 2.7 and 3.5 can handle bytearrays...
23+ # pylint: disable=invalid-name
24+ # we need to keep compatHMAC and `x` for API compatibility
25+ if sys .version_info < (3 , 4 ):
26+ def compatHMAC (x ):
27+ """Convert bytes-like input to format acceptable for HMAC."""
28+ return bytes (x )
29+ else :
30+ def compatHMAC (x ):
31+ """Convert bytes-like input to format acceptable for HMAC."""
32+ return x
33+ # pylint: enable=invalid-name
2534
2635 def compatAscii2Bytes (val ):
2736 """Convert ASCII string to bytes."""
@@ -80,6 +89,25 @@ def remove_whitespace(text):
8089 """Removes all whitespace from passed in string"""
8190 return re .sub (r"\s+" , "" , text , flags = re .UNICODE )
8291
92+ # pylint: disable=invalid-name
93+ # pylint is stupid here and deson't notice it's a function, not
94+ # constant
95+ bytes_to_int = int .from_bytes
96+ # pylint: enable=invalid-name
97+
98+ def bit_length (val ):
99+ """Return number of bits necessary to represent an integer."""
100+ return val .bit_length ()
101+
102+ def int_to_bytes (val , length = None , byteorder = "big" ):
103+ """Return number converted to bytes"""
104+ if length is None :
105+ length = byte_length (val )
106+ # for gmpy we need to convert back to native int
107+ if type (val ) != int :
108+ val = int (val )
109+ return bytearray (val .to_bytes (length = length , byteorder = byteorder ))
110+
83111else :
84112 # Python 2.6 requires strings instead of bytearrays in a couple places,
85113 # so we define this function so it does the conversion if needed.
@@ -92,13 +120,23 @@ def compat26Str(x): return str(x)
92120 def remove_whitespace (text ):
93121 """Removes all whitespace from passed in string"""
94122 return re .sub (r"\s+" , "" , text )
123+
124+ def bit_length (val ):
125+ """Return number of bits necessary to represent an integer."""
126+ if val == 0 :
127+ return 0
128+ return len (bin (val ))- 2
95129 else :
96130 def compat26Str (x ): return x
97131
98132 def remove_whitespace (text ):
99133 """Removes all whitespace from passed in string"""
100134 return re .sub (r"\s+" , "" , text , flags = re .UNICODE )
101135
136+ def bit_length (val ):
137+ """Return number of bits necessary to represent an integer."""
138+ return val .bit_length ()
139+
102140 def compatAscii2Bytes (val ):
103141 """Convert ASCII string to bytes."""
104142 return val
@@ -147,6 +185,35 @@ def time_stamp():
147185 """Returns system time as a float"""
148186 return time .clock ()
149187
188+ def bytes_to_int (val , byteorder ):
189+ """Convert bytes to an int."""
190+ if not val :
191+ return 0
192+ if byteorder == "big" :
193+ return int (b2a_hex (val ), 16 )
194+ if byteorder == "little" :
195+ return int (b2a_hex (val [::- 1 ]), 16 )
196+ raise ValueError ("Only 'big' and 'little' endian supported" )
197+
198+ def int_to_bytes (val , length = None , byteorder = "big" ):
199+ """Return number converted to bytes"""
200+ if length is None :
201+ length = byte_length (val )
202+ if byteorder == "big" :
203+ return bytearray ((val >> i ) & 0xff
204+ for i in reversed (range (0 , length * 8 , 8 )))
205+ if byteorder == "little" :
206+ return bytearray ((val >> i ) & 0xff
207+ for i in range (0 , length * 8 , 8 ))
208+ raise ValueError ("Only 'big' or 'little' endian supported" )
209+
210+
211+ def byte_length (val ):
212+ """Return number of bytes necessary to represent an integer."""
213+ length = bit_length (val )
214+ return (length + 7 ) // 8
215+
216+
150217try :
151218 # Fedora and Red Hat Enterprise Linux versions have small curves removed
152219 getattr (ecdsa , 'NIST192p' )
0 commit comments