-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxkpasswd.go
211 lines (169 loc) · 3.86 KB
/
xkpasswd.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import "fmt"
import "log"
import "time"
import "strings"
import "strconv"
import "os"
import "io/ioutil"
import "math/rand"
import "path/filepath"
import "github.com/urfave/cli"
func readLines(path string) ([]string, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
lines := strings.Split(string(content), "\n")
return lines, nil
}
func getWords() (int, []string) {
executablePath, err := os.Executable()
if err != nil {
panic(err)
}
executableDir := filepath.Dir(executablePath)
var path string = strings.Replace("%s/xkpasswd-words.txt", "%s", executableDir, -1)
lines, err := readLines(path)
if err != nil {
log.Fatalf("readLines: %s", err)
return 0, nil
}
count := len(lines)
return count, lines
}
func getRandomWord() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
allWordsCount, allWords := getWords()
var randomNumber int = r.Intn(allWordsCount)
return allWords[randomNumber]
}
func getRandomDigit(numLimit int) int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var digit int = r.Intn(numLimit)
return digit
}
func generatePassword(pattern string, separator string) string {
words := patternToArray(pattern, separator)
return strings.Join(words, "")
}
func patternToArray(pattern string, separator string) []string {
array := make([]string, 0)
for i := 0; i < len(pattern); i++ {
if string(pattern[i]) == "w" {
array = append(array, getRandomWord())
}
if string(pattern[i]) == "d" {
array = append(array, strconv.Itoa(getRandomDigit(10)))
}
if string(pattern[i]) == "s" {
array = append(array, separator)
}
}
return array
}
func getSeparatorForComplexity(level int) string {
// enforce limits
if level < 1 {
level = 1
}
if level > 6 {
level = 6
}
// define allowed separators
var separatorPool string = "#.-=+_"
if level == 5 {
separatorPool = "#.-=+_!$*:~?"
}
if level == 6 {
separatorPool = "#.-=+_!$*:~?%^&;"
}
rtn := separatorPool[getRandomDigit(len(separatorPool))]
return string(rtn)
}
func getPatternForComplexity(level int) string {
// enforce limits
if level < 1 {
level = 1
}
if level > 6 {
level = 6
}
// define the pattern
var rtn string
if level == 1 {
rtn = "wsw"
}
if level == 2 {
rtn = "wswsw"
}
if level == 3 {
rtn = "wswswsdd"
}
if level == 4 {
rtn = "wswswswsdd"
}
if level == 5 {
rtn = "wswswswswsd"
}
if level == 6 {
rtn = "ddswswswswswsdd"
}
return rtn
}
func main() {
// TODO flag to specify list of words, fallback to default (+download if missing)
app := cli.NewApp()
app.Name = "xkpasswd"
app.Version = "0.0.1"
app.Usage = "Memorable password generator"
var inputComplexity int
var inputPattern string
var inputSeparator string
var inputNumber int
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "complexity, c",
Value: 2,
Usage: "Define complexity (1-6)",
Destination: &inputComplexity,
},
cli.StringFlag{
Name: "pattern, p",
Value: "",
Usage: "Define pattern (w = word, d = digit, s = separator)",
Destination: &inputPattern,
},
cli.StringFlag{
Name: "separator, s",
Value: "",
Usage: "Define separator character",
Destination: &inputSeparator,
},
cli.IntFlag{
Name: "number, n",
Value: 1,
Usage: "Define number of passwords to generate",
Destination: &inputNumber,
},
}
app.Action = func(c *cli.Context) error {
var pattern string
var separator string
if len(inputPattern) > 0 {
pattern = inputPattern
} else {
pattern = getPatternForComplexity(inputComplexity)
}
if len(inputSeparator) > 0 {
separator = inputSeparator
} else {
separator = getSeparatorForComplexity(inputComplexity)
}
for i := 0; i < inputNumber; i++ {
fmt.Println(generatePassword(pattern, separator))
}
return nil
}
app.Run(os.Args)
}