random.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // Package rand implements functions similar to math/rand in the standard
  7. // library, but on top of a secure random number generator.
  8. package rand
  9. import (
  10. "crypto/md5"
  11. cryptoRand "crypto/rand"
  12. "encoding/binary"
  13. "io"
  14. mathRand "math/rand"
  15. )
  16. // Reader is the standard crypto/rand.Reader, re-exported for convenience
  17. var Reader = cryptoRand.Reader
  18. // randomCharset contains the characters that can make up a randomString().
  19. const randomCharset = "2345679abcdefghijkmnopqrstuvwxyzACDEFGHJKLMNPQRSTUVWXYZ"
  20. var (
  21. // defaultSecureSource is a concurrency safe math/rand.Source with a
  22. // cryptographically sound base.
  23. defaltSecureSource = newSecureSource()
  24. // defaultSecureRand is a math/rand.Rand based on the secure source.
  25. defaultSecureRand = mathRand.New(defaltSecureSource)
  26. )
  27. // String returns a strongly random string of characters (taken from
  28. // randomCharset) of the specified length. The returned string contains ~5.8
  29. // bits of entropy per character, due to the character set used.
  30. func String(l int) string {
  31. bs := make([]byte, l)
  32. for i := range bs {
  33. bs[i] = randomCharset[defaultSecureRand.Intn(len(randomCharset))]
  34. }
  35. return string(bs)
  36. }
  37. // Int63 returns a strongly random int63
  38. func Int63() int64 {
  39. return defaltSecureSource.Int63()
  40. }
  41. // Int64 returns a strongly random int64
  42. func Int64() int64 {
  43. var bs [8]byte
  44. _, err := io.ReadFull(cryptoRand.Reader, bs[:])
  45. if err != nil {
  46. panic("randomness failure: " + err.Error())
  47. }
  48. return int64(binary.BigEndian.Uint64(bs[:]))
  49. }
  50. // Intn returns, as an int, a non-negative strongly random number in [0,n).
  51. // It panics if n <= 0.
  52. func Intn(n int) int {
  53. return defaultSecureRand.Intn(n)
  54. }
  55. // SeedFromBytes calculates a weak 64 bit hash from the given byte slice,
  56. // suitable for use a predictable random seed.
  57. func SeedFromBytes(bs []byte) int64 {
  58. h := md5.New()
  59. h.Write(bs)
  60. s := h.Sum(nil)
  61. // The MD5 hash of the byte slice is 16 bytes long. We interpret it as two
  62. // uint64s and XOR them together.
  63. return int64(binary.BigEndian.Uint64(s[0:]) ^ binary.BigEndian.Uint64(s[8:]))
  64. }