You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
4.9 KiB

9 years ago
9 years ago
6 years ago
6 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package common
  2. import (
  3. crand "crypto/rand"
  4. mrand "math/rand"
  5. "sync"
  6. "time"
  7. )
  8. const (
  9. strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
  10. )
  11. // Rand is a prng, that is seeded with OS randomness.
  12. // The OS randomness is obtained from crypto/rand, however none of the provided
  13. // methods are suitable for cryptographic usage.
  14. // They all utilize math/rand's prng internally.
  15. //
  16. // All of the methods here are suitable for concurrent use.
  17. // This is achieved by using a mutex lock on all of the provided methods.
  18. type Rand struct {
  19. sync.Mutex
  20. rand *mrand.Rand
  21. }
  22. var grand *Rand
  23. func init() {
  24. grand = NewRand()
  25. grand.init()
  26. }
  27. func NewRand() *Rand {
  28. rand := &Rand{}
  29. rand.init()
  30. return rand
  31. }
  32. func (r *Rand) init() {
  33. bz := cRandBytes(8)
  34. var seed uint64
  35. for i := 0; i < 8; i++ {
  36. seed |= uint64(bz[i])
  37. seed <<= 8
  38. }
  39. r.reset(int64(seed))
  40. }
  41. func (r *Rand) reset(seed int64) {
  42. r.rand = mrand.New(mrand.NewSource(seed))
  43. }
  44. //----------------------------------------
  45. // Global functions
  46. func Seed(seed int64) {
  47. grand.Seed(seed)
  48. }
  49. func RandStr(length int) string {
  50. return grand.Str(length)
  51. }
  52. func RandUint16() uint16 {
  53. return grand.Uint16()
  54. }
  55. func RandUint32() uint32 {
  56. return grand.Uint32()
  57. }
  58. func RandUint64() uint64 {
  59. return grand.Uint64()
  60. }
  61. func RandUint() uint {
  62. return grand.Uint()
  63. }
  64. func RandInt16() int16 {
  65. return grand.Int16()
  66. }
  67. func RandInt32() int32 {
  68. return grand.Int32()
  69. }
  70. func RandInt64() int64 {
  71. return grand.Int64()
  72. }
  73. func RandInt() int {
  74. return grand.Int()
  75. }
  76. func RandInt31() int32 {
  77. return grand.Int31()
  78. }
  79. func RandInt31n(n int32) int32 {
  80. return grand.Int31n(n)
  81. }
  82. func RandInt63() int64 {
  83. return grand.Int63()
  84. }
  85. func RandInt63n(n int64) int64 {
  86. return grand.Int63n(n)
  87. }
  88. func RandFloat32() float32 {
  89. return grand.Float32()
  90. }
  91. func RandFloat64() float64 {
  92. return grand.Float64()
  93. }
  94. func RandTime() time.Time {
  95. return grand.Time()
  96. }
  97. func RandBytes(n int) []byte {
  98. return grand.Bytes(n)
  99. }
  100. func RandIntn(n int) int {
  101. return grand.Intn(n)
  102. }
  103. func RandPerm(n int) []int {
  104. return grand.Perm(n)
  105. }
  106. //----------------------------------------
  107. // Rand methods
  108. func (r *Rand) Seed(seed int64) {
  109. r.Lock()
  110. r.reset(seed)
  111. r.Unlock()
  112. }
  113. // Str constructs a random alphanumeric string of given length.
  114. func (r *Rand) Str(length int) string {
  115. chars := []byte{}
  116. MAIN_LOOP:
  117. for {
  118. val := r.Int63()
  119. for i := 0; i < 10; i++ {
  120. v := int(val & 0x3f) // rightmost 6 bits
  121. if v >= 62 { // only 62 characters in strChars
  122. val >>= 6
  123. continue
  124. } else {
  125. chars = append(chars, strChars[v])
  126. if len(chars) == length {
  127. break MAIN_LOOP
  128. }
  129. val >>= 6
  130. }
  131. }
  132. }
  133. return string(chars)
  134. }
  135. func (r *Rand) Uint16() uint16 {
  136. return uint16(r.Uint32() & (1<<16 - 1))
  137. }
  138. func (r *Rand) Uint32() uint32 {
  139. r.Lock()
  140. u32 := r.rand.Uint32()
  141. r.Unlock()
  142. return u32
  143. }
  144. func (r *Rand) Uint64() uint64 {
  145. return uint64(r.Uint32())<<32 + uint64(r.Uint32())
  146. }
  147. func (r *Rand) Uint() uint {
  148. r.Lock()
  149. i := r.rand.Int()
  150. r.Unlock()
  151. return uint(i)
  152. }
  153. func (r *Rand) Int16() int16 {
  154. return int16(r.Uint32() & (1<<16 - 1))
  155. }
  156. func (r *Rand) Int32() int32 {
  157. return int32(r.Uint32())
  158. }
  159. func (r *Rand) Int64() int64 {
  160. return int64(r.Uint64())
  161. }
  162. func (r *Rand) Int() int {
  163. r.Lock()
  164. i := r.rand.Int()
  165. r.Unlock()
  166. return i
  167. }
  168. func (r *Rand) Int31() int32 {
  169. r.Lock()
  170. i31 := r.rand.Int31()
  171. r.Unlock()
  172. return i31
  173. }
  174. func (r *Rand) Int31n(n int32) int32 {
  175. r.Lock()
  176. i31n := r.rand.Int31n(n)
  177. r.Unlock()
  178. return i31n
  179. }
  180. func (r *Rand) Int63() int64 {
  181. r.Lock()
  182. i63 := r.rand.Int63()
  183. r.Unlock()
  184. return i63
  185. }
  186. func (r *Rand) Int63n(n int64) int64 {
  187. r.Lock()
  188. i63n := r.rand.Int63n(n)
  189. r.Unlock()
  190. return i63n
  191. }
  192. func (r *Rand) Float32() float32 {
  193. r.Lock()
  194. f32 := r.rand.Float32()
  195. r.Unlock()
  196. return f32
  197. }
  198. func (r *Rand) Float64() float64 {
  199. r.Lock()
  200. f64 := r.rand.Float64()
  201. r.Unlock()
  202. return f64
  203. }
  204. func (r *Rand) Time() time.Time {
  205. return time.Unix(int64(r.Uint64()), 0)
  206. }
  207. // Bytes returns n random bytes generated from the internal
  208. // prng.
  209. func (r *Rand) Bytes(n int) []byte {
  210. // cRandBytes isn't guaranteed to be fast so instead
  211. // use random bytes generated from the internal PRNG
  212. bs := make([]byte, n)
  213. for i := 0; i < len(bs); i++ {
  214. bs[i] = byte(r.Int() & 0xFF)
  215. }
  216. return bs
  217. }
  218. // Intn returns, as an int, a uniform pseudo-random number in the range [0, n).
  219. // It panics if n <= 0.
  220. func (r *Rand) Intn(n int) int {
  221. r.Lock()
  222. i := r.rand.Intn(n)
  223. r.Unlock()
  224. return i
  225. }
  226. // Perm returns a pseudo-random permutation of n integers in [0, n).
  227. func (r *Rand) Perm(n int) []int {
  228. r.Lock()
  229. perm := r.rand.Perm(n)
  230. r.Unlock()
  231. return perm
  232. }
  233. // NOTE: This relies on the os's random number generator.
  234. // For real security, we should salt that with some seed.
  235. // See github.com/tendermint/go-crypto for a more secure reader.
  236. func cRandBytes(numBytes int) []byte {
  237. b := make([]byte, numBytes)
  238. _, err := crand.Read(b)
  239. if err != nil {
  240. PanicCrisis(err)
  241. }
  242. return b
  243. }