Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
44
pkg/utils/randomvalues/noncryptorandomvalues.go
Normal file
44
pkg/utils/randomvalues/noncryptorandomvalues.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package randomvalues
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NonCryptoGenerator struct {
|
||||
characters string
|
||||
*rand.Rand
|
||||
*sync.Mutex
|
||||
}
|
||||
|
||||
// Only set seed when you need a non-random value
|
||||
func NewNonCryptoGenerator(allowedchars string, seed int64) NonCryptoGenerator {
|
||||
if len(allowedchars) == 0 {
|
||||
allowedchars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
}
|
||||
instance := NonCryptoGenerator{
|
||||
characters: allowedchars,
|
||||
}
|
||||
|
||||
if seed != 0 {
|
||||
instance.Rand = rand.New(rand.NewSource(seed))
|
||||
}else{
|
||||
instance.Rand = rand.New(rand.NewSource(time.Now().UnixMilli()))
|
||||
}
|
||||
instance.Mutex = &sync.Mutex{}
|
||||
|
||||
return instance
|
||||
}
|
||||
|
||||
func (g *NonCryptoGenerator) GetString(length int) (string) {
|
||||
g.Lock()
|
||||
defer g.Unlock()
|
||||
result := make([]byte, length)
|
||||
for i := 0; i < length; i++ {
|
||||
num := g.Intn(len(g.characters))
|
||||
result[i] = g.characters[num]
|
||||
}
|
||||
|
||||
return string(result)
|
||||
}
|
||||
Reference in New Issue
Block a user