This repository has been archived by the owner on May 26, 2021. It is now read-only.
/
complexity.go
81 lines (73 loc) · 2.19 KB
/
complexity.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
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package password
import (
"crypto/rand"
"math/big"
"strings"
)
var (
complexCharValues = map[string]string{
"lower": `abcdefghijklmnopqrstuvwxyz`,
"upper": `ABCDEFGHIJKLMNOPQRSTUVWXYZ`,
"digit": `0123456789`,
"spec": ` !"#$%&'()*+,-./:;<=>?@[\]^_{|}~` + "`",
}
complexChars = complexCharValues["lower"] + complexCharValues["upper"] + complexCharValues["digit"]
complexRequiredList []string
)
// SetupComplexity setup complexity with values
// value can set lower,upper,digit,spec
// default use lower, upper and digit to generate a random password, and not meets complexity.
func SetupComplexity(values []string) {
complexChars = ""
complexRequiredList = nil
if len(values) != 1 || values[0] != "off" {
for _, val := range values {
if chars, ok := complexCharValues[val]; ok {
complexChars += chars
complexRequiredList = append(complexRequiredList, chars)
}
}
if len(complexRequiredList) == 0 {
// No valid character classes found; use all classes as default
for _, chars := range complexCharValues {
complexChars += chars
complexRequiredList = append(complexRequiredList, chars)
}
}
}
if complexChars == "" {
// No complexities to check; provide a sensible default for password generation
complexChars = complexCharValues["lower"] + complexCharValues["upper"] + complexCharValues["digit"]
}
}
// IsComplexEnough return True if password meets complexity settings
func IsComplexEnough(pwd string) bool {
if len(complexChars) > 0 {
for _, chars := range complexRequiredList {
if !strings.ContainsAny(chars, pwd) {
return false
}
}
}
return true
}
// Generate a random password
func Generate(n int) (string, error) {
buffer := make([]byte, n)
max := big.NewInt(int64(len(complexChars)))
for {
for j := 0; j < n; j++ {
rnd, err := rand.Int(rand.Reader, max)
if err != nil {
return "", err
}
buffer[j] = complexChars[rnd.Int64()]
}
if IsComplexEnough(string(buffer)) && string(buffer[0]) != " " && string(buffer[n-1]) != " " {
return string(buffer), nil
}
}
}