Remove CRand* from go-common
This commit is contained in:
parent
1559ae1ac9
commit
461c3b9785
17
random.go
17
random.go
|
@ -2,7 +2,6 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
"encoding/hex"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -12,8 +11,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Seed math/rand with "secure" int64
|
b := cRandBytes(8)
|
||||||
b := CRandBytes(8)
|
|
||||||
var seed uint64
|
var seed uint64
|
||||||
for i := 0; i < 8; i++ {
|
for i := 0; i < 8; i++ {
|
||||||
seed |= uint64(b[i])
|
seed |= uint64(b[i])
|
||||||
|
@ -127,10 +125,10 @@ func RandBytes(n int) []byte {
|
||||||
return bs
|
return bs
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
// NOTE: This relies on the os's random number generator.
|
||||||
// CRand* methods are crypto safe.
|
// For real security, we should salt that with some seed.
|
||||||
|
// See github.com/tendermint/go-crypto for a more secure reader.
|
||||||
func CRandBytes(numBytes int) []byte {
|
func cRandBytes(numBytes int) []byte {
|
||||||
b := make([]byte, numBytes)
|
b := make([]byte, numBytes)
|
||||||
_, err := crand.Read(b)
|
_, err := crand.Read(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -138,8 +136,3 @@ func CRandBytes(numBytes int) []byte {
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandHex(24) gives 96 bits of randomness, strong enough for most purposes.
|
|
||||||
func CRandHex(numDigits int) string {
|
|
||||||
return hex.EncodeToString(CRandBytes(numDigits / 2))
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue