-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
67 lines (53 loc) · 1.57 KB
/
generator.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
package cryptorandomstring
import "strings"
type Generator struct {
length uint64
kind string
characters string
}
const defaultKind = "hex"
var (
urlSafeCharacters = strings.Split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~", "")
numericCharacters = strings.Split("0123456789", "")
distinguishableCharacters = strings.Split("CDEHKMPRTUWXY012458", "")
asciiPrintableCharacters = strings.Split("!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", "")
alphanumericCharacters = strings.Split("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "")
)
var allowedTypes = map[string]bool{
"": true,
"hex": true,
"base64": true,
"url-safe": true,
"numeric": true,
"distinguishable": true,
"ascii-printable": true,
"alphanumeric": true,
}
var defaultGenerator *Generator
func New() *Generator {
return &Generator{}
}
func (g *Generator) WithLength(length uint64) *Generator {
g.length = length
return g
}
func WithLength(length uint64) *Generator {
return defaultGenerator.WithLength(length)
}
func (g *Generator) WithKind(kind string) *Generator {
g.kind = kind
return g
}
func WithKind(kind string) *Generator {
return defaultGenerator.WithKind(kind)
}
func (g *Generator) WithCharacters(characters string) *Generator {
g.characters = characters
return g
}
func WithCharacters(characters string) *Generator {
return defaultGenerator.WithCharacters(characters)
}
func init() {
defaultGenerator = New()
}