Skip to content

Commit 6d6ba3a

Browse files
committed
random string
update algorithm consolidate MakeRandomString, RandomString & GenerateCrytoString into one function
1 parent ba396f1 commit 6d6ba3a

File tree

6 files changed

+19
-54
lines changed

6 files changed

+19
-54
lines changed

logic/enrollmentkey.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/gravitl/netmaker/database"
1111
"github.com/gravitl/netmaker/models"
12-
"github.com/gravitl/netmaker/netclient/ncutils"
1312
)
1413

1514
// EnrollmentErrors - struct for holding EnrollmentKey error messages
@@ -190,9 +189,9 @@ func getUniqueEnrollmentID() (string, error) {
190189
if err != nil {
191190
return "", err
192191
}
193-
newID := ncutils.MakeRandomString(models.EnrollmentKeyLength)
192+
newID := RandomString(models.EnrollmentKeyLength)
194193
for _, ok := currentKeys[newID]; ok; {
195-
newID = ncutils.MakeRandomString(models.EnrollmentKeyLength)
194+
newID = RandomString(models.EnrollmentKeyLength)
196195
}
197196
return newID, nil
198197
}

logic/jwts.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ var jwtSecretKey []byte
1919
func SetJWTSecret() {
2020
currentSecret, jwtErr := FetchJWTSecret()
2121
if jwtErr != nil {
22-
newValue, err := GenerateCryptoString(64)
23-
if err != nil {
24-
logger.FatalLog("something went wrong when generating JWT signature")
25-
}
22+
newValue := RandomString(64)
2623
jwtSecretKey = []byte(newValue) // 512 bit random password
2724
if err := StoreJWTSecret(string(jwtSecretKey)); err != nil {
2825
logger.FatalLog("something went wrong when configuring JWT authentication")

logic/util.go

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
package logic
33

44
import (
5-
crand "crypto/rand"
5+
"crypto/rand"
6+
"encoding/base32"
67
"encoding/base64"
78
"encoding/json"
8-
"math/big"
9-
"math/rand"
109
"net"
1110
"os"
1211
"strings"
1312
"time"
1413

1514
"github.com/c-robinson/iplib"
1615
"github.com/gravitl/netmaker/database"
16+
"github.com/gravitl/netmaker/logger"
1717
)
1818

1919
// IsBase64 - checks if a string is in base64 format
@@ -68,32 +68,15 @@ func SetNetworkNodesLastModified(networkName string) error {
6868
return nil
6969
}
7070

71-
// GenerateCryptoString - generates random string of n length
72-
func GenerateCryptoString(n int) (string, error) {
73-
const chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
74-
ret := make([]byte, n)
75-
for i := range ret {
76-
num, err := crand.Int(crand.Reader, big.NewInt(int64(len(chars))))
77-
if err != nil {
78-
return "", err
79-
}
80-
ret[i] = chars[num.Int64()]
81-
}
82-
83-
return string(ret), nil
84-
}
85-
8671
// RandomString - returns a random string in a charset
8772
func RandomString(length int) string {
88-
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
89-
90-
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
91-
92-
b := make([]byte, length)
93-
for i := range b {
94-
b[i] = charset[seededRand.Intn(len(charset))]
73+
randombytes := make([]byte, length)
74+
_, err := rand.Read(randombytes)
75+
if err != nil {
76+
logger.Log(0, "random string", err.Error())
77+
return ""
9578
}
96-
return string(b)
79+
return base32.StdEncoding.EncodeToString(randombytes)[:length]
9780
}
9881

9982
// StringSliceContains - sees if a string slice contains a string element

netclient/ncutils/netclientutils_test.go renamed to logic/util_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ncutils
1+
package logic
22

33
import (
44
"strings"
@@ -7,20 +7,20 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
func TestMakeRandomString(t *testing.T) {
10+
func TestRandomString(t *testing.T) {
1111
for testCase := 0; testCase < 100; testCase++ {
1212
for size := 2; size < 2058; size++ {
13-
if length := len(MakeRandomString(size)); length != size {
13+
if length := len(RandomString(size)); length != size {
1414
t.Fatalf("expected random string of size %d, got %d instead", size, length)
1515
}
1616
}
1717
}
1818
}
1919

2020
func TestMakeRandomStringValid(t *testing.T) {
21-
lengthStr := MakeRandomString(10)
21+
lengthStr := RandomString(10)
2222
assert.Equal(t, len(lengthStr), 10)
23-
validMqID := MakeRandomString(23)
23+
validMqID := RandomString(23)
2424
assert.False(t, strings.Contains(validMqID, "#"))
2525
assert.False(t, strings.Contains(validMqID, "!"))
2626
assert.False(t, strings.Contains(validMqID, "\""))

mq/mq.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
mqtt "github.com/eclipse/paho.mqtt.golang"
1010
"github.com/gravitl/netmaker/logger"
11-
"github.com/gravitl/netmaker/netclient/ncutils"
11+
"github.com/gravitl/netmaker/logic"
1212
"github.com/gravitl/netmaker/servercfg"
1313
)
1414

@@ -27,7 +27,7 @@ var mqclient mqtt.Client
2727
func setMqOptions(user, password string, opts *mqtt.ClientOptions) {
2828
broker, _ := servercfg.GetMessageQueueEndpoint()
2929
opts.AddBroker(broker)
30-
id := ncutils.MakeRandomString(23)
30+
id := logic.RandomString(23)
3131
opts.ClientID = id
3232
opts.SetUsername(user)
3333
opts.SetPassword(password)

netclient/ncutils/netclientutils.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ncutils
22

33
import (
44
"bytes"
5-
"crypto/rand"
65
"encoding/gob"
76
)
87

@@ -32,16 +31,3 @@ func ConvertBytesToKey(data []byte) (*[32]byte, error) {
3231
}
3332
return result, err
3433
}
35-
36-
// MakeRandomString - generates a random string of len n
37-
func MakeRandomString(n int) string {
38-
const validChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
39-
result := make([]byte, n)
40-
if _, err := rand.Reader.Read(result); err != nil {
41-
return ""
42-
}
43-
for i, b := range result {
44-
result[i] = validChars[b%byte(len(validChars))]
45-
}
46-
return string(result)
47-
}

0 commit comments

Comments
 (0)