-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (110 loc) · 2.72 KB
/
main.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
123
124
125
126
127
128
129
package main
import (
"crypto/ed25519"
"crypto/rand"
"encoding/pem"
"flag"
"fmt"
"github.com/mikesmitty/edkey"
"golang.org/x/crypto/ssh"
"golang.org/x/text/language"
"golang.org/x/text/message"
"io"
"log"
"os"
"regexp"
"runtime"
"runtime/pprof"
"sync/atomic"
"time"
)
const syncStep = 1000
var (
threads = flag.Int("threads", runtime.NumCPU(), "number of threads to run")
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to file")
)
func incr(arr *[]byte) {
for i := len(*arr) - 1; i >= 0; i-- {
(*arr)[i]++
if (*arr)[i] != 0 {
break
}
}
}
type result struct {
privRepr []byte
pubRepr []byte
}
func main() {
flag.Parse()
if flag.NArg() != 1 {
fmt.Fprintf(flag.CommandLine.Output(), "%s [FLAGS] REGEX, with flags:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
log.Fatalf("Could not create CPU profile: %v", err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatalf("Could not profile CPU: %v", err)
}
defer pprof.StopCPUProfile()
}
researched := regexp.MustCompile(flag.Arg(0))
log.Printf("Looking for a public key matching %v", researched)
start := time.Now()
attempts := int64(0)
resultCh := make(chan result, 1)
for i := 0; i < *threads; i++ {
go func() {
localAttempts := 0
seed := make([]byte, ed25519.SeedSize)
publicKey := make(ed25519.PublicKey, ed25519.PublicKeySize)
if _, err := io.ReadFull(rand.Reader, seed); err != nil {
log.Fatalf("Could not read randomness: %v", err)
}
for {
privateKey := ed25519.NewKeyFromSeed(seed)
copy(publicKey, privateKey[32:])
pubKey, err := ssh.NewPublicKey(publicKey)
if err != nil {
log.Fatalf("Could not derive public key: %v", err)
}
pubRepr := ssh.MarshalAuthorizedKey(pubKey)
// Remove trailing newline
pubRepr = pubRepr[:len(pubRepr)-1]
if researched.Match(pubRepr) {
resultCh <- result{
privRepr: pem.EncodeToMemory(&pem.Block{
Type: "OPENSSH PRIVATE KEY",
Bytes: edkey.MarshalED25519PrivateKey(privateKey),
}),
pubRepr: pubRepr,
}
}
localAttempts++
if localAttempts == syncStep {
localAttempts = 0
atomic.AddInt64(&attempts, syncStep)
}
incr(&seed)
}
}()
}
ticker := time.NewTicker(time.Second)
printer := message.NewPrinter(language.English)
for {
select {
case found := <-resultCh:
log.Printf("Public key:\n%s", found.pubRepr)
log.Printf("Private key:\n%s", found.privRepr)
return
case <-ticker.C:
rate := int(float64(attempts) / time.Since(start).Seconds())
log.Printf("Generated %s keypairs (%s Hz)", printer.Sprint(atomic.LoadInt64(&attempts)), printer.Sprint(rate))
}
}
}