|
@@ -2,22 +2,24 @@ package random
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"math/rand"
|
|
"math/rand"
|
|
|
|
|
+ "strings"
|
|
|
"time"
|
|
"time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
type (
|
|
|
Random struct {
|
|
Random struct {
|
|
|
- charset Charset
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- Charset string
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// Charsets
|
|
|
const (
|
|
const (
|
|
|
- Alphanumeric Charset = Alphabetic + Numeric
|
|
|
|
|
- Alphabetic Charset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
- Numeric Charset = "0123456789"
|
|
|
|
|
- Hex Charset = Numeric + "abcdef"
|
|
|
|
|
|
|
+ Uppercase string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
+ Lowercase = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
+ Alphabetic = Uppercase + Lowercase
|
|
|
|
|
+ Numeric = "0123456789"
|
|
|
|
|
+ Alphanumeric = Alphabetic + Numeric
|
|
|
|
|
+ Symbols = "`" + `~!@#$%^&*()-_+={}[]|\;:"<>,./?`
|
|
|
|
|
+ Hex = Numeric + "abcdef"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
var (
|
|
@@ -26,27 +28,21 @@ var (
|
|
|
|
|
|
|
|
func New() *Random {
|
|
func New() *Random {
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
- return &Random{
|
|
|
|
|
- charset: Alphanumeric,
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func (r *Random) SetCharset(c Charset) {
|
|
|
|
|
- r.charset = c
|
|
|
|
|
|
|
+ return new(Random)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (r *Random) String(length uint8) string {
|
|
|
|
|
|
|
+func (r *Random) String(length uint8, charsets ...string) string {
|
|
|
|
|
+ charset := strings.Join(charsets, "")
|
|
|
|
|
+ if charset == "" {
|
|
|
|
|
+ charset = Alphanumeric
|
|
|
|
|
+ }
|
|
|
b := make([]byte, length)
|
|
b := make([]byte, length)
|
|
|
for i := range b {
|
|
for i := range b {
|
|
|
- b[i] = r.charset[rand.Int63()%int64(len(r.charset))]
|
|
|
|
|
|
|
+ b[i] = charset[rand.Int63()%int64(len(charset))]
|
|
|
}
|
|
}
|
|
|
return string(b)
|
|
return string(b)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func SetCharset(c Charset) {
|
|
|
|
|
- global.SetCharset(c)
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func String(length uint8) string {
|
|
|
|
|
- return global.String(length)
|
|
|
|
|
|
|
+func String(length uint8, charsets ...string) string {
|
|
|
|
|
+ return global.String(length, charsets...)
|
|
|
}
|
|
}
|