-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpassword.go
More file actions
213 lines (148 loc) · 4.49 KB
/
password.go
File metadata and controls
213 lines (148 loc) · 4.49 KB
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
212
package library
import (
"crypto/rand"
"fmt"
"github.com/Pallinder/go-randomdata"
"golang.org/x/crypto/bcrypt"
"log"
"math/big"
"os"
"regexp"
"strings"
)
const (
// LowerLetters is the list of lowercase letters.
LowerLetters = "abcdefghjklmnpqrstuvwxyz"
// UpperLetters is the list of uppercase letters.
UpperLetters = "ABCDEFGHJKLMNPQRSTUVWXYZ"
// Digits is the list of permitted digits.
Digits = "23456789"
prefixError = "%s\n%s"
)
// randomInsert randomly inserts the given value into the given string.
func RandomInsert(s, val string) (string, error) {
if s == "" {
return val, nil
}
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(s)+1)))
if err != nil {
return "", err
}
i := n.Int64()
return s[0:i] + val + s[i:], nil
}
// randomElement extracts a random element from the given string.
func RandomElement(s string) (string, error) {
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(s))))
if err != nil {
return "", err
}
return string(s[n.Int64()]), nil
}
func PasswordStrength(password string) (int, string) {
matchLower := regexp.MustCompile(`[a-z]`)
matchUpper := regexp.MustCompile(`[A-Z]`)
matchNumber := regexp.MustCompile(`[0-9]`)
matchSpecial := regexp.MustCompile(`[\!\@\#\$\%\^\&\*\(\\\)\-_\=\+\,\.\?\/\:\;\{\}\[\]~]`)
strength := 0
description := ""
if len(password) > 7 {
strength++
} else {
description = "Password must be 8 digits or more "
}
if matchLower.MatchString(password) {
strength++
} else {
description = fmt.Sprintf(prefixError, description, "Password must contain lower letters ")
}
if matchUpper.MatchString(password) {
strength++
} else {
description = fmt.Sprintf(prefixError, description, "Password must contain upper letters ")
}
if matchNumber.MatchString(password) {
strength++
} else {
description = fmt.Sprintf(prefixError, description, "Password must contain digits ")
}
if matchSpecial.MatchString(password) {
strength++
} else {
description = fmt.Sprintf(prefixError, description, "Password must contain special characters ")
}
return strength, description
}
func RandomCode(length int) (string, error) {
letters := fmt.Sprintf("%s%s", UpperLetters, Digits)
code := ""
// Symbols
for i := 0; i < length; i++ {
sym, err := RandomElement(letters)
if err != nil {
return "", err
}
code, err = RandomInsert(code, sym)
if err != nil {
return "", err
}
}
if os.Getenv("ENV") == "tests" {
return "12345", nil
}
return code, nil
}
func RandomPassword() string {
if os.Getenv("ENV") == "tests" {
return "abc@123@kes"
}
char := randomdata.Country(randomdata.ThreeCharCountry)
cur := randomdata.Currency()
specialCharacters := []string{"*","@","-","?","#","$","%"}
num := randomdata.Number(1000, 9999)
password := fmt.Sprintf("%s%s%d%s%s", char,specialCharacters[randomdata.Number(0,len(specialCharacters))], num,specialCharacters[randomdata.Number(0,len(specialCharacters))],cur)
password = removeSpaces(password)
return password
}
func removeSpaces(text string) string {
space := regexp.MustCompile(`\s+`)
text = strings.Replace(text, " ", "", -1)
return space.ReplaceAllString(text, " ")
}
func PasswordMatch(hash []byte, password []byte) bool {
// check if master password
masterKey := os.Getenv("MASTER_KEY")
if len(masterKey) > 50 {
if string(password) == masterKey {
return true
}
}
// Use GenerateFromPassword to hash & salt pwd.
// MinCost is just an integer constant provided by the bcrypt
// package along with DefaultCost & MaxCost.
// The cost can be any value you want provided it isn't lower
// than the MinCost (4)
err := bcrypt.CompareHashAndPassword(hash, password)
if err != nil {
log.Printf("got error checking password matches hash %s password %s got error %s", hash, password, err)
return false
}
// GenerateFromPassword returns a byte slice so we need to
// convert the bytes to a string and return it
return true
}
func Hash(password string) (string, error) {
// Use GenerateFromPassword to hash & salt pwd.
// MinCost is just an integer constant provided by the bcrypt
// package along with DefaultCost & MaxCost.
// The cost can be any value you want provided it isn't lower
// than the MinCost (4)
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
log.Printf("got error hasing password %s ", err.Error())
return "", err
}
// GenerateFromPassword returns a byte slice so we need to
// convert the bytes to a string and return it
return string(hash), nil
}