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,7 +89,24 @@ 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
8395 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 ))
84110
85111else :
86112 # Python 2.6 requires strings instead of bytearrays in a couple places,
@@ -94,13 +120,23 @@ def compat26Str(x): return str(x)
94120 def remove_whitespace (text ):
95121 """Removes all whitespace from passed in string"""
96122 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
97129 else :
98130 def compat26Str (x ): return x
99131
100132 def remove_whitespace (text ):
101133 """Removes all whitespace from passed in string"""
102134 return re .sub (r"\s+" , "" , text , flags = re .UNICODE )
103135
136+ def bit_length (val ):
137+ """Return number of bits necessary to represent an integer."""
138+ return val .bit_length ()
139+
104140 def compatAscii2Bytes (val ):
105141 """Convert ASCII string to bytes."""
106142 return val
@@ -159,6 +195,25 @@ def bytes_to_int(val, byteorder):
159195 return int (b2a_hex (val [::- 1 ]), 16 )
160196 raise ValueError ("Only 'big' and 'little' endian supported" )
161197
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+
162217try :
163218 # Fedora and Red Hat Enterprise Linux versions have small curves removed
164219 getattr (ecdsa , 'NIST192p' )
0 commit comments