Skip to content

Commit

Permalink
Merge pull request moby#15123 from ewindisch/seed-pkgrand-cryptorand
Browse files Browse the repository at this point in the history
Prefer crypto rand seed for pkg/rand
  • Loading branch information
tiborvass committed Jul 31, 2015
2 parents 8d2739d + 4742a39 commit 9a9a12b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/random/random.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package random

import (
cryptorand "crypto/rand"
"io"
"math"
"math/big"
"math/rand"
"sync"
"time"
Expand Down Expand Up @@ -36,8 +39,15 @@ func (r *lockedSource) Seed(seed int64) {
// NewSource returns math/rand.Source safe for concurrent use and initialized
// with current unix-nano timestamp
func NewSource() rand.Source {
var seed int64
if cryptoseed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64)); err != nil {
// This should not happen, but worst-case fallback to time-based seed.
seed = time.Now().UnixNano()
} else {
seed = cryptoseed.Int64()
}
return &lockedSource{
src: rand.NewSource(time.Now().UnixNano()),
src: rand.NewSource(seed),
}
}

Expand Down

0 comments on commit 9a9a12b

Please sign in to comment.