Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

139 wiersze
4.0 KiB

  1. """
  2. Common functions for providing cross-python version compatibility.
  3. """
  4. import sys
  5. import re
  6. import binascii
  7. from six import integer_types
  8. def str_idx_as_int(string, index):
  9. """Take index'th byte from string, return as integer"""
  10. val = string[index]
  11. if isinstance(val, integer_types):
  12. return val
  13. return ord(val)
  14. if sys.version_info < (3, 0): # pragma: no branch
  15. import platform
  16. def normalise_bytes(buffer_object):
  17. """Cast the input into array of bytes."""
  18. # flake8 runs on py3 where `buffer` indeed doesn't exist...
  19. return buffer(buffer_object) # noqa: F821
  20. def hmac_compat(ret):
  21. return ret
  22. if (
  23. sys.version_info < (2, 7)
  24. or sys.version_info < (2, 7, 4)
  25. or platform.system() == "Java"
  26. ): # pragma: no branch
  27. def remove_whitespace(text):
  28. """Removes all whitespace from passed in string"""
  29. return re.sub(r"\s+", "", text)
  30. def compat26_str(val):
  31. return str(val)
  32. def bit_length(val):
  33. if val == 0:
  34. return 0
  35. return len(bin(val)) - 2
  36. else:
  37. def remove_whitespace(text):
  38. """Removes all whitespace from passed in string"""
  39. return re.sub(r"\s+", "", text, flags=re.UNICODE)
  40. def compat26_str(val):
  41. return val
  42. def bit_length(val):
  43. """Return number of bits necessary to represent an integer."""
  44. return val.bit_length()
  45. def b2a_hex(val):
  46. return binascii.b2a_hex(compat26_str(val))
  47. def a2b_hex(val):
  48. try:
  49. return bytearray(binascii.a2b_hex(val))
  50. except Exception as e:
  51. raise ValueError("base16 error: %s" % e)
  52. def bytes_to_int(val, byteorder):
  53. """Convert bytes to an int."""
  54. if not val:
  55. return 0
  56. if byteorder == "big":
  57. return int(b2a_hex(val), 16)
  58. if byteorder == "little":
  59. return int(b2a_hex(val[::-1]), 16)
  60. raise ValueError("Only 'big' and 'little' endian supported")
  61. def int_to_bytes(val, length=None, byteorder="big"):
  62. """Return number converted to bytes"""
  63. if length is None:
  64. length = byte_length(val)
  65. if byteorder == "big":
  66. return bytearray(
  67. (val >> i) & 0xFF for i in reversed(range(0, length * 8, 8))
  68. )
  69. if byteorder == "little":
  70. return bytearray(
  71. (val >> i) & 0xFF for i in range(0, length * 8, 8)
  72. )
  73. raise ValueError("Only 'big' or 'little' endian supported")
  74. else:
  75. def hmac_compat(data):
  76. return data
  77. def normalise_bytes(buffer_object):
  78. """Cast the input into array of bytes."""
  79. return memoryview(buffer_object).cast("B")
  80. def compat26_str(val):
  81. return val
  82. def remove_whitespace(text):
  83. """Removes all whitespace from passed in string"""
  84. return re.sub(r"\s+", "", text, flags=re.UNICODE)
  85. def a2b_hex(val):
  86. try:
  87. return bytearray(binascii.a2b_hex(bytearray(val, "ascii")))
  88. except Exception as e:
  89. raise ValueError("base16 error: %s" % e)
  90. # pylint: disable=invalid-name
  91. # pylint is stupid here and doesn't notice it's a function, not
  92. # constant
  93. bytes_to_int = int.from_bytes
  94. # pylint: enable=invalid-name
  95. def bit_length(val):
  96. """Return number of bits necessary to represent an integer."""
  97. return val.bit_length()
  98. def int_to_bytes(val, length=None, byteorder="big"):
  99. """Convert integer to bytes."""
  100. if length is None:
  101. length = byte_length(val)
  102. # for gmpy we need to convert back to native int
  103. if not isinstance(val, int):
  104. val = int(val)
  105. return bytearray(val.to_bytes(length=length, byteorder=byteorder))
  106. def byte_length(val):
  107. """Return number of bytes necessary to represent an integer."""
  108. length = bit_length(val)
  109. return (length + 7) // 8