rand.go 473 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package core
  2. import (
  3. "crypto/rand"
  4. "encoding/binary"
  5. "io"
  6. mrand "math/rand"
  7. "github.com/cloudflare/cfssl/log"
  8. )
  9. var seeded bool
  10. func seed() error {
  11. if seeded {
  12. return nil
  13. }
  14. var buf [8]byte
  15. _, err := io.ReadFull(rand.Reader, buf[:])
  16. if err != nil {
  17. return err
  18. }
  19. n := int64(binary.LittleEndian.Uint64(buf[:]))
  20. mrand.Seed(n)
  21. seeded = true
  22. return nil
  23. }
  24. func init() {
  25. err := seed()
  26. if err != nil {
  27. log.Errorf("seeding mrand failed: %v", err)
  28. }
  29. }