added pseudorandom generator

This commit is contained in:
Gani Georgiev
2022-11-06 15:26:34 +02:00
parent 46dc6cc47c
commit 4cddb6b5cb
9 changed files with 89 additions and 47 deletions
+33 -9
View File
@@ -1,18 +1,18 @@
package security
import (
"crypto/rand"
cryptoRand "crypto/rand"
"math/big"
mathRand "math/rand"
)
// RandomString generates a random string with the specified length.
//
// The generated string is cryptographically random and matches
// [A-Za-z0-9]+ (aka. it's transparent to URL-encoding).
func RandomString(length int) string {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
const defaultRandomAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
return RandomStringWithAlphabet(length, alphabet)
// RandomString generates a cryptographically random string with the specified length.
//
// The generated string matches [A-Za-z0-9]+ and it's transparent to URL-encoding.
func RandomString(length int) string {
return RandomStringWithAlphabet(length, defaultRandomAlphabet)
}
// RandomStringWithAlphabet generates a cryptographically random string
@@ -24,7 +24,7 @@ func RandomStringWithAlphabet(length int, alphabet string) string {
max := big.NewInt(int64(len(alphabet)))
for i := range b {
n, err := rand.Int(rand.Reader, max)
n, err := cryptoRand.Int(cryptoRand.Reader, max)
if err != nil {
panic(err)
}
@@ -33,3 +33,27 @@ func RandomStringWithAlphabet(length int, alphabet string) string {
return string(b)
}
// RandomString generates a pseudorandom string with the specified length.
//
// The generated string matches [A-Za-z0-9]+ and it's transparent to URL-encoding.
//
// For a cryptographically random string (but a little bit slower) use PseudoRandomString instead.
func PseudoRandomString(length int) string {
return RandomStringWithAlphabet(length, defaultRandomAlphabet)
}
// PseudoRandomStringWithAlphabet generates a pseudorandom string
// with the specified length and characters set.
//
// For a cryptographically random (but a little bit slower) use RandomStringWithAlphabet instead.
func PseudoRandomStringWithAlphabet(length int, alphabet string) string {
b := make([]byte, length)
max := len(alphabet)
for i := range b {
b[i] = alphabet[mathRand.Intn(max)]
}
return string(b)
}