From beb2665c491c0585de68074fddd389bf41add946 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 10 Feb 2020 17:15:44 -0500 Subject: [PATCH] add tor.IsValidHostname --- tor/torUtils.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tor/torUtils.go b/tor/torUtils.go index c98f659..5a4ac32 100644 --- a/tor/torUtils.go +++ b/tor/torUtils.go @@ -35,3 +35,17 @@ func GetTorV3Hostname(pub ed25519.PublicKey) string { serviceID := base32.StdEncoding.EncodeToString(combined) return strings.ToLower(serviceID) } + +// IsValidHostname returns true if the given address is a valid onion v3 address +func IsValidHostname(address string) bool { + if len(address) == V3HostnameLength { + data, err := base32.StdEncoding.DecodeString(strings.ToUpper(address)) + if err == nil { + pubkey := data[0:ed25519.PublicKeySize] + if GetTorV3Hostname(ed25519.PublicKey(pubkey)) == address { + return true + } + } + } + return false +} \ No newline at end of file