Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/ecdsa/_compat.py: 21%

87 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:16 +0000

1""" 

2Common functions for providing cross-python version compatibility. 

3""" 

4import sys 

5import re 

6import binascii 

7from six import integer_types 

8 

9 

10def str_idx_as_int(string, index): 

11 """Take index'th byte from string, return as integer""" 

12 val = string[index] 

13 if isinstance(val, integer_types): 

14 return val 

15 return ord(val) 

16 

17 

18if sys.version_info < (3, 0): # pragma: no branch 

19 import platform 

20 

21 def normalise_bytes(buffer_object): 

22 """Cast the input into array of bytes.""" 

23 # flake8 runs on py3 where `buffer` indeed doesn't exist... 

24 return buffer(buffer_object) # noqa: F821 

25 

26 def hmac_compat(ret): 

27 return ret 

28 

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 

34 

35 def remove_whitespace(text): 

36 """Removes all whitespace from passed in string""" 

37 return re.sub(r"\s+", "", text) 

38 

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 

47 else: 

48 

49 def remove_whitespace(text): 

50 """Removes all whitespace from passed in string""" 

51 return re.sub(r"\s+", "", text, flags=re.UNICODE) 

52 

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 a2b_hex(val): 

64 try: 

65 return bytearray(binascii.a2b_hex(val)) 

66 except Exception as e: 

67 raise ValueError("base16 error: %s" % e) 

68 

69 def bytes_to_int(val, byteorder): 

70 """Convert bytes to an int.""" 

71 if not val: 

72 return 0 

73 if byteorder == "big": 

74 return int(b2a_hex(val), 16) 

75 if byteorder == "little": 

76 return int(b2a_hex(val[::-1]), 16) 

77 raise ValueError("Only 'big' and 'little' endian supported") 

78 

79 def int_to_bytes(val, length=None, byteorder="big"): 

80 """Return number converted to bytes""" 

81 if length is None: 

82 length = byte_length(val) 

83 if byteorder == "big": 

84 return bytearray( 

85 (val >> i) & 0xFF for i in reversed(range(0, length * 8, 8)) 

86 ) 

87 if byteorder == "little": 

88 return bytearray( 

89 (val >> i) & 0xFF for i in range(0, length * 8, 8) 

90 ) 

91 raise ValueError("Only 'big' or 'little' endian supported") 

92 

93else: 

94 if sys.version_info < (3, 4): # pragma: no branch 

95 # on python 3.3 hmac.hmac.update() accepts only bytes, on newer 

96 # versions it does accept memoryview() also 

97 def hmac_compat(data): 

98 if not isinstance(data, bytes): # pragma: no branch 

99 return bytes(data) 

100 return data 

101 

102 def normalise_bytes(buffer_object): 

103 """Cast the input into array of bytes.""" 

104 if not buffer_object: 

105 return b"" 

106 return memoryview(buffer_object).cast("B") 

107 

108 else: 

109 

110 def hmac_compat(data): 

111 return data 

112 

113 def normalise_bytes(buffer_object): 

114 """Cast the input into array of bytes.""" 

115 return memoryview(buffer_object).cast("B") 

116 

117 def compat26_str(val): 

118 return val 

119 

120 def remove_whitespace(text): 

121 """Removes all whitespace from passed in string""" 

122 return re.sub(r"\s+", "", text, flags=re.UNICODE) 

123 

124 def a2b_hex(val): 

125 try: 

126 return bytearray(binascii.a2b_hex(bytearray(val, "ascii"))) 

127 except Exception as e: 

128 raise ValueError("base16 error: %s" % e) 

129 

130 # pylint: disable=invalid-name 

131 # pylint is stupid here and doesn't notice it's a function, not 

132 # constant 

133 bytes_to_int = int.from_bytes 

134 # pylint: enable=invalid-name 

135 

136 def bit_length(val): 

137 """Return number of bits necessary to represent an integer.""" 

138 return val.bit_length() 

139 

140 def int_to_bytes(val, length=None, byteorder="big"): 

141 """Convert integer to bytes.""" 

142 if length is None: 

143 length = byte_length(val) 

144 # for gmpy we need to convert back to native int 

145 if type(val) != int: 

146 val = int(val) 

147 return bytearray(val.to_bytes(length=length, byteorder=byteorder)) 

148 

149 

150def byte_length(val): 

151 """Return number of bytes necessary to represent an integer.""" 

152 length = bit_length(val) 

153 return (length + 7) // 8