random.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "io"
  11. mathRand "math/rand"
  12. "reflect"
  13. )
  14. // Reader is the standard crypto/rand.Reader with added buffering.
  15. var Reader = defaultSecureSource
  16. func Read(p []byte) (int, error) {
  17. return io.ReadFull(defaultSecureSource, p)
  18. }
  19. // randomCharset contains the characters that can make up a rand.String().
  20. const randomCharset = "2345679abcdefghijkmnopqrstuvwxyzACDEFGHJKLMNPQRSTUVWXYZ"
  21. var (
  22. // defaultSecureSource is a concurrency-safe, cryptographically secure
  23. // math/rand.Source.
  24. defaultSecureSource = newSecureSource()
  25. // defaultSecureRand is a math/rand.Rand based on the secure source.
  26. defaultSecureRand = mathRand.New(defaultSecureSource)
  27. )
  28. // String returns a cryptographically secure random string of characters
  29. // (taken from randomCharset) of the specified length. The returned string
  30. // contains ~5.8 bits of entropy per character, due to the character set used.
  31. func String(l int) string {
  32. bs := make([]byte, l)
  33. for i := range bs {
  34. bs[i] = randomCharset[defaultSecureRand.Intn(len(randomCharset))]
  35. }
  36. return string(bs)
  37. }
  38. // Int63 returns a cryptographically secure random int63.
  39. func Int63() int64 {
  40. return defaultSecureSource.Int63()
  41. }
  42. // Uint64 returns a cryptographically secure strongly random uint64.
  43. func Uint64() uint64 {
  44. return defaultSecureSource.Uint64()
  45. }
  46. // Intn returns, as an int, a cryptographically secure non-negative
  47. // random number in [0,n). It panics if n <= 0.
  48. func Intn(n int) int {
  49. return defaultSecureRand.Intn(n)
  50. }
  51. // Shuffle the order of elements in slice.
  52. func Shuffle(slice interface{}) {
  53. rv := reflect.ValueOf(slice)
  54. swap := reflect.Swapper(slice)
  55. length := rv.Len()
  56. if length < 2 {
  57. return
  58. }
  59. defaultSecureRand.Shuffle(length, swap)
  60. }