forked from openprivacy/connectivity
33 lines
815 B
Go
33 lines
815 B
Go
package tor
|
|
|
|
import (
|
|
"encoding/base32"
|
|
// "errors"
|
|
"golang.org/x/crypto/ed25519"
|
|
"golang.org/x/crypto/sha3"
|
|
"strings"
|
|
)
|
|
|
|
func getCheckdigits(pub ed25519.PublicKey) []byte {
|
|
// Calculate checksum sha3(".onion checksum" || publicKey || version)
|
|
checkstr := []byte(salt)
|
|
checkstr = append(checkstr, pub...)
|
|
checkstr = append(checkstr, version)
|
|
checksum := sha3.Sum256(checkstr)
|
|
return checksum[:2]
|
|
}
|
|
|
|
// 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
|
|
}
|