forked from axllent/wireguard-vanity-keygen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
122 lines (104 loc) · 2.69 KB
/
worker.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"strings"
"sync"
"time"
)
var (
wordMap map[string]int
mapMutex = sync.RWMutex{}
)
// Crunch will generate a new key and compare to the search(s)
func Crunch() {
k, err := newPrivateKey()
if err != nil {
panic(err)
}
pub := k.Public().String()
matchKey := pub
if !caseSensitive {
matchKey = strings.ToLower(pub)
}
// Assume the task is completed, once all searched have been found
// and limits have been reached, it sends an exit signal
completed := true
// Allow only one routine at a time to avoid
// "concurrent map iteration and map write"
mapMutex.Lock()
defer mapMutex.Unlock()
for w, count := range wordMap {
if count == 0 {
continue
}
completed = false
if strings.HasPrefix(matchKey, w) {
wordMap[w] = count - 1
fmt.Printf("private %s public %s\n", k.String(), pub)
}
}
if completed {
// send exit status, allows time for processes to exit
stopChan <- 1
}
<-threadChan // removes an int from threads, allowing another to proceed
}
// CalculateSpeed returns average calculations per second based
// on the time per run taken from 2 seconds runtime.
func calculateSpeed() (int64, time.Duration) {
var n int64
n = 1
start := time.Now()
for loopStart := time.Now(); ; {
// check every 100 loops if time is reached
if n%100 == 0 {
if time.Since(loopStart) > 2*time.Second {
break
}
}
threadChan <- 1 // will block if there is MAX ints in threads
go func() {
// dry run
k, err := newPrivateKey()
if err != nil {
panic(err)
}
_ = k.String()
t := strings.ToLower(k.Public().String())
// Allow only one routine at a time to avoid
// "concurrent map iteration and map write"
mapMutex.Lock()
defer mapMutex.Unlock()
for w := range wordMap {
_ = strings.HasPrefix(t, w)
}
<-threadChan // removes an int from threads, allowing another to proceed
n++
}()
}
estimate64 := int64(time.Since(start)) / n
return n / 2, time.Duration(estimate64)
}
// CalculateProbability calculates the probability that a string
// can be found. Case-insensitive letter matches [a-z] can be
// found in upper and lowercase combinations, so have a higher
// chance of being found than [0-9], / or +, or case-sensitive matches.
func calculateProbability(s string) int64 {
var nonAlphaProbability, alphaProbability int64
alphaProbability = 26 + 10 + 2
nonAlphaProbability = 26 + 26 + 10 + 2
if caseSensitive {
alphaProbability = nonAlphaProbability
}
ascii := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var p int64
p = 1
for _, char := range s {
if !strings.Contains(ascii, string(char)) {
p = p * nonAlphaProbability
} else {
p = p * alphaProbability
}
}
return p
}