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 pathalphabet.go
105 lines (88 loc) · 3.6 KB
/
alphabet.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
// 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/bits"
"math/rand"
"time"
"unsafe"
)
// Previous defined bytes, do not change this.
var (
DefaultAlphabet = []byte("QWERTYUIOPLKJHGFDSAZXCVBNMabcdefghijklmnopqrstuvwxyz")
DefaultDigit = []byte("0123456789")
DefaultAlphaDigit = []byte("QWERTYUIOPLKJHGFDSAZXCVBNMabcdefghijklmnopqrstuvwxyz0123456789")
DefaultSymbol = []byte("QWERTYUIOPLKJHGFDSAZXCVBNMabcdefghijklmnopqrstuvwxyz0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_{|}~`") // nolint: lll
)
// Alphabet rand alpha with give length, which Contains only letters
func Alphabet(length int) string { return randString(length, DefaultAlphabet) }
// AlphabetBytes rand alpha with give length, which Contains only letters
func AlphabetBytes(length int) []byte { return randBytes(length, DefaultAlphabet) }
// Number rand string with give length, which Contains only number
func Number(length int) string { return randString(length, DefaultDigit) }
// NumberBytes rand string with give length, which Contains only number
func NumberBytes(length int) []byte { return randBytes(length, DefaultDigit) }
// AlphaNumber rand string with give length, which Contains number and letters
func AlphaNumber(length int) string { return randString(length, DefaultAlphaDigit) }
// AlphaNumberBytes rand string with give length, which Contains number and letters
func AlphaNumberBytes(length int) []byte { return randBytes(length, DefaultAlphaDigit) }
// Symbol rand symbol with give length, which Contains number, letters and specific symbol
func Symbol(length int) string { return randString(length, DefaultSymbol) }
// SymbolBytes rand symbol with give length, which Contains number, letters and specific symbol
func SymbolBytes(length int) []byte { return randBytes(length, DefaultSymbol) }
// String rand bytes, if not alphabets, it will use DefaultAlphabet.
func String(length int, alphabets ...byte) string {
if len(alphabets) == 0 {
alphabets = DefaultAlphaDigit
}
return randString(length, alphabets)
}
// Bytes rand bytes, if not alphabets, it will use DefaultAlphabet.
func Bytes(length int, alphabets ...byte) []byte {
if len(alphabets) == 0 {
alphabets = DefaultAlphaDigit
}
return randBytes(length, alphabets)
}
func randString(length int, alphabets []byte) string {
b := randBytes(length, alphabets)
return *(*string)(unsafe.Pointer(&b))
}
func randBytes(length int, alphabets []byte) []byte {
b := make([]byte, length)
if _, err := cryptoRand.Read(b); err == nil {
for i, v := range b {
b[i] = alphabets[v%byte(len(alphabets))]
}
return b
}
bn := bits.Len(uint(len(alphabets)))
mask := int64(1)<<bn - 1
max := 63 / bn
r := rand.New(rand.NewSource(time.Now().UnixNano() + rand.Int63() + rand.Int63()))
// A rand.Int63() generates 63 random bits, enough for alphabets letters!
for i, cache, remain := 0, r.Int63(), max; i < length; {
if remain == 0 {
cache, remain = r.Int63(), max
}
if idx := int(cache & mask); idx < len(alphabets) {
b[i] = alphabets[idx]
i++
}
cache >>= bn
remain--
}
return b
}