mirror of
https://github.com/mjl-/mox.git
synced 2025-07-10 10:34:40 +03:00
fix bug with concurrent math/rand.Rand.Read
firstly by using crypto/rand in those cases. and secondly by putting a lock around the Read (though it isn't used at the moment). found while working while implementing sending tls reports.
This commit is contained in:
19
mox-/rand.go
19
mox-/rand.go
@ -5,11 +5,24 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
mathrand "math/rand"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// NewRand returns a new PRNG seeded with random bytes from crypto/rand.
|
||||
func NewRand() *mathrand.Rand {
|
||||
return mathrand.New(mathrand.NewSource(CryptoRandInt()))
|
||||
type rand struct {
|
||||
*mathrand.Rand
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// NewPseudoRand returns a new PRNG seeded with random bytes from crypto/rand.
|
||||
func NewPseudoRand() *rand {
|
||||
return &rand{Rand: mathrand.New(mathrand.NewSource(CryptoRandInt()))}
|
||||
}
|
||||
|
||||
// Read can be called concurrently.
|
||||
func (r *rand) Read(buf []byte) (int, error) {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
return r.Rand.Read(buf)
|
||||
}
|
||||
|
||||
// CryptoRandInt returns a cryptographically random number.
|
||||
|
Reference in New Issue
Block a user