|
@@ -0,0 +1,110 @@
|
|
|
+def normalize_hash(_hash: int) -> int:
|
|
|
+ #hex = hash_to_hex(_hash)
|
|
|
+ #return int(hex, 16)
|
|
|
+ #_bytes = _hash.to_bytes(8, byteorder='big', signed=True)
|
|
|
+ #return bytes_to_hash(_bytes)
|
|
|
+ return _hash & 0xffff
|
|
|
+
|
|
|
+def normalize_bytes(_bytes: bytes) -> bytes:
|
|
|
+ return (b'\x00\x00' + _bytes)[-2:]
|
|
|
+
|
|
|
+def normalize_hex(hex: str) -> str:
|
|
|
+ #_bytes = hex_to_bytes(hex)
|
|
|
+ #return _bytes.hex()
|
|
|
+ return hex.zfill(4)[-4:]
|
|
|
+ #return hash_to_hex(hex_to_hash(hex))
|
|
|
+
|
|
|
+def hex_to_bytes(hex: str) -> bytes:
|
|
|
+ _bytes = bytes.fromhex(hex.zfill(4))
|
|
|
+ return normalize_bytes(_bytes)
|
|
|
+
|
|
|
+def bytes_to_hex(_bytes: bytes) -> str:
|
|
|
+ return normalize_bytes(_bytes).hex()
|
|
|
+
|
|
|
+def hash_to_bytes(_hash: int) -> bytes:
|
|
|
+ _bytes = _hash.to_bytes(8, byteorder='big', signed=True)
|
|
|
+ return normalize_bytes(_bytes)
|
|
|
+
|
|
|
+def bytes_to_hash(_bytes: bytes) -> int:
|
|
|
+ norm = normalize_bytes(_bytes)
|
|
|
+ return int.from_bytes(norm, byteorder='big', signed=False)
|
|
|
+
|
|
|
+def hash_to_hex(_hash: int) -> str:
|
|
|
+ #return hash_to_bytes(_hash).hex()
|
|
|
+ #return normalize_hex(
|
|
|
+ #return f"{_hash + (1 << 64):x}"[-4:]
|
|
|
+ #return hex(_hash + (1<<64))[2:][-4:]
|
|
|
+ #return f"{_hash & 0xffff:04x}"
|
|
|
+ return hex((_hash|0x10000) & 0x1ffff)[3:]
|
|
|
+
|
|
|
+def hex_to_hash(hex: str) -> int:
|
|
|
+ #_bytes = bytes.fromhex(hex.zfill(4))
|
|
|
+ #return bytes_to_hash(_bytes)
|
|
|
+ #return int(normalize_hex(hex), 16)
|
|
|
+ return int(hex, 16) & 0xffff
|
|
|
+
|
|
|
+# PYTHONHASHSEED=0
|
|
|
+#assert hash_to_hex(10) == '000a', ":" + hash_to_hex(10)
|
|
|
+assert hash_to_hex(41545) == 'a249', hash_to_hex(41545)
|
|
|
+assert hash_to_hex(-7583489610679606711) == 'a249', hash_to_hex(-7583489610679606711)
|
|
|
+assert hash_to_hex(1421958803217889556) == normalize_hex('13bbcfff670ab914'), hash_to_hex(1421958803217889556)
|
|
|
+#assert hash('a').to_bytes(8,'big', signed=True) == b'\x96\xc2\x08`\xcd\x93\xa2I'
|
|
|
+assert normalize_bytes(b'\x96\xc2\x08`\xcd\x93\xa2I') == b'\xa2I'
|
|
|
+assert normalize_bytes(b'\xa2I') == b'\xa2I'
|
|
|
+assert normalize_bytes(b'\x0a') == b'\x00\x0a'
|
|
|
+
|
|
|
+#assert hash('a') == -7583489610679606711
|
|
|
+assert normalize_hash(-7583489610679606711) == 41545, normalize_hash(-7583489610679606711)
|
|
|
+assert normalize_hash(41545) == 41545, normalize_hash(41545)
|
|
|
+
|
|
|
+#assert hash('a').to_bytes(8,'big', signed=True).hex() == '96c20860cd93a249'
|
|
|
+assert normalize_hex('96c20860cd93a249') == 'a249', normalize_hex('96c20860cd93a249')
|
|
|
+assert normalize_hex('a249') == 'a249'
|
|
|
+
|
|
|
+assert normalize_hex('a') == '000a', normalize_hex('a')
|
|
|
+assert normalize_hex('0a') == '000a'
|
|
|
+assert normalize_hex('00a') == '000a'
|
|
|
+
|
|
|
+assert hex_to_hash('a') == 10
|
|
|
+assert hex_to_hash('0a') == 10
|
|
|
+assert hex_to_hash('00a') == 10
|
|
|
+assert hex_to_hash('000a') == 10
|
|
|
+assert hex_to_hash('a249') == 41545
|
|
|
+assert hex_to_hash('96c20860cd93a249') == 41545
|
|
|
+
|
|
|
+assert bytes_to_hash(b'\n') == 10
|
|
|
+assert bytes_to_hash(b'\x00\n') == 10
|
|
|
+assert bytes_to_hash(b'\xa2I') == 41545
|
|
|
+assert bytes_to_hash(b'\x96\xc2\x08`\xcd\x93\xa2I') == 41545
|
|
|
+
|
|
|
+assert hex_to_bytes('a') == b'\x00\n'
|
|
|
+assert hex_to_bytes('0a') == b'\x00\n'
|
|
|
+assert hex_to_bytes('00a') == b'\x00\n'
|
|
|
+assert hex_to_bytes('000a') == b'\x00\n'
|
|
|
+assert hex_to_bytes('a249') == b'\xa2I'
|
|
|
+assert hex_to_bytes('96c20860cd93a249') == b'\xa2I'
|
|
|
+
|
|
|
+assert hash_to_hex(1421958803217889556) == normalize_hex('13bbcfff670ab914'), hash_to_hex(1421958803217889556)
|
|
|
+assert hash_to_hex(10) == '000a', hash_to_hex(10)
|
|
|
+assert hash_to_hex(41545) == 'a249', hash_to_hex(41545)
|
|
|
+assert hash_to_hex(-7583489610679606711) == 'a249', hash_to_hex(-7583489610679606711)
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ import timeit
|
|
|
+ # For Python>=3.5 one can also write:
|
|
|
+ #print(timeit.timeit("normalize_bytes(b'a')", globals=locals(), number=5000000))
|
|
|
+ for method, arg in [
|
|
|
+ ('normalize_hash', "hash('a')"),
|
|
|
+ ('normalize_hex', "'a'"),
|
|
|
+ ('normalize_bytes', r"b'\x0a'"),
|
|
|
+ ('hex_to_hash', "'a'"),
|
|
|
+ ('hex_to_bytes', "'a'"),
|
|
|
+ ('bytes_to_hash', r"b'\n'"),
|
|
|
+ ('bytes_to_hex', r"b'\n'"),
|
|
|
+ ('hash_to_bytes', "10"),
|
|
|
+ ('hash_to_hex', "10"),
|
|
|
+ ]:
|
|
|
+ test = f"{method}({arg})"
|
|
|
+ elapsed = timeit.timeit(test, globals=locals(), number=1000000)
|
|
|
+ print(f"{method}: {elapsed}")
|