Skip to content

Commit 5c85f0e

Browse files
committed
Merge branch 'release/0.0.5'
2 parents ec7ed6d + 917c3b7 commit 5c85f0e

6 files changed

Lines changed: 128 additions & 85 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/dist/
2-
/wireguard-vanity-keygen
2+
/wireguard-vanity-keygen
3+
/cmd/wg-vanity-keygen/wg-vanity-keygen

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [0.0.5]
4+
5+
- Allow for inclusion as a module (see #2)
6+
7+
38
## [0.0.4]
49

510
- Update core modules

crypto.go renamed to keygen/crypto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package keygen
22

33
import (
44
"crypto/rand"

utils.go renamed to keygen/utils.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package keygen
22

33
import (
44
"fmt"
@@ -8,62 +8,62 @@ import (
88
"time"
99
)
1010

11-
// Checks the search does not contain any invalid characters
12-
func isValidSearch(s string) bool {
11+
// IsValidSearch checks the search does not contain any invalid characters
12+
func IsValidSearch(s string) bool {
1313
var r = regexp.MustCompile(`[^a-zA-Z0-9\/\+]`)
1414
return !r.MatchString(s)
1515
}
1616

17-
// Returns a human-readable output of time.Duration
18-
func humanizeDuration(duration time.Duration) string {
17+
// HumanizeDuration returns a human-readable output of time.Duration
18+
func HumanizeDuration(duration time.Duration) string {
1919
// more than duration can handle
2020
if duration.Hours() < 0.0 {
2121
return fmt.Sprintf("hundreds of years")
2222
}
2323
if duration.Hours() > 8760.0 {
2424
y := int64(duration.Hours() / 8760)
25-
return fmt.Sprintf("%d %s", y, plural("year", y))
25+
return fmt.Sprintf("%d %s", y, Plural("year", y))
2626
}
2727
if duration.Hours() > 720.0 {
2828
m := int64(duration.Hours() / 24 / 30)
29-
return fmt.Sprintf("%d %s", m, plural("month", m))
29+
return fmt.Sprintf("%d %s", m, Plural("month", m))
3030
}
3131
if duration.Hours() > 168.0 {
3232
w := int64(duration.Hours() / 168)
33-
return fmt.Sprintf("%d %s", w, plural("week", w))
33+
return fmt.Sprintf("%d %s", w, Plural("week", w))
3434
}
3535
if duration.Seconds() < 60.0 {
3636
s := int64(duration.Seconds())
37-
return fmt.Sprintf("%d %s", s, plural("second", s))
37+
return fmt.Sprintf("%d %s", s, Plural("second", s))
3838
}
3939
if duration.Minutes() < 60.0 {
4040
m := int64(duration.Minutes())
41-
return fmt.Sprintf("%d %s", m, plural("minute", m))
41+
return fmt.Sprintf("%d %s", m, Plural("minute", m))
4242
}
4343
if duration.Hours() < 24.0 {
4444
m := int64(math.Mod(duration.Minutes(), 60))
4545
h := int64(duration.Hours())
4646
return fmt.Sprintf("%d %s, %d %s",
47-
h, plural("hour", h), m, plural("minute", m))
47+
h, Plural("hour", h), m, Plural("minute", m))
4848
}
4949
// if duration.Hours() <
5050
h := int64(math.Mod(duration.Hours(), 24))
5151
d := int64(duration.Hours() / 24)
5252
return fmt.Sprintf("%d %s, %d %s",
53-
d, plural("day", d), h, plural("hour", h))
53+
d, Plural("day", d), h, Plural("hour", h))
5454
}
5555

56-
// Returns a plural of `s` if the value `v` is 0 or > 0
57-
func plural(s string, v int64) string {
56+
// Plural returns a Plural of `s` if the value `v` is 0 or > 0
57+
func Plural(s string, v int64) string {
5858
if v == 1 {
5959
return s
6060
}
6161

6262
return s + "s"
6363
}
6464

65-
// Returns a number-formatted string, eg: 1,123,456
66-
func numberFormat(n int64) string {
65+
// NumberFormat returns a number-formatted string, eg: 1,123,456
66+
func NumberFormat(n int64) string {
6767
in := strconv.FormatInt(n, 10)
6868
numOfDigits := len(in)
6969
if n < 0 {

worker.go renamed to keygen/worker.go

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
1-
package main
1+
package keygen
22

33
import (
4-
"fmt"
54
"strings"
65
"sync"
76
"time"
87
)
98

10-
var (
11-
wordMap map[string]int
12-
mapMutex = sync.RWMutex{}
13-
)
9+
// Options struct
10+
type Options struct {
11+
LimitResults int
12+
Threads int
13+
CaseSensitive bool
14+
Cores int
15+
}
16+
17+
// Cruncher struct
18+
type Cruncher struct {
19+
Options
20+
WordMap map[string]int
21+
mapMutex sync.RWMutex
22+
thread chan int
23+
Abort bool // set to true to abort processing
24+
}
25+
26+
// Pair struct
27+
type Pair struct {
28+
Private string
29+
Public string
30+
}
31+
32+
// New returns a Cruncher
33+
func New(options Options) *Cruncher {
34+
return &Cruncher{
35+
Options: options,
36+
WordMap: make(map[string]int),
37+
thread: make(chan int, options.Cores),
38+
}
39+
}
1440

1541
// Crunch will generate a new key and compare to the search(s)
16-
func Crunch() {
42+
func (c *Cruncher) crunch(cb func(match Pair)) bool {
1743
k, err := newPrivateKey()
1844
if err != nil {
1945
panic(err)
@@ -22,7 +48,7 @@ func Crunch() {
2248
pub := k.Public().String()
2349
matchKey := pub
2450

25-
if !caseSensitive {
51+
if !c.CaseSensitive {
2652
matchKey = strings.ToLower(pub)
2753
}
2854

@@ -32,30 +58,26 @@ func Crunch() {
3258

3359
// Allow only one routine at a time to avoid
3460
// "concurrent map iteration and map write"
35-
mapMutex.Lock()
36-
defer mapMutex.Unlock()
37-
for w, count := range wordMap {
61+
c.mapMutex.Lock()
62+
defer c.mapMutex.Unlock()
63+
for w, count := range c.WordMap {
3864
if count == 0 {
3965
continue
4066
}
4167
completed = false
4268
if strings.HasPrefix(matchKey, w) {
43-
wordMap[w] = count - 1
44-
fmt.Printf("private %s public %s\n", k.String(), pub)
69+
c.WordMap[w] = count - 1
70+
cb(Pair{Private: k.String(), Public: pub})
4571
}
4672
}
4773

48-
if completed {
49-
// send exit status, allows time for processes to exit
50-
stopChan <- 1
51-
}
52-
53-
<-threadChan // removes an int from threads, allowing another to proceed
74+
<-c.thread // removes an int from threads, allowing another to proceed
75+
return completed
5476
}
5577

5678
// CalculateSpeed returns average calculations per second based
5779
// on the time per run taken from 2 seconds runtime.
58-
func calculateSpeed() (int64, time.Duration) {
80+
func (c *Cruncher) CalculateSpeed() (int64, time.Duration) {
5981
var n int64
6082
n = 1
6183
start := time.Now()
@@ -68,7 +90,7 @@ func calculateSpeed() (int64, time.Duration) {
6890
}
6991
}
7092

71-
threadChan <- 1 // will block if there is MAX ints in threads
93+
c.thread <- 1 // will block if there is MAX ints in threads
7294
go func() {
7395
// dry run
7496
k, err := newPrivateKey()
@@ -80,12 +102,12 @@ func calculateSpeed() (int64, time.Duration) {
80102

81103
// Allow only one routine at a time to avoid
82104
// "concurrent map iteration and map write"
83-
mapMutex.Lock()
84-
defer mapMutex.Unlock()
85-
for w := range wordMap {
105+
c.mapMutex.Lock()
106+
defer c.mapMutex.Unlock()
107+
for w := range c.WordMap {
86108
_ = strings.HasPrefix(t, w)
87109
}
88-
<-threadChan // removes an int from threads, allowing another to proceed
110+
<-c.thread // removes an int from threads, allowing another to proceed
89111
n++
90112
}()
91113
}
@@ -99,7 +121,7 @@ func calculateSpeed() (int64, time.Duration) {
99121
// can be found. Case-insensitive letter matches [a-z] can be
100122
// found in upper and lowercase combinations, so have a higher
101123
// chance of being found than [0-9], / or +, or case-sensitive matches.
102-
func calculateProbability(s string) int64 {
124+
func CalculateProbability(s string, caseSensitive bool) int64 {
103125
var nonAlphaProbability, alphaProbability int64
104126
alphaProbability = 26 + 10 + 2
105127
nonAlphaProbability = 26 + 26 + 10 + 2
@@ -120,3 +142,27 @@ func calculateProbability(s string) int64 {
120142

121143
return p
122144
}
145+
146+
// CollectToSlice will run till all the matching keys were calculated. This can take some time
147+
func (c *Cruncher) CollectToSlice() []Pair {
148+
var matches []Pair
149+
c.Find(func(match Pair) {
150+
matches = append(matches, match)
151+
})
152+
return matches
153+
}
154+
155+
// Find will invoke a callback function for each match to support some interactivity or at least feedback
156+
func (c *Cruncher) Find(cb func(match Pair)) {
157+
for {
158+
c.thread <- 1 // will block if there is MAX ints in threads
159+
go func() {
160+
if c.crunch(cb) {
161+
c.Abort = true
162+
}
163+
}()
164+
if c.Abort {
165+
return
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)