random string

update algorithm
consolidate MakeRandomString, RandomString & GenerateCrytoString into
one function
This commit is contained in:
Matthew R Kasun
2023-05-02 13:28:00 -04:00
parent ba396f1b5b
commit 6d6ba3aa7d
6 changed files with 19 additions and 54 deletions

View File

@@ -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)
}