Take the Tor Project's own onion address: 2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion. It looks random, and mostly it is, but every one of those 56 characters has a job. There's no lookup table and no registry behind it. The address is the service's public key, with a little error checking bolted on.

The recipe

The v3 address format is defined in the Tor rendezvous spec. Stripped down, it's this:

onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
CHECKSUM      = SHA3-256(".onion checksum" | PUBKEY | VERSION)[:2]

PUBKEY   = 32 bytes, the ed25519 public key of the service
VERSION  = 1 byte, 0x03
CHECKSUM = first 2 bytes of the SHA3-256 hash above

That's 32 + 2 + 1 = 35 bytes, or 280 bits. Base32 encodes five bits per character, and 280 divided by 5 is exactly 56. No padding, no leftover bits, which is a tidy bit of design. v2 addresses were 16 characters because they were 80 bits of a SHA-1 hash of an RSA key; v3 doesn't hash the key down at all, it just puts the whole thing in the address.

Byte by byte

BytesBase32 charactersContents
0–31roughly the first 51.2ed25519 public key
32–33roughly 51.2 to 54.4Checksum
34roughly the last 1.6Version (0x03)

The fractional character counts aren't a typo. Base32 characters straddle byte boundaries, so the key bleeds into character 52, the checksum straddles a couple more, and the version byte is split across the last two characters. That's why you can't just "look at the last two letters" to read the checksum.

Why the last letter is always d

The version byte is 0x03, binary 00000011. The final base32 character holds the last five bits of the 280-bit string, which are the last five bits of the version byte: 00011. In the RFC 4648 base32 alphabet (a–z then 2–7), value 3 is d.

So every valid v3 address ends in d. If someone hands you a 56-character address ending in anything else, it's broken. It's a crude sanity check, but I've seen it catch copy-paste mangling more than once.

What the checksum does and doesn't do

The two-byte checksum exists to catch typos. If you change a character, the decoded key and the checksum will almost certainly stop matching, and tor refuses the address outright instead of trying to fetch a descriptor that doesn't exist. Two bytes means a random corruption has about a 1 in 65,536 chance of slipping through. That's fine for typos.

It is not a security feature against phishing. Anyone can generate a key, compute its checksum, and get a perfectly valid address. A phishing clone of a site has a valid checksum too. It just has a different key. The checksum also includes the string ".onion checksum" as a prefix to the hash input, which is ordinary domain separation so this hash can't be confused with some other SHA3 use in the protocol.

Checking an address is a separate problem, covered in How to Verify an Onion Address. Short version: compare the whole thing against a source you trust, not the first and last few characters.

Decoding one yourself

You can pull the parts out with a few lines of Python. This uses the standard library plus hashlib's SHA3, which has been built in since Python 3.6:

import base64, hashlib

addr = "2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid"
raw = base64.b32decode(addr.upper())

pubkey, checksum, version = raw[:32], raw[32:34], raw[34:]
expected = hashlib.sha3_256(b".onion checksum" + pubkey + version).digest()[:2]

print("version:", version.hex())
print("checksum ok:", checksum == expected)
print("pubkey:", pubkey.hex())

Run it and you'll get version 03, a valid checksum, and 32 bytes of key. Swap one letter in the address and the checksum check fails.

The key files on disk

When tor creates a v3 service, the HiddenServiceDir ends up with:

/var/lib/tor/mysite/
├── authorized_clients/
├── hostname
├── hs_ed25519_public_key
└── hs_ed25519_secret_key

hostname is just the address as text. hs_ed25519_public_key holds the 32-byte key behind a short header. The important file is hs_ed25519_secret_key. Whoever has it is the service. Copy it to another machine, start tor there, and that machine now answers for your address. Lose it, and the address is gone forever, since there's no recovery or reissuing. Back it up somewhere encrypted and offline. If it leaks, treat the address as burned.

The empty authorized_clients/ directory is for client authorization, which lets you restrict who can even find the service.

Why you can't pick your address

Since the address is the key, choosing an address means finding a private key whose public key happens to encode to the string you want. Ed25519 keys come from random 32-byte seeds and there's no way to steer the output. All you can do is generate keys by the billion and keep the ones that start with the letters you like. That's what mkp224o does. A five or six character prefix is quick. Each extra character multiplies the work by 32, so anything past about eight or nine gets expensive fast. Facebook's and the New York Times' onion addresses start with readable words for exactly this reason: someone burned a lot of CPU on them.

Where the blinded keys come in

One more detail that trips people up. The key in the address is the long-term identity key, but tor never puts it in a descriptor directly. Each day it derives a blinded public key from the identity key and the current time period, and uses that to sign and index descriptors. HSDirs only ever see blinded keys, so they can't list the onion addresses they're storing. Clients who know the address can do the same derivation and find the right descriptor. It's the main reason v3 addresses can't be scraped from the network the way v2 ones were, and it's one of the things the move away from v2 was really about.

#v3-onion #onion-address #ed25519 #base32 #checksum