libs/rand: remove custom seed function (#7473)

This commit is contained in:
Sam Kleinman
2021-12-17 12:15:27 -05:00
committed by GitHub
parent ff615dda66
commit 9a0dbdbf13
3 changed files with 11 additions and 51 deletions
-43
View File
@@ -1,9 +1,6 @@
package rand
import (
crand "crypto/rand"
"encoding/binary"
"fmt"
mrand "math/rand"
)
@@ -11,37 +8,6 @@ const (
strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
)
func init() {
Reseed()
}
// NewRand returns a prng, that is seeded with OS randomness.
// The OS randomness is obtained from crypto/rand, however, like with any math/rand.Rand
// object none of the provided methods are suitable for cryptographic usage.
//
// Note that the returned instance of math/rand's Rand is not
// suitable for concurrent use by multiple goroutines.
//
// For concurrent use, call Reseed to reseed math/rand's default source and
// use math/rand's top-level convenience functions instead.
func NewRand() *mrand.Rand {
seed := crandSeed()
// nolint:gosec // G404: Use of weak random number generator
return mrand.New(mrand.NewSource(seed))
}
// Reseed conveniently re-seeds the default Source of math/rand with
// randomness obtained from crypto/rand.
//
// Note that this does not make math/rand suitable for cryptographic usage.
//
// Use math/rand's top-level convenience functions remain suitable
// for concurrent use by multiple goroutines.
func Reseed() {
seed := crandSeed()
mrand.Seed(seed)
}
// Str constructs a random alphanumeric string of given length
// from math/rand's global default Source.
func Str(length int) string {
@@ -78,12 +44,3 @@ func Bytes(n int) []byte {
}
return bs
}
func crandSeed() int64 {
var seed int64
err := binary.Read(crand.Reader, binary.BigEndian, &seed)
if err != nil {
panic(fmt.Sprintf("could nor read random seed from crypto/rand: %v", err))
}
return seed
}