-
Notifications
You must be signed in to change notification settings - Fork 0
/
strrand.go
133 lines (110 loc) · 4.25 KB
/
strrand.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
package strrand
import (
ibytes "bytes"
"crypto/rand"
"encoding/binary"
)
const (
// BinaryCharset defines a binary character set (0 and 1).
BinaryCharset = "01"
// OctalCharset defines an octal character set (0-7).
OctalCharset = "01234567"
// DecimalCharset defines a decimal character set (0-9).
DecimalCharset = "0123456789"
// HexadecimalCharset defines a hexadecimal character set (0-9, a-f).
HexadecimalCharset = "0123456789abcdef"
)
const (
// UppercaseCharset defines uppercase alphabetic characters (A-Z).
UppercaseCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// LowercaseCharset defines lowercase alphabetic characters (a-z).
LowercaseCharset = "abcdefghijklmnopqrstuvwxyz"
// SpecialCharset defines special characters.
SpecialCharset = "!@#$%^&*()-_=+[]{}|;:',.<>?/`~"
)
const (
// AlphabetCharset combines uppercase and lowercase alphabetic characters.
AlphabetCharset = UppercaseCharset + LowercaseCharset
// Base62Charset defines a Base62 character set (0-9, A-Z, a-z).
Base62Charset = DecimalCharset + AlphabetCharset
// Base64Charset defines a Base64 character set (0-9, A-Z, a-z, +, /).
Base64Charset = Base62Charset + "+/"
// DefaultCharset defines a default character set (Base62 + special characters).
DefaultCharset = Base62Charset + SpecialCharset
)
// Binary generates a random string of the specified length using the binary charset (01).
func Binary(length int) string {
return random(length, BinaryCharset)
}
// Octal generates a random string of the specified length using the octal charset (01234567).
func Octal(length int) string {
return random(length, OctalCharset)
}
// Decimal generates a random string of the specified length using the decimal charset (0123456789).
func Decimal(length int) string {
return random(length, DecimalCharset)
}
// Hexadecimal generates a random string of the specified length using the hexadecimal charset (0123456789abcdef).
func Hexadecimal(length int) string {
return random(length, HexadecimalCharset)
}
// CapitalLetters generates a random string of the specified length using uppercase letters.
func CapitalLetters(length int) string {
return random(length, UppercaseCharset)
}
// LowercaseLetters generates a random string of the specified length using lowercase letters.
func LowercaseLetters(length int) string {
return random(length, LowercaseCharset)
}
// SpecialLetters generates a random string of the specified length using special characters.
func SpecialLetters(length int) string {
return random(length, SpecialCharset)
}
// Base62 generates a random string of the specified length using the base62 charset (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz).
func Base62(length int) string {
return random(length, Base62Charset)
}
// Base64 generates a random string of the specified length using the base64 charset (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/).
func Base64(length int) string {
return random(length, Base64Charset)
}
// Letters generates a random string of the specified length using both uppercase and lowercase letters.
func Letters(length int) string {
return random(length, AlphabetCharset)
}
// DefaultString generates a random string of the specified length using the default charset (base62 + special characters).
func DefaultString(length int) string {
return random(length, DefaultCharset)
}
// random generates a random string of the specified length using the provided charset.
func random(length int, charset string) string {
if length <= 0 {
return ""
}
var buffer ibytes.Buffer
buffer.Grow(length)
charsetRune := []rune(charset)
charsetRuneLength := uint32(len(charsetRune))
for range length {
index := binary.BigEndian.Uint32(bytes(4)) % charsetRuneLength
buffer.WriteRune(charsetRune[index])
}
return buffer.String()
}
// bytes generates a byte slice of the specified length filled with random data.
func bytes(length int) []byte {
b := make([]byte, length)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return b
}
// String generates a random string of the specified length using a custom charset if provided,
// otherwise, it uses the default charset.
func String(length int, customCharset ...string) string {
if len(customCharset) == 0 {
return DefaultString(length)
}
return random(length, customCharset[0])
}