-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.go
77 lines (67 loc) · 1.54 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
67
68
69
70
71
72
73
74
75
76
77
package passwordgen
import (
"math"
"math/rand"
"strconv"
"time"
)
var consonants = [21]string{"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"}
var vowels = [5]string{"a", "e", "i", "o", "u"}
var leetMap = map[string]int{
"a": 4,
"e": 3,
"i": 1,
"o": 0,
}
var symbols = [6]string{"!", "#", "$", "@", "%", "^"}
var hasLeetAlready = false
func init() {
rand.Seed(time.Now().UnixNano())
}
/*
Generate generates password
*/
func Generate(request GenerateRequest) string {
length := 8.0
password := ""
numDigits := 2
alphaNumChars := int(length) - numDigits
pairs := int(math.Floor(float64(alphaNumChars / 2)))
for pairs > 0 {
password += getPair(request.LeetMode, request.RandomUpperCase)
pairs--
}
password += getDigits(numDigits)
if request.AddSymbol {
password += symbols[getRand(len(symbols)-1)]
}
hasLeetAlready = false
return password
}
func getPair(leetMode bool, randomUpperCase bool) string {
return getConsonant() + getVowel(leetMode)
}
func getDigits(length int) string {
var digits = ""
for i := 0; i < length; i++ {
digits += getDigit()
}
return digits
}
func getDigit() string {
return strconv.Itoa(getRand(9))
}
func getConsonant() string {
return consonants[getRand(len(consonants)-1)]
}
func getVowel(leetMode bool) string {
var vowel = vowels[getRand(len(vowels)-1)]
if val, ok := leetMap[vowel]; ok && leetMode && !hasLeetAlready {
vowel = strconv.Itoa(val)
hasLeetAlready = true
}
return vowel
}
func getRand(max int) int {
return rand.Intn(max)
}