random: speed up String function for generating larger blocks

This commit is contained in:
Nick Craig-Wood 2023-11-23 19:58:22 +00:00
parent 251a8e3c39
commit 5d5473c8a5
2 changed files with 16 additions and 9 deletions

View file

@ -224,7 +224,7 @@ func (r *chargenReader) Read(p []byte) (n int, err error) {
func fileName() (name string) { func fileName() (name string) {
for { for {
length := randSource.Intn(maxFileNameLength-minFileNameLength) + minFileNameLength length := randSource.Intn(maxFileNameLength-minFileNameLength) + minFileNameLength
name = random.StringFn(length, randSource.Intn) name = random.StringFn(length, randSource)
if _, found := fileNames[name]; !found { if _, found := fileNames[name]; !found {
break break
} }

View file

@ -4,28 +4,35 @@ package random
import ( import (
cryptorand "crypto/rand" cryptorand "crypto/rand"
"encoding/base64" "encoding/base64"
"encoding/binary"
"fmt" "fmt"
mathrand "math/rand" "io"
) )
// StringFn create a random string for test purposes using the random // StringFn create a random string for test purposes using the random
// number generator function passed in. // number generator function passed in.
// //
// Do not use these for passwords. // Do not use these for passwords.
func StringFn(n int, randIntn func(n int) int) string { func StringFn(n int, randReader io.Reader) string {
const ( const (
vowel = "aeiou" vowel = "aeiou"
consonant = "bcdfghjklmnpqrstvwxyz" consonant = "bcdfghjklmnpqrstvwxyz"
digit = "0123456789" digit = "0123456789"
) )
pattern := []string{consonant, vowel, consonant, vowel, consonant, vowel, consonant, digit} var (
out := make([]byte, n) pattern = []string{consonant, vowel, consonant, vowel, consonant, vowel, consonant, digit}
p := 0 out = make([]byte, n)
p = 0
)
_, err := io.ReadFull(randReader, out)
if err != nil {
panic(fmt.Sprintf("internal error: failed to read from random reader: %v", err))
}
for i := range out { for i := range out {
source := pattern[p] source := pattern[p]
p = (p + 1) % len(pattern) p = (p + 1) % len(pattern)
out[i] = source[randIntn(len(source))] // this generation method means the distribution is slightly biased. However these
// strings are not for passwords so this is deemed OK.
out[i] = source[out[i]%byte(len(source))]
} }
return string(out) return string(out)
} }
@ -34,7 +41,7 @@ func StringFn(n int, randIntn func(n int) int) string {
// //
// Do not use these for passwords. // Do not use these for passwords.
func String(n int) string { func String(n int) string {
return StringFn(n, mathrand.Intn) return StringFn(n, cryptorand.Reader)
} }
// Password creates a crypto strong password which is just about // Password creates a crypto strong password which is just about