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

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

79 statements  

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 

95 def hmac_compat(data): 

96 return data 

97 

98 def normalise_bytes(buffer_object): 

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

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

101 

102 def compat26_str(val): 

103 return val 

104 

105 def remove_whitespace(text): 

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

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

108 

109 def a2b_hex(val): 

110 try: 

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

112 except Exception as e: 

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

114 

115 # pylint: disable=invalid-name 

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

117 # constant 

118 bytes_to_int = int.from_bytes 

119 # pylint: enable=invalid-name 

120 

121 def bit_length(val): 

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

123 return val.bit_length() 

124 

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

126 """Convert integer to bytes.""" 

127 if length is None: 

128 length = byte_length(val) 

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

130 if not isinstance(val, int): 

131 val = int(val) 

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

133 

134 

135def byte_length(val): 

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

137 length = bit_length(val) 

138 return (length + 7) // 8