-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
47 lines (38 loc) · 989 Bytes
/
helpers.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
package crypto
import (
"fmt"
"math/rand"
"time"
)
func CombinePasswordAndKey(pass, key []byte) ([]byte, error) {
if len(pass) < 8 {
return nil, fmt.Errorf("master password must be at least 8 characters")
}
if len(key) != 16 {
return nil, fmt.Errorf("key was not of length 16")
}
for idx := 0; len(pass) < 16; idx++ {
pass = append(pass, pass[idx])
}
return append(pass[:16], key...), nil
}
func GeneratePassword(length int, numbers, specials bool) string {
const alphaValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numberValues = "1234567890"
const specialValues = "!@#$%^&*()_-+="
rand.Seed(time.Now().UnixNano())
pass := make([]byte, length)
values := alphaValues
switch {
case numbers && specials:
values += numberValues + specialValues
case numbers:
values += numberValues
case specials:
values += specialValues
}
for idx := range pass {
pass[idx] = values[rand.Int63()%int64(len(values))]
}
return string(pass)
}