This repository was archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandomx.go
More file actions
94 lines (82 loc) · 2.23 KB
/
Copy pathrandomx.go
File metadata and controls
94 lines (82 loc) · 2.23 KB
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
package randomx
import (
"math/rand"
"time"
)
// Character sets
const (
LowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
UppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Digits = "0123456789"
Symbols = "!@#$%^&*()-_=+[]{}|;:,.<>?/~"
)
// Predefined GenerateOptions for commonly used character sets
var (
OptionAllChars = GenerateOptions{
UseLowercase: true,
UseUppercase: true,
UseDigits: true,
UseSymbols: true,
}
OptionLetters = GenerateOptions{
UseLowercase: true,
UseUppercase: true,
UseDigits: false,
UseSymbols: false,
}
OptionDigitsOnly = GenerateOptions{
UseLowercase: false,
UseUppercase: false,
UseDigits: true,
UseSymbols: false,
}
)
// Interface defines the methods for generating random strings.
type Interface interface {
GenerateRandomString(length int, charSet string) string
GenerateRandomStringFromBuiltin(length int, options GenerateOptions) string
}
// provider is the random string generator.
type provider struct {
randSrc *rand.Rand
}
// New creates a new random string provider.
func New() Interface {
seed := time.Now().UnixNano()
source := rand.NewSource(seed)
return &provider{
randSrc: rand.New(source),
}
}
// GenerateRandomString generates a random string of the specified length using the given character set.
func (p *provider) GenerateRandomString(length int, charSet string) string {
result := make([]byte, length)
for i := range result {
result[i] = charSet[p.randSrc.Intn(len(charSet))]
}
return string(result)
}
// GenerateOptions is the parameter options for GenerateRandomStringFromBuiltin.
type GenerateOptions struct {
UseLowercase bool
UseUppercase bool
UseDigits bool
UseSymbols bool
}
// GenerateRandomStringFromBuiltin generates a random string of the specified length using predefined or customized character sets.
func (p *provider) GenerateRandomStringFromBuiltin(length int, options GenerateOptions) string {
charSetBuilder := ""
if options.UseLowercase {
charSetBuilder += LowercaseLetters
}
if options.UseUppercase {
charSetBuilder += UppercaseLetters
}
if options.UseDigits {
charSetBuilder += Digits
}
if options.UseSymbols {
charSetBuilder += Symbols
}
return p.GenerateRandomString(length, charSetBuilder)
}