This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexity.go
198 lines (175 loc) · 4.26 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
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
// Copyright [2022] [thinkgos] [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package extrand
import (
cryptorand "crypto/rand"
"math/big"
"math/rand"
"strings"
"time"
"unsafe"
)
const (
lower = `abcdefghijklmnopqrstuvwxyz`
upper = `ABCDEFGHIJKLMNOPQRSTUVWXYZ`
digit = `0123456789`
spec = ` !"#$%&'()*+,-./:;<=>?@[\]^_{|}~` + "`"
)
type complexityConfig struct {
lower bool
upper bool
digit bool
spec bool
meet bool
}
// Option for Complexity
type Option func(*complexityConfig)
// WithLower use lower
func WithLower() Option {
return func(c *complexityConfig) {
c.lower = true
}
}
// WithUpper use upper
func WithUpper() Option {
return func(c *complexityConfig) {
c.upper = true
}
}
// WithDigit use digit
func WithDigit() Option {
return func(c *complexityConfig) {
c.digit = true
}
}
// WithLowerUpperDigit use lower upper digit
func WithLowerUpperDigit() Option {
return func(c *complexityConfig) {
c.lower = true
c.upper = true
c.digit = true
}
}
// WithLowerUpper use lower upper
func WithLowerUpper() Option {
return func(c *complexityConfig) {
c.lower = true
c.upper = true
}
}
// WithSpec use spec
func WithSpec() Option {
return func(c *complexityConfig) {
c.spec = true
}
}
// WithAll use lower upper digit spec and enable meet complexity
func WithAll() Option {
return func(c *complexityConfig) {
c.lower = true
c.upper = true
c.digit = true
c.spec = true
c.meet = true
}
}
// WithMeet enable meet complexity
func WithMeet() Option {
return func(c *complexityConfig) {
c.meet = true
}
}
// Complexity setting
type Complexity struct {
chars string
requiredList []string
}
// NewComplexity new complexity with option
// default use lower, upper and digit to generate a random string, and not meets complexity.
func NewComplexity(opts ...Option) *Complexity {
c := complexityConfig{false, false, false, false, false}
for _, opt := range opts {
opt(&c)
}
co := &Complexity{}
if c.lower {
co.chars += lower
co.requiredList = append(co.requiredList, lower)
}
if c.upper {
co.chars += upper
co.requiredList = append(co.requiredList, upper)
}
if c.digit {
co.chars += digit
co.requiredList = append(co.requiredList, digit)
}
if c.spec {
co.chars += spec
co.requiredList = append(co.requiredList, spec)
}
if c.meet {
if co.chars == "" {
co.requiredList = []string{lower, upper, digit}
}
} else {
co.requiredList = nil
}
if co.chars == "" {
co.chars = lower + upper + digit
}
return co
}
// IsComplexEnough return True if s meets complexity settings
func (sf *Complexity) IsComplexEnough(s string) bool {
for _, chars := range sf.requiredList {
if !strings.ContainsAny(chars, s) {
return false
}
}
return true
}
// Generate a random string which is complex enough.
func (sf *Complexity) Generate(n int) string {
var idx int
buffer := make([]byte, n)
max := big.NewInt(int64(len(sf.chars)))
for {
for j := 0; j < n; j++ {
rnd, err := cryptorand.Int(cryptorand.Reader, max)
if err != nil {
idx = rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(sf.chars))
} else {
idx = int(rnd.Int64())
}
buffer[j] = sf.chars[idx]
}
v := *(*string)(unsafe.Pointer(&buffer))
if sf.IsComplexEnough(v) && v[:1] != " " && v[n-1:] != " " {
return v
}
}
}
// use lower, upper and digit to generate a random string, and meets complexity.
var complexity = NewComplexity(WithMeet())
// IsComplexEnough return True if s meets complexity settings.
// which use lower, upper and digit, and meets complexity.
func IsComplexEnough(s string) bool {
return complexity.IsComplexEnough(s)
}
// Generate a random string which is complex enough.
// which use lower, upper and digit, and meets complexity.
func Generate(n int) string {
return complexity.Generate(n)
}