-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
193 lines (175 loc) · 6.13 KB
/
config.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
// SPDX-FileCopyrightText: 2021-2024 Winni Neessen <[email protected]>
//
// SPDX-License-Identifier: MIT
package apg
// List of default values for Config instances
const (
// DefaultMinLength reflects the default minimum length of a generated password
DefaultMinLength int64 = 12
// DefaultMaxLength reflects the default maximum length of a generated password
DefaultMaxLength int64 = 20
// DefaultBinarySize is the default byte size for generating binary random bytes
DefaultBinarySize int64 = 32
// DefaultMode sets the default character set mode bitmask to a combination of
// lower- and upper-case characters as well as numbers
DefaultMode ModeMask = ModeLowerCase | ModeNumeric | ModeUpperCase
// DefaultNumberPass reflects the default amount of passwords returned by the generator
DefaultNumberPass int64 = 6
)
// Config represents the apg.Generator config parameters
type Config struct {
// Algorithm sets the Algorithm used for the password generation
Algorithm Algorithm
// BinaryHexMode if set will output the hex representation of the generated
// binary random string
BinaryHexMode bool
// BinaryNewline if set will print out a new line in AlgoBinary mode
BinaryNewline bool
// CheckHIBP sets a flag if the generated password has to be checked
// against the HIBP pwned password database
CheckHIBP bool
// ExcludeChars is a list of characters that should be excluded from
// generated passwords
ExcludeChars string
// FixedLength sets a fixed length for generated passwords and ignores
// the MinLength and MaxLength values
FixedLength int64
// MaxLength sets the maximum length for a generated password
MaxLength int64
// MinLength sets the minimum length for a generated password
MinLength int64
// MinLowerCase represents the minimum amount of lower-case characters that have
// to be part of the generated password
MinLowerCase int64
// MinNumeric represents the minimum amount of numeric characters that have
// to be part of the generated password
MinNumeric int64
// MinSpecial represents the minimum amount of special characters that have
// to be part of the generated password
MinSpecial int64
// MinUpperCase represents the minimum amount of upper-case characters that have
// to be part of the generated password
MinUpperCase int64
// MobileGrouping indicates if the generated password should be grouped in a
// mobile-friendly manner
MobileGrouping bool
// Mode holds the different character modes for the Random algorithm
Mode ModeMask
// NumberPass sets the number of passwords that are generated
// and returned by the generator
NumberPass int64
// SpellPassword if set will spell the generated passwords in the phonetic alphabet
SpellPassword bool
// SpellPronounceable if set will spell the generated pronounceable passwords in
// as its corresponding syllables
SpellPronounceable bool
}
// Option is a function that can override default Config settings
type Option func(*Config)
// NewConfig creates a new Config instance and pre-fills it with sane
// default settings. The Config is returned as pointer value
func NewConfig(opts ...Option) *Config {
config := &Config{
MaxLength: DefaultMaxLength,
MinLength: DefaultMinLength,
Mode: DefaultMode,
NumberPass: DefaultNumberPass,
}
// Override defaults with optionally provided config.Option functions
for _, opt := range opts {
if opt == nil {
continue
}
opt(config)
}
return config
}
// WithAlgorithm overrides the algorithm mode for the password generation
func WithAlgorithm(algo Algorithm) Option {
return func(config *Config) {
config.Algorithm = algo
}
}
// WithBinaryHexMode sets the hex mode for the AlgoBinary
func WithBinaryHexMode() Option {
return func(config *Config) {
config.BinaryHexMode = true
}
}
// WithExcludeChars sets a list of characters to be excluded in the generated
// passwords
func WithExcludeChars(chars string) Option {
return func(config *Config) {
config.ExcludeChars = chars
}
}
// WithFixedLength sets a fixed password length
func WithFixedLength(length int64) Option {
return func(config *Config) {
config.FixedLength = length
}
}
// WithMinLength overrides the minimum password length
func WithMinLength(length int64) Option {
return func(config *Config) {
config.MinLength = length
}
}
// WithMinLowercase sets the minimum amount of lowercase characters that
// the generated password should contain
//
// CAVEAT: using to high values with this option, can lead to extraordinary
// calculation times, resulting in apg-go to never finish
func WithMinLowercase(amount int64) Option {
return func(config *Config) {
config.MinLowerCase = amount
}
}
// WithMinNumeric sets the minimum amount of numeric characters that
// the generated password should contain
//
// CAVEAT: using to high values with this option, can lead to extraordinary
// calculation times, resulting in apg-go to never finish
func WithMinNumeric(amount int64) Option {
return func(config *Config) {
config.MinNumeric = amount
}
}
// WithMinSpecial sets the minimum amount of special characters that
// the generated password should contain
//
// CAVEAT: using to high values with this option, can lead to extraordinary
// calculation times, resulting in apg-go to never finish
func WithMinSpecial(amount int64) Option {
return func(config *Config) {
config.MinSpecial = amount
}
}
// WithMinUppercase sets the minimum amount of uppercase characters that
// the generated password should contain
//
// CAVEAT: using to high values with this option, can lead to extraordinary
// calculation times, resulting in apg-go to never finish
func WithMinUppercase(amount int64) Option {
return func(config *Config) {
config.MinUpperCase = amount
}
}
// WithMaxLength overrides the maximum password length
func WithMaxLength(length int64) Option {
return func(config *Config) {
config.MaxLength = length
}
}
// WithMobileGrouping enables the mobile-friendly character grouping for AlgoRandom
func WithMobileGrouping() Option {
return func(config *Config) {
config.MobileGrouping = true
}
}
// WithModeMask overrides the default mode mask for the random algorithm
func WithModeMask(mask ModeMask) Option {
return func(config *Config) {
config.Mode = mask
}
}