-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.go
40 lines (31 loc) · 1.1 KB
/
address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"encoding/base32"
"strings"
// Use bine for ed25519: https://stackoverflow.com/questions/44810708/ed25519-public-result-is-different
"github.com/cretz/bine/torutil/ed25519"
"golang.org/x/crypto/sha3"
)
const (
// 15 (constant string), 32 (raw public key), 1 (version) = 48
checkSize = 48
// 32 (raw pulic key), 2 (checksum), 1 (version) = 35
addressSize = 35
version = 0x03
)
var salt = []byte(".onion checksum")
// V3AddressFromKeyPair calculates the V3 Tor address for an ed25519 key pair.
func V3AddressFromKeyPair(pair ed25519.KeyPair) string {
checkBytes := make([]byte, checkSize)
copy(checkBytes[:len(salt)], salt)
checkBytes[checkSize-1] = version
addressBytes := make([]byte, addressSize)
addressBytes[addressSize-1] = version
keyPair, _ := ed25519.GenerateKey(nil)
copy(checkBytes[15:], keyPair.PublicKey())
checksum := sha3.Sum256(checkBytes)
// onion_address = base32(pubkey || checksum || version)
copy(addressBytes[0:], keyPair.PublicKey())
copy(addressBytes[32:], checksum[0:2])
return strings.ToLower(base32.StdEncoding.EncodeToString(addressBytes))
}