csrand.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2014, Yawning Angel <yawning at schwanenlied dot me>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // Package csrand implements the math/rand interface over crypto/rand, along
  28. // with some utility functions for common random number/byte related tasks.
  29. //
  30. // Not all of the convinience routines are replicated, only those that are
  31. // immediately useful. The Rand variable provides access to the full math/rand
  32. // API.
  33. package csrand // import "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/common/csrand"
  34. import (
  35. cryptRand "crypto/rand"
  36. "encoding/binary"
  37. "fmt"
  38. "io"
  39. "math/rand"
  40. )
  41. var (
  42. csRandSourceInstance csRandSource
  43. // Rand is a math/rand instance backed by crypto/rand CSPRNG.
  44. Rand = rand.New(csRandSourceInstance)
  45. )
  46. type csRandSource struct {
  47. // This does not keep any state as it is backed by crypto/rand.
  48. }
  49. func (r csRandSource) Int63() int64 {
  50. var src [8]byte
  51. if err := Bytes(src[:]); err != nil {
  52. panic(err)
  53. }
  54. val := binary.BigEndian.Uint64(src[:])
  55. val &= (1<<63 - 1)
  56. return int64(val)
  57. }
  58. func (r csRandSource) Seed(seed int64) {
  59. // No-op.
  60. }
  61. // Intn returns, as a int, a pseudo random number in [0, n).
  62. func Intn(n int) int {
  63. return Rand.Intn(n)
  64. }
  65. // Float64 returns, as a float64, a pesudo random number in [0.0,1.0).
  66. func Float64() float64 {
  67. return Rand.Float64()
  68. }
  69. // IntRange returns a uniformly distributed int [min, max].
  70. func IntRange(min, max int) int {
  71. if max < min {
  72. panic(fmt.Sprintf("IntRange: min > max (%d, %d)", min, max))
  73. }
  74. r := (max + 1) - min
  75. ret := Rand.Intn(r)
  76. return ret + min
  77. }
  78. // Bytes fills the slice with random data.
  79. func Bytes(buf []byte) error {
  80. if _, err := io.ReadFull(cryptRand.Reader, buf); err != nil {
  81. return err
  82. }
  83. return nil
  84. }
  85. // Reader is a alias of rand.Reader.
  86. var Reader = cryptRand.Reader