-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (157 loc) · 4.44 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
/*
Copyright (C) 2021 Daniel Gurney
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import (
"flag"
"fmt"
"math/rand"
"time"
"github.com/dgurney/unikey/generator"
"github.com/dgurney/unikey/validator"
)
const version = "0.7.0"
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
benchAll := flag.Uint("bench", 0, "Benchmark generation of N*3 keys.")
bench := flag.Uint("bg", 0, "Benchmark generation of N*3 keys.")
benchValidation := flag.Uint("bv", 0, "Benchmark validation of N*3 keys.")
cd := flag.Bool("d", false, "Generate a 10-digit CD key.")
elevencd := flag.Bool("e", false, "Generate an 11-digit CD key.")
oem := flag.Bool("o", false, "Generate an OEM key.")
repeat := flag.Int("r", 1, "Generate n keys.")
t := flag.Bool("t", false, "Show how long the generation took.")
validate := flag.String("v", "", "Validate a CD or OEM key.")
ver := flag.Bool("ver", false, "Show version information and exit.")
Is95 := flag.Bool("95", false, "Apply Windows 95/equivalent old product rules to OEM/CD key validation.")
strict11Digit := flag.Bool("11s", false, "Enforce check digit validation on 11-digit CD keys (should not be needed in regular cases)")
flag.Parse()
if *ver {
fmt.Printf("unikey-mod7 v%s by Daniel Gurney\n", version)
return
}
if *repeat < 1 {
*repeat = 1
}
if *bench != 0 {
fmt.Printf("Running key generation benchmark with %d keys of each type...\n", *bench)
generationBenchmark(*bench)
return
}
if *benchValidation != 0 {
fmt.Printf("Running key validation benchmark with %d keys of each type...\n", *benchValidation)
validationBenchmark(*benchValidation)
return
}
if *benchAll != 0 {
fmt.Printf("Running key generation and validation benchmarks with %d keys of each type...\n", *benchAll)
generationBenchmark(*benchAll)
validationBenchmark(*benchAll)
return
}
var started time.Time
if *t {
started = time.Now()
}
if *validate != "" {
k := *validate
var ki validator.KeyValidator
switch {
case len(k) == 12 && k[4:5] == "-":
ki = validator.Mod7ElevenCD{
First: k[0:4],
Second: k[5:12],
EnableCheckDigitRule: *strict11Digit,
}
case len(k) == 11 && k[3:4] == "-":
ki = validator.Mod7CD{
First: k[0:3],
Second: k[4:11],
Is95: *Is95,
}
case len(k) == 11 && *Is95:
ki = validator.Mod7CD{
First: k[0:3],
Second: k[4:11],
Is95: *Is95,
}
case len(k) == 23 && k[5:6] == "-" && k[9:10] == "-" && k[17:18] == "-" && len(k[18:]) == 5:
ki = validator.Mod7OEM{
First: k[0:5],
// nice
Second: k[6:9],
Third: k[10:17],
Fourth: k[18:],
Is95: *Is95,
}
default:
fmt.Println("Could not recognize key type")
return
}
err := ki.Validate()
switch {
default:
fmt.Printf("%s is valid\n", k)
case err != nil:
fmt.Printf("%s is invalid: %s\n", k, err)
}
return
}
if !*cd && !*elevencd && !*oem {
fmt.Println("You must specify what you want to do! Usage:")
flag.PrintDefaults()
return
}
oemkey := generator.Mod7OEM{}
ecdkey := generator.Mod7ElevenCD{}
cdkey := generator.Mod7CD{}
var k generator.KeyGenerator
for i := 0; i < *repeat; i++ {
switch {
case *elevencd:
ecdkey.Generate()
k = &ecdkey
case *cd:
cdkey.Generate()
k = &cdkey
case *oem:
oemkey.Generate()
k = &oemkey
}
fmt.Println(k.String())
}
if *t {
var ended time.Duration
switch {
case time.Since(started).Round(time.Second) > 1:
ended = time.Since(started).Round(time.Millisecond)
default:
ended = time.Since(started).Round(time.Microsecond)
}
if ended < 1 {
// Oh Windows...
fmt.Println("Could not display elapsed time correctly :(")
return
}
switch {
case *repeat > 1:
fmt.Printf("Took %s to generate %d keys.\n", ended, *repeat)
return
case *repeat == 1:
fmt.Printf("Took %s to generate %d key.\n", ended, *repeat)
return
}
}
}