-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
66 lines (58 loc) · 1.33 KB
/
password.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
package workers
import "math/rand"
const lowerStr = "abcdefghijklmnopqrstuvwxyz"
const upperStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numericStr = "0123456789"
const specialStr = "!@#$%&*+-=?"
const spaceStr = " "
// Password will generate a random password
// Minimum number length of 5 if less than
func Password(lower bool, upper bool, numeric bool, special bool, space bool, num int) string {
// Make sure the num minimun is at least 5
if num < 5 {
num = 5
}
i := 0
b := make([]byte, num)
var passString string
if lower {
passString += lowerStr
b[i] = lowerStr[rand.Int63()%int64(len(lowerStr))]
i++
}
if upper {
passString += upperStr
b[i] = upperStr[rand.Int63()%int64(len(upperStr))]
i++
}
if numeric {
passString += numericStr
b[i] = numericStr[rand.Int63()%int64(len(numericStr))]
i++
}
if special {
passString += specialStr
b[i] = specialStr[rand.Int63()%int64(len(specialStr))]
i++
}
if space {
passString += spaceStr
b[i] = spaceStr[rand.Int63()%int64(len(spaceStr))]
i++
}
// Set default if empty
if passString == "" {
passString = lowerStr + numericStr
}
// Loop through and add it up
for i <= num-1 {
b[i] = passString[rand.Int63()%int64(len(passString))]
i++
}
// Shuffle bytes
for i := range b {
j := rand.Intn(i + 1)
b[i], b[j] = b[j], b[i]
}
return string(b)
}