Adding IsValidHostname function
This commit is contained in:
parent
aba5fa4609
commit
d54ed0b106
17
utils/tor.go
17
utils/tor.go
|
@ -33,6 +33,8 @@ func expandKey(pri ed25519.PrivateKey) string {
|
||||||
return base64.StdEncoding.EncodeToString(h[:])
|
return base64.StdEncoding.EncodeToString(h[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const V3HostnameLength = 56
|
||||||
|
|
||||||
// Hidden service version
|
// Hidden service version
|
||||||
const version = byte(0x03)
|
const version = byte(0x03)
|
||||||
|
|
||||||
|
@ -48,6 +50,7 @@ func getCheckdigits(pub ed25519.PublicKey) []byte {
|
||||||
return checksum[:2]
|
return checksum[:2]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTorV3Hostname converts an ed25519 public key to a valid tor onion hostname
|
||||||
func GetTorV3Hostname(pub ed25519.PublicKey) string {
|
func GetTorV3Hostname(pub ed25519.PublicKey) string {
|
||||||
// Construct onion address base32(publicKey || checkdigits || version)
|
// Construct onion address base32(publicKey || checkdigits || version)
|
||||||
checkdigits := getCheckdigits(pub)
|
checkdigits := getCheckdigits(pub)
|
||||||
|
@ -57,3 +60,17 @@ func GetTorV3Hostname(pub ed25519.PublicKey) string {
|
||||||
serviceID := base32.StdEncoding.EncodeToString(combined)
|
serviceID := base32.StdEncoding.EncodeToString(combined)
|
||||||
return strings.ToLower(serviceID)
|
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
|
||||||
|
}
|
|
@ -1,10 +1,12 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/asn1"
|
"encoding/asn1"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
|
"golang.org/x/crypto/ed25519"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -41,3 +43,16 @@ func TestGetTorHostname(t *testing.T) {
|
||||||
t.Errorf("Hostname %s does not equal %s", hostname, "kwke2hntvyfqm7dr")
|
t.Errorf("Hostname %s does not equal %s", hostname, "kwke2hntvyfqm7dr")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func TestV3(t *testing.T) {
|
||||||
|
pub,_,_ := ed25519.GenerateKey(rand.Reader)
|
||||||
|
hostname := GetTorV3Hostname(pub)
|
||||||
|
if !IsValidHostname(hostname) {
|
||||||
|
t.Errorf("Generated V3 Hostname was invalid")
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsValidHostname(hostname[0:34]) {
|
||||||
|
t.Errorf("Invalid V3 Hostname was marked valid")
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue