-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcharset.go
132 lines (109 loc) · 4.28 KB
/
charset.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
package passit
import (
"io"
"unicode"
"golang.org/x/exp/utf8string"
)
type asciiGenerator struct{ s string }
// Digit is a Generator that returns a random numeric digit.
var Digit Generator = &asciiGenerator{"0123456789"}
// LatinLower is a Generator that returns a random lowercase character from the latin
// alphabet.
var LatinLower Generator = &asciiGenerator{"abcdefghijklmnopqrstuvwxyz"}
// LatinUpper is a Generator that returns a random uppercase character from the latin
// alphabet.
var LatinUpper Generator = &asciiGenerator{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}
// LatinMixed is a Generator that returns a random mixed-case characters from the
// latin alphabet.
var LatinMixed Generator = &asciiGenerator{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}
// LatinLowerDigit is a Generator that returns a random lowercase character from the
// latin alphabet or a numeric digit.
var LatinLowerDigit Generator = &asciiGenerator{"abcdefghijklmnopqrstuvwxyz0123456789"}
// LatinUpperDigit is a Generator that returns a random uppercase character from the
// latin alphabet or a numeric digit.
var LatinUpperDigit Generator = &asciiGenerator{"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}
// LatinMixedDigit is a Generator that returns a random mixed-case characters from
// the latin alphabet or a numeric digit.
var LatinMixedDigit Generator = &asciiGenerator{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}
// ASCIINoLettersNumbers is a Generator that returns a random ASCII punctuation or
// symbol character. The set of characters is
//
// !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
//
// This is ASCII characters from the Unicode P/Punctuation and S/Symbol categories,
// or alternatively ASCII graphic characters that are not letters, numbers or
// spaces.
var ASCIINoLettersNumbers Generator = &asciiGenerator{"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"}
// ASCIINoLetters is a Generator that returns a random ASCII number, punctuation or
// symbol character. The set of characters is
//
// !"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~
//
// This is ASCII characters from the Unicode N/Number, P/Punctuation and S/Symbol
// categories, or alternatively ASCII graphic characters that are not letters or
// spaces.
var ASCIINoLetters Generator = &asciiGenerator{"!\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`{|}~"}
// ASCIIGraphic is a Generator that returns a random ASCII graphic character
// excluding space. The set of characters is
//
// !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
//
// This is ASCII characters from the Unicode L/Letter, M/Mark, N/Number,
// P/Punctuation and S/Symbol categories.
var ASCIIGraphic Generator = &asciiGenerator{"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"}
func (ag *asciiGenerator) Password(r io.Reader) (string, error) {
idx, err := readIntN(r, len(ag.s))
if err != nil {
return "", err
}
return ag.s[idx : idx+1], nil
}
type runeGenerator utf8string.String
// FromCharset returns a Generator that returns a random rune from charset.
func FromCharset(charset string) Generator {
us := utf8string.NewString(charset)
switch us.RuneCount() {
case 0:
return Empty
case 1:
return String(charset)
default:
return (*runeGenerator)(us)
}
}
func (rg *runeGenerator) Password(r io.Reader) (string, error) {
us := (*utf8string.String)(rg)
idx, err := readIntN(r, us.RuneCount())
if err != nil {
return "", err
}
return us.Slice(idx, idx+1), nil
}
type unicodeGenerator struct {
tab *unicode.RangeTable
runes int
}
// FromRangeTable returns a Generator that returns a random rune from the
// unicode.RangeTable.
//
// The returned Generator is only deterministic if the same unicode.RangeTable is
// used. Be aware that the builtin unicode.X tables are subject to change as new
// versions of Unicode are released and are not suitable for deterministic use.
func FromRangeTable(tab *unicode.RangeTable) Generator {
runes := countRunesInTable(tab)
switch runes {
case 0:
return Empty
case 1:
return String(string(getRuneInTable(tab, 0)))
default:
return &unicodeGenerator{tab, runes}
}
}
func (ug *unicodeGenerator) Password(r io.Reader) (string, error) {
idx, err := readIntN(r, ug.runes)
if err != nil {
return "", err
}
return string(getRuneInTable(ug.tab, idx)), nil
}