Skip to content

Commit

Permalink
Merge branch 'release/0.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
axllent committed Mar 12, 2023
2 parents ec7ed6d + 917c3b7 commit 5c85f0e
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 85 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/dist/
/wireguard-vanity-keygen
/wireguard-vanity-keygen
/cmd/wg-vanity-keygen/wg-vanity-keygen
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [0.0.5]

- Allow for inclusion as a module (see #2)


## [0.0.4]

- Update core modules
Expand Down
2 changes: 1 addition & 1 deletion crypto.go → keygen/crypto.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package keygen

import (
"crypto/rand"
Expand Down
32 changes: 16 additions & 16 deletions utils.go → keygen/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package keygen

import (
"fmt"
Expand All @@ -8,62 +8,62 @@ import (
"time"
)

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

// Returns a human-readable output of time.Duration
func humanizeDuration(duration time.Duration) string {
// HumanizeDuration returns a human-readable output of time.Duration
func HumanizeDuration(duration time.Duration) string {
// more than duration can handle
if duration.Hours() < 0.0 {
return fmt.Sprintf("hundreds of years")
}
if duration.Hours() > 8760.0 {
y := int64(duration.Hours() / 8760)
return fmt.Sprintf("%d %s", y, plural("year", y))
return fmt.Sprintf("%d %s", y, Plural("year", y))
}
if duration.Hours() > 720.0 {
m := int64(duration.Hours() / 24 / 30)
return fmt.Sprintf("%d %s", m, plural("month", m))
return fmt.Sprintf("%d %s", m, Plural("month", m))
}
if duration.Hours() > 168.0 {
w := int64(duration.Hours() / 168)
return fmt.Sprintf("%d %s", w, plural("week", w))
return fmt.Sprintf("%d %s", w, Plural("week", w))
}
if duration.Seconds() < 60.0 {
s := int64(duration.Seconds())
return fmt.Sprintf("%d %s", s, plural("second", s))
return fmt.Sprintf("%d %s", s, Plural("second", s))
}
if duration.Minutes() < 60.0 {
m := int64(duration.Minutes())
return fmt.Sprintf("%d %s", m, plural("minute", m))
return fmt.Sprintf("%d %s", m, Plural("minute", m))
}
if duration.Hours() < 24.0 {
m := int64(math.Mod(duration.Minutes(), 60))
h := int64(duration.Hours())
return fmt.Sprintf("%d %s, %d %s",
h, plural("hour", h), m, plural("minute", m))
h, Plural("hour", h), m, Plural("minute", m))
}
// if duration.Hours() <
h := int64(math.Mod(duration.Hours(), 24))
d := int64(duration.Hours() / 24)
return fmt.Sprintf("%d %s, %d %s",
d, plural("day", d), h, plural("hour", h))
d, Plural("day", d), h, Plural("hour", h))
}

// Returns a plural of `s` if the value `v` is 0 or > 0
func plural(s string, v int64) string {
// Plural returns a Plural of `s` if the value `v` is 0 or > 0
func Plural(s string, v int64) string {
if v == 1 {
return s
}

return s + "s"
}

// Returns a number-formatted string, eg: 1,123,456
func numberFormat(n int64) string {
// NumberFormat returns a number-formatted string, eg: 1,123,456
func NumberFormat(n int64) string {
in := strconv.FormatInt(n, 10)
numOfDigits := len(in)
if n < 0 {
Expand Down
98 changes: 72 additions & 26 deletions worker.go → keygen/worker.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
package main
package keygen

import (
"fmt"
"strings"
"sync"
"time"
)

var (
wordMap map[string]int
mapMutex = sync.RWMutex{}
)
// Options struct
type Options struct {
LimitResults int
Threads int
CaseSensitive bool
Cores int
}

// Cruncher struct
type Cruncher struct {
Options
WordMap map[string]int
mapMutex sync.RWMutex
thread chan int
Abort bool // set to true to abort processing
}

// Pair struct
type Pair struct {
Private string
Public string
}

// New returns a Cruncher
func New(options Options) *Cruncher {
return &Cruncher{
Options: options,
WordMap: make(map[string]int),
thread: make(chan int, options.Cores),
}
}

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

if !caseSensitive {
if !c.CaseSensitive {
matchKey = strings.ToLower(pub)
}

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

// 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 {
c.mapMutex.Lock()
defer c.mapMutex.Unlock()
for w, count := range c.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)
c.WordMap[w] = count - 1
cb(Pair{Private: k.String(), Public: pub})
}
}

if completed {
// send exit status, allows time for processes to exit
stopChan <- 1
}

<-threadChan // removes an int from threads, allowing another to proceed
<-c.thread // removes an int from threads, allowing another to proceed
return completed
}

// CalculateSpeed returns average calculations per second based
// on the time per run taken from 2 seconds runtime.
func calculateSpeed() (int64, time.Duration) {
func (c *Cruncher) CalculateSpeed() (int64, time.Duration) {
var n int64
n = 1
start := time.Now()
Expand All @@ -68,7 +90,7 @@ func calculateSpeed() (int64, time.Duration) {
}
}

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

// Allow only one routine at a time to avoid
// "concurrent map iteration and map write"
mapMutex.Lock()
defer mapMutex.Unlock()
for w := range wordMap {
c.mapMutex.Lock()
defer c.mapMutex.Unlock()
for w := range c.WordMap {
_ = strings.HasPrefix(t, w)
}
<-threadChan // removes an int from threads, allowing another to proceed
<-c.thread // removes an int from threads, allowing another to proceed
n++
}()
}
Expand All @@ -99,7 +121,7 @@ func calculateSpeed() (int64, time.Duration) {
// 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 {
func CalculateProbability(s string, caseSensitive bool) int64 {
var nonAlphaProbability, alphaProbability int64
alphaProbability = 26 + 10 + 2
nonAlphaProbability = 26 + 26 + 10 + 2
Expand All @@ -120,3 +142,27 @@ func calculateProbability(s string) int64 {

return p
}

// CollectToSlice will run till all the matching keys were calculated. This can take some time
func (c *Cruncher) CollectToSlice() []Pair {
var matches []Pair
c.Find(func(match Pair) {
matches = append(matches, match)
})
return matches
}

// Find will invoke a callback function for each match to support some interactivity or at least feedback
func (c *Cruncher) Find(cb func(match Pair)) {
for {
c.thread <- 1 // will block if there is MAX ints in threads
go func() {
if c.crunch(cb) {
c.Abort = true
}
}()
if c.Abort {
return
}
}
}
Loading

0 comments on commit 5c85f0e

Please sign in to comment.