Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
quick and dirty go impl of tor hsv3 onion address generation
package main
import (
"crypto/ed25519"
"encoding/base32"
"encoding/hex"
"fmt"
"strings"
"golang.org/x/crypto/sha3"
)
func main() {
var pubkey string = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"
var version byte = 3
checksum := v3checksum(decodePubkey(pubkey), version)
address := append(decodePubkey(pubkey), checksum...)
address = append(address, version)
onion := strings.ToLower(base32.StdEncoding.EncodeToString(address))
fmt.Println(onion)
}
func decodePubkey(encoded string) (decoded []byte) {
decoded, err := hex.DecodeString(encoded)
if err != nil {
panic(err)
}
if len(decoded) != ed25519.PublicKeySize {
panic("invalid key data length")
}
return decoded
}
func v3checksum(pubkey []byte, version byte) (checksum []byte) {
prefix := []byte(".onion checksum")
data := append(prefix, pubkey...)
data = append(data, version)
shasum := sha3.Sum256(data)
checksum = shasum[:][:2]
return checksum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment