hs_build_address.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import sys
  2. import hashlib
  3. import struct
  4. import base64
  5. # Python 3.6+, the SHA3 is available in hashlib natively. Else this requires
  6. # the pysha3 package (pip install pysha3).
  7. if sys.version_info < (3, 6):
  8. import sha3
  9. # Test vector to make sure the right sha3 version will be used. pysha3 < 1.0
  10. # used the old Keccak implementation. During the finalization of SHA3, NIST
  11. # changed the delimiter suffix from 0x01 to 0x06. The Keccak sponge function
  12. # stayed the same. pysha3 1.0 provides the previous Keccak hash, too.
  13. TEST_VALUE = "e167f68d6563d75bb25f3aa49c29ef612d41352dc00606de7cbd630bb2665f51"
  14. if TEST_VALUE != sha3.sha3_256(b"Hello World").hexdigest():
  15. print("pysha3 version is < 1.0. Please install from:")
  16. print("https://github.com/tiran/pysha3https://github.com/tiran/pysha3")
  17. sys.exit(1)
  18. # Checksum is built like so:
  19. # CHECKSUM = SHA3(".onion checksum" || PUBKEY || VERSION)
  20. PREFIX = ".onion checksum".encode()
  21. # 32 bytes ed25519 pubkey.
  22. PUBKEY = ("\x42" * 32).encode()
  23. # Version 3 is proposal224
  24. VERSION = 3
  25. data = struct.pack('15s32sb', PREFIX, PUBKEY, VERSION)
  26. checksum = hashlib.sha3_256(data).digest()
  27. # Onion address is built like so:
  28. # onion_address = base32(PUBKEY || CHECKSUM || VERSION) + ".onion"
  29. address = struct.pack('!32s2sb', PUBKEY, checksum, VERSION)
  30. onion_addr = base64.b32encode(address).decode().lower()
  31. print("%s" % (onion_addr))