@@ -15,6 +15,8 @@ def str_idx_as_int(string, index):
1515
1616
1717if sys .version_info < (3 , 0 ): # pragma: no branch
18+ import binascii
19+ import platform
1820
1921 def normalise_bytes (buffer_object ):
2022 """Cast the input into array of bytes."""
@@ -24,22 +26,64 @@ def normalise_bytes(buffer_object):
2426 def hmac_compat (ret ):
2527 return ret
2628
27- if sys . version_info < ( 2 , 7 ) or sys . version_info < ( # pragma: no branch
28- 2 ,
29- 7 ,
30- 4 ,
31- ):
29+ if (
30+ sys . version_info < ( 2 , 7 )
31+ or sys . version_info < ( 2 , 7 , 4 )
32+ or platform . system () == "Java"
33+ ): # pragma: no branch
3234
3335 def remove_whitespace (text ):
3436 """Removes all whitespace from passed in string"""
3537 return re .sub (r"\s+" , "" , text )
3638
39+ def compat26_str (val ):
40+ return str (val )
41+
42+ def bit_length (val ):
43+ if val == 0 :
44+ return 0
45+ return len (bin (val )) - 2
46+
3747 else :
3848
3949 def remove_whitespace (text ):
4050 """Removes all whitespace from passed in string"""
4151 return re .sub (r"\s+" , "" , text , flags = re .UNICODE )
4252
53+ def compat26_str (val ):
54+ return val
55+
56+ def bit_length (val ):
57+ """Return number of bits necessary to represent an integer."""
58+ return val .bit_length ()
59+
60+ def b2a_hex (val ):
61+ return binascii .b2a_hex (compat26_str (val ))
62+
63+ def bytes_to_int (val , byteorder ):
64+ """Convert bytes to an int."""
65+ if not val :
66+ return 0
67+ if byteorder == "big" :
68+ return int (b2a_hex (val ), 16 )
69+ if byteorder == "little" :
70+ return int (b2a_hex (val [::- 1 ]), 16 )
71+ raise ValueError ("Only 'big' and 'little' endian supported" )
72+
73+ def int_to_bytes (val , length = None , byteorder = "big" ):
74+ """Return number converted to bytes"""
75+ if length is None :
76+ length = byte_length (val )
77+ if byteorder == "big" :
78+ return bytearray (
79+ (val >> i ) & 0xFF for i in reversed (range (0 , length * 8 , 8 ))
80+ )
81+ if byteorder == "little" :
82+ return bytearray (
83+ (val >> i ) & 0xFF for i in range (0 , length * 8 , 8 )
84+ )
85+ raise ValueError ("Only 'big' or 'little' endian supported" )
86+
4387
4488else :
4589 if sys .version_info < (3 , 4 ): # pragma: no branch
@@ -62,3 +106,28 @@ def normalise_bytes(buffer_object):
62106 def remove_whitespace (text ):
63107 """Removes all whitespace from passed in string"""
64108 return re .sub (r"\s+" , "" , text , flags = re .UNICODE )
109+
110+ # pylint: disable=invalid-name
111+ # pylint is stupid here and deson't notice it's a function, not
112+ # constant
113+ bytes_to_int = int .from_bytes
114+ # pylint: enable=invalid-name
115+
116+ def bit_length (val ):
117+ """Return number of bits necessary to represent an integer."""
118+ return val .bit_length ()
119+
120+ def int_to_bytes (val , length = None , byteorder = "big" ):
121+ """Convert integer to bytes."""
122+ if length is None :
123+ length = byte_length (val )
124+ # for gmpy we need to convert back to native int
125+ if type (val ) != int :
126+ val = int (val )
127+ return bytearray (val .to_bytes (length = length , byteorder = byteorder ))
128+
129+
130+ def byte_length (val ):
131+ """Return number of bytes necessary to represent an integer."""
132+ length = bit_length (val )
133+ return (length + 7 ) // 8
0 commit comments