@@ -20,12 +20,14 @@ const b32Lower = "abcdefghijklmnopqrstuvwxyz234567"
20
20
21
21
var b32Enc = base32 .NewEncoding (b32Lower ).WithPadding (base32 .NoPadding )
22
22
23
- func generate (wg * sync.WaitGroup , prefix string ) {
23
+ func generate (wg * sync.WaitGroup , prefix string , prefixDecodedLen int ) {
24
24
for {
25
25
publicKey , secretKey , err := ed25519 .GenerateKey (nil )
26
26
checkErr (err )
27
27
28
- publicKeyB32 := b32Enc .EncodeToString (publicKey )
28
+ // Match the public key with prefix.
29
+ // No need to encode all PK to B32 if prefix shorter
30
+ publicKeyB32 := b32Enc .EncodeToString (publicKey [0 :prefixDecodedLen ])
29
31
// If a matching address is found, save key and notify wait group
30
32
if strings .HasPrefix (publicKeyB32 , prefix ) {
31
33
onionAddress := encodePublicKey (publicKey )
@@ -92,6 +94,19 @@ func main() {
92
94
os .Exit (1 )
93
95
}
94
96
97
+ prefixLen := len (prefix )
98
+ prefixDecodedLen := b32Enc .DecodedLen (prefixLen )
99
+ // Same DecodedLen(prefix) but without rounding to floor
100
+ prefixDecodedLenRatio := float64 (prefixLen ) * 5.0 / 8.0
101
+ // if there is some division remainder
102
+ if prefixDecodedLenRatio > float64 (prefixDecodedLen ) {
103
+ // 8 bytes encoded into 5 chars. We must add them all because don't know which byte changes a char
104
+ prefixDecodedLen += 8
105
+ }
106
+ if prefixDecodedLen > 32 {
107
+ prefixDecodedLen = 32
108
+ }
109
+
95
110
// Get the number of desired addresses from second argument.
96
111
numAddresses , _ := strconv .Atoi (os .Args [2 ])
97
112
@@ -101,7 +116,7 @@ func main() {
101
116
102
117
// For each CPU, run a generate goroutine
103
118
for i := 0 ; i < runtime .NumCPU (); i ++ {
104
- go generate (& wg , prefix )
119
+ go generate (& wg , prefix , prefixDecodedLen )
105
120
}
106
121
107
122
// Exit after the desired number of addresses have been found.
0 commit comments