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)
|
||||
}
|
||||
129
pkg/utils/randomvalues/randomvalues.go
Normal file
129
pkg/utils/randomvalues/randomvalues.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package randomvalues
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"fiskerinc.com/modules/utils/mt19937"
|
||||
)
|
||||
|
||||
type Generator struct {
|
||||
characters string
|
||||
counter int32
|
||||
maxUniform int32
|
||||
onceUniform sync.Once
|
||||
uniform *mt19937.UniformIntDistribution
|
||||
mt *mt19937.MT19937
|
||||
}
|
||||
|
||||
func NewGenerator(allowedchars string) Generator {
|
||||
if len(allowedchars) == 0 {
|
||||
allowedchars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
}
|
||||
instance := Generator{
|
||||
characters: allowedchars,
|
||||
maxUniform: 255,
|
||||
}
|
||||
val, _ := instance.GetUInt32()
|
||||
instance.counter = int32(val)
|
||||
|
||||
return instance
|
||||
}
|
||||
|
||||
func (g *Generator) GetBytes(length int) ([]byte, error) {
|
||||
result := make([]byte, length)
|
||||
_, err := rand.Read(result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (g *Generator) GetString(length int) (string, error) {
|
||||
result := make([]byte, length)
|
||||
for i := 0; i < length; i++ {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(g.characters))))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
result[i] = g.characters[num.Int64()]
|
||||
}
|
||||
|
||||
return string(result), nil
|
||||
}
|
||||
|
||||
func (g *Generator) GetUInt64() (uint64, error) {
|
||||
buf := make([]byte, 8)
|
||||
_, err := rand.Read(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return binary.LittleEndian.Uint64(buf), nil
|
||||
}
|
||||
|
||||
func (g *Generator) GetUInt32() (uint32, error) {
|
||||
buf := make([]byte, 4)
|
||||
_, err := rand.Read(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return binary.LittleEndian.Uint32(buf), nil
|
||||
}
|
||||
|
||||
func (g *Generator) GetInt(max int) int {
|
||||
result, _ := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
||||
return int(result.Int64())
|
||||
}
|
||||
|
||||
func (g *Generator) GetHex() (string, error) {
|
||||
value, err := g.GetUInt64()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%016s", strconv.FormatUint(value, 16)), nil
|
||||
}
|
||||
|
||||
func (g *Generator) getUniformIntDistribution() *mt19937.UniformIntDistribution {
|
||||
g.onceUniform.Do(func() {
|
||||
if g.uniform != nil {
|
||||
return
|
||||
}
|
||||
g.mt = mt19937.New()
|
||||
g.uniform = mt19937.DistInt64(g.mt, 0, 255)
|
||||
})
|
||||
|
||||
return g.uniform
|
||||
}
|
||||
|
||||
func (g *Generator) GetUnformDistInt() int {
|
||||
dist := g.getUniformIntDistribution()
|
||||
return int(dist.Int64())
|
||||
}
|
||||
|
||||
func (g *Generator) GetUniformDistHex() (string, error) {
|
||||
now := time.Now().UnixNano()
|
||||
result := uint64(now) << 32
|
||||
d := g.GetUnformDistInt() & 0xFF
|
||||
result |= uint64(d << 24)
|
||||
d = g.GetUnformDistInt() & 0xFF
|
||||
result |= uint64(d << 16)
|
||||
c := g.counter & 0xFFFF
|
||||
result |= uint64(c)
|
||||
g.counter++
|
||||
|
||||
return fmt.Sprintf("%016s", strconv.FormatUint(result, 16)), nil
|
||||
}
|
||||
|
||||
func (g *Generator) Close() {
|
||||
g.counter = 0
|
||||
g.characters = ""
|
||||
g.mt = nil
|
||||
g.uniform = nil
|
||||
}
|
||||
200
pkg/utils/randomvalues/randomvalues_test.go
Normal file
200
pkg/utils/randomvalues/randomvalues_test.go
Normal file
@@ -0,0 +1,200 @@
|
||||
package randomvalues_test
|
||||
|
||||
import (
|
||||
b64 "encoding/base64"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"fiskerinc.com/modules/testhelper"
|
||||
"fiskerinc.com/modules/utils/randomvalues"
|
||||
)
|
||||
|
||||
func TestRandomString(t *testing.T) {
|
||||
instance := randomvalues.NewGenerator("")
|
||||
|
||||
type TestCase struct {
|
||||
Length int
|
||||
}
|
||||
|
||||
tests := []TestCase{
|
||||
{
|
||||
Length: 12,
|
||||
},
|
||||
{
|
||||
Length: 32,
|
||||
},
|
||||
{
|
||||
Length: 10,
|
||||
},
|
||||
{
|
||||
Length: 100,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
name := fmt.Sprintf("%v Length", test.Length)
|
||||
result, err := instance.GetString(test.Length)
|
||||
if err != nil {
|
||||
t.Errorf(testhelper.TestErrorTemplate, name, nil, err)
|
||||
}
|
||||
if len(result) != test.Length {
|
||||
t.Errorf(testhelper.TestErrorTemplate, name, test.Length, len(result))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomUInt64(t *testing.T) {
|
||||
generated := map[uint64]int{}
|
||||
instance := randomvalues.NewGenerator("")
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
value, err := instance.GetUInt64()
|
||||
if err != nil {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "RandomeUInt64", nil, err)
|
||||
}
|
||||
|
||||
if result, ok := generated[value]; ok {
|
||||
generated[value] = result + 1
|
||||
} else {
|
||||
generated[value] = 1
|
||||
}
|
||||
}
|
||||
|
||||
for key, value := range generated {
|
||||
if value > 1 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, fmt.Sprintf("%v occurance", key), 1, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomHex(t *testing.T) {
|
||||
generated := map[string]int{}
|
||||
instance := randomvalues.NewGenerator("")
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
value, err := instance.GetHex()
|
||||
if err != nil {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "RandomeHex", nil, err)
|
||||
}
|
||||
|
||||
if result, ok := generated[value]; ok {
|
||||
generated[value] = result + 1
|
||||
} else {
|
||||
generated[value] = 1
|
||||
}
|
||||
}
|
||||
|
||||
for key, value := range generated {
|
||||
if value > 1 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, fmt.Sprintf("%v occurance", key), 1, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUniformHex(t *testing.T) {
|
||||
repeats := 0
|
||||
|
||||
generated := map[string]int{}
|
||||
instance := randomvalues.NewGenerator("")
|
||||
|
||||
for i := 0; i < 1000000; i++ {
|
||||
value, err := instance.GetUniformDistHex()
|
||||
if err != nil {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "UniformHex", nil, err)
|
||||
}
|
||||
|
||||
if result, ok := generated[value]; ok {
|
||||
generated[value] = result + 1
|
||||
repeats += 1
|
||||
} else {
|
||||
generated[value] = 1
|
||||
}
|
||||
}
|
||||
|
||||
for key, value := range generated {
|
||||
if value > 1 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, fmt.Sprintf("%v occurance", key), 1, value)
|
||||
}
|
||||
}
|
||||
|
||||
if repeats > 0 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "Repeated", 0, repeats)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomBytes(t *testing.T) {
|
||||
keysize := 16
|
||||
generated := map[string]int{}
|
||||
instance := randomvalues.NewGenerator("")
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
value, err := instance.GetBytes(keysize)
|
||||
if err != nil {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "RandomBytes", nil, err)
|
||||
}
|
||||
|
||||
val := b64.StdEncoding.EncodeToString(value)
|
||||
if result, ok := generated[val]; ok {
|
||||
generated[val] = result + 1
|
||||
} else {
|
||||
generated[val] = 1
|
||||
}
|
||||
}
|
||||
|
||||
for key, value := range generated {
|
||||
if value > 1 {
|
||||
t.Errorf(testhelper.TestErrorTemplate, fmt.Sprintf("%v occurance", key), 1, value)
|
||||
}
|
||||
decoded, err := b64.StdEncoding.DecodeString(key)
|
||||
if err != nil {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "Decode string error", nil, err)
|
||||
}
|
||||
if len(decoded) != keysize {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "Decoded string length", keysize, len(decoded))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRandomString(b *testing.B) {
|
||||
instance := randomvalues.NewGenerator("")
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := instance.GetString(32)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRandomUInt64(b *testing.B) {
|
||||
instance := randomvalues.NewGenerator("")
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := instance.GetUInt64()
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRandomHex(b *testing.B) {
|
||||
instance := randomvalues.NewGenerator("")
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := instance.GetHex()
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkUniformDistHex(b *testing.B) {
|
||||
instance := randomvalues.NewGenerator("")
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := instance.GetUniformDistHex()
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user