mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 16:57:51 +08:00
random string
update algorithm consolidate MakeRandomString, RandomString & GenerateCrytoString into one function
This commit is contained in:
@@ -2,7 +2,6 @@ package ncutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/gob"
|
||||
)
|
||||
|
||||
@@ -32,16 +31,3 @@ func ConvertBytesToKey(data []byte) (*[32]byte, error) {
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
// MakeRandomString - generates a random string of len n
|
||||
func MakeRandomString(n int) string {
|
||||
const validChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
result := make([]byte, n)
|
||||
if _, err := rand.Reader.Read(result); err != nil {
|
||||
return ""
|
||||
}
|
||||
for i, b := range result {
|
||||
result[i] = validChars[b%byte(len(validChars))]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
Reference in New Issue
Block a user