Merge branch 'maint-0.2.9' into maint-0.3.3
[tor.git] / src / test / hs_build_address.py
blob7ff22c3a9a963195cc4817342b16b4d278337e32
1 import sys
2 import hashlib
3 import struct
4 import base64
6 # Python 3.6+, the SHA3 is available in hashlib natively. Else this requires
7 # the pysha3 package (pip install pysha3).
8 if sys.version_info < (3, 6):
9 import sha3
11 # Test vector to make sure the right sha3 version will be used. pysha3 < 1.0
12 # used the old Keccak implementation. During the finalization of SHA3, NIST
13 # changed the delimiter suffix from 0x01 to 0x06. The Keccak sponge function
14 # stayed the same. pysha3 1.0 provides the previous Keccak hash, too.
15 TEST_VALUE = "e167f68d6563d75bb25f3aa49c29ef612d41352dc00606de7cbd630bb2665f51"
16 if TEST_VALUE != sha3.sha3_256(b"Hello World").hexdigest():
17 print("pysha3 version is < 1.0. Please install from:")
18 print("https://github.com/tiran/pysha3https://github.com/tiran/pysha3")
19 sys.exit(1)
21 # Checksum is built like so:
22 # CHECKSUM = SHA3(".onion checksum" || PUBKEY || VERSION)
23 PREFIX = ".onion checksum".encode()
24 # 32 bytes ed25519 pubkey from first test vector of
25 # https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02#section-6
26 PUBKEY = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a".decode('hex')
27 # Version 3 is proposal224
28 VERSION = 3
30 data = struct.pack('15s32sb', PREFIX, PUBKEY, VERSION)
31 checksum = hashlib.sha3_256(data).digest()
33 # Onion address is built like so:
34 # onion_address = base32(PUBKEY || CHECKSUM || VERSION) + ".onion"
35 address = struct.pack('!32s2sb', PUBKEY, checksum, VERSION)
36 onion_addr = base64.b32encode(address).decode().lower()
38 print("%s" % (onion_addr))