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.

357 lines
6.4 KiB

9 years ago
9 years ago
7 years ago
7 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
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. // pseudo random number generator.
  12. // seeded with OS randomness (crand)
  13. type Rand struct {
  14. sync.Mutex
  15. rand *mrand.Rand
  16. }
  17. var grand *Rand
  18. func init() {
  19. grand = NewRand()
  20. grand.init()
  21. }
  22. func NewRand() *Rand {
  23. rand := &Rand{}
  24. rand.init()
  25. return rand
  26. }
  27. func (r *Rand) init() {
  28. bz := cRandBytes(8)
  29. var seed uint64
  30. for i := 0; i < 8; i++ {
  31. seed |= uint64(bz[i])
  32. seed <<= 8
  33. }
  34. r.reset(int64(seed))
  35. }
  36. func (r *Rand) reset(seed int64) {
  37. r.rand = mrand.New(mrand.NewSource(seed))
  38. }
  39. //----------------------------------------
  40. // Global functions
  41. func Seed(seed int64) {
  42. grand.Seed(seed)
  43. }
  44. func RandStr(length int) string {
  45. return grand.Str(length)
  46. }
  47. func RandUint16() uint16 {
  48. return grand.Uint16()
  49. }
  50. func RandUint32() uint32 {
  51. return grand.Uint32()
  52. }
  53. func RandUint64() uint64 {
  54. return grand.Uint64()
  55. }
  56. func RandUint() uint {
  57. return grand.Uint()
  58. }
  59. func RandInt16() int16 {
  60. return grand.Int16()
  61. }
  62. func RandInt32() int32 {
  63. return grand.Int32()
  64. }
  65. func RandInt64() int64 {
  66. return grand.Int64()
  67. }
  68. func RandInt() int {
  69. return grand.Int()
  70. }
  71. func RandInt31() int32 {
  72. return grand.Int31()
  73. }
  74. func RandInt31n(n int32) int32 {
  75. return grand.Int31n(n)
  76. }
  77. func RandInt63() int64 {
  78. return grand.Int63()
  79. }
  80. func RandInt63n(n int64) int64 {
  81. return grand.Int63n(n)
  82. }
  83. func RandUint16Exp() uint16 {
  84. return grand.Uint16Exp()
  85. }
  86. func RandUint32Exp() uint32 {
  87. return grand.Uint32Exp()
  88. }
  89. func RandUint64Exp() uint64 {
  90. return grand.Uint64Exp()
  91. }
  92. func RandFloat32() float32 {
  93. return grand.Float32()
  94. }
  95. func RandFloat64() float64 {
  96. return grand.Float64()
  97. }
  98. func RandTime() time.Time {
  99. return grand.Time()
  100. }
  101. func RandBytes(n int) []byte {
  102. return grand.Bytes(n)
  103. }
  104. func RandIntn(n int) int {
  105. return grand.Intn(n)
  106. }
  107. func RandPerm(n int) []int {
  108. return grand.Perm(n)
  109. }
  110. //----------------------------------------
  111. // Rand methods
  112. func (r *Rand) Seed(seed int64) {
  113. r.Lock()
  114. r.reset(seed)
  115. r.Unlock()
  116. }
  117. // Constructs an alphanumeric string of given length.
  118. // It is not safe for cryptographic usage.
  119. func (r *Rand) Str(length int) string {
  120. chars := []byte{}
  121. MAIN_LOOP:
  122. for {
  123. val := r.Int63()
  124. for i := 0; i < 10; i++ {
  125. v := int(val & 0x3f) // rightmost 6 bits
  126. if v >= 62 { // only 62 characters in strChars
  127. val >>= 6
  128. continue
  129. } else {
  130. chars = append(chars, strChars[v])
  131. if len(chars) == length {
  132. break MAIN_LOOP
  133. }
  134. val >>= 6
  135. }
  136. }
  137. }
  138. return string(chars)
  139. }
  140. // It is not safe for cryptographic usage.
  141. func (r *Rand) Uint16() uint16 {
  142. return uint16(r.Uint32() & (1<<16 - 1))
  143. }
  144. // It is not safe for cryptographic usage.
  145. func (r *Rand) Uint32() uint32 {
  146. r.Lock()
  147. u32 := r.rand.Uint32()
  148. r.Unlock()
  149. return u32
  150. }
  151. // It is not safe for cryptographic usage.
  152. func (r *Rand) Uint64() uint64 {
  153. return uint64(r.Uint32())<<32 + uint64(r.Uint32())
  154. }
  155. // It is not safe for cryptographic usage.
  156. func (r *Rand) Uint() uint {
  157. r.Lock()
  158. i := r.rand.Int()
  159. r.Unlock()
  160. return uint(i)
  161. }
  162. // It is not safe for cryptographic usage.
  163. func (r *Rand) Int16() int16 {
  164. return int16(r.Uint32() & (1<<16 - 1))
  165. }
  166. // It is not safe for cryptographic usage.
  167. func (r *Rand) Int32() int32 {
  168. return int32(r.Uint32())
  169. }
  170. // It is not safe for cryptographic usage.
  171. func (r *Rand) Int64() int64 {
  172. return int64(r.Uint64())
  173. }
  174. // It is not safe for cryptographic usage.
  175. func (r *Rand) Int() int {
  176. r.Lock()
  177. i := r.rand.Int()
  178. r.Unlock()
  179. return i
  180. }
  181. // It is not safe for cryptographic usage.
  182. func (r *Rand) Int31() int32 {
  183. r.Lock()
  184. i31 := r.rand.Int31()
  185. r.Unlock()
  186. return i31
  187. }
  188. // It is not safe for cryptographic usage.
  189. func (r *Rand) Int31n(n int32) int32 {
  190. r.Lock()
  191. i31n := r.rand.Int31n(n)
  192. r.Unlock()
  193. return i31n
  194. }
  195. // It is not safe for cryptographic usage.
  196. func (r *Rand) Int63() int64 {
  197. r.Lock()
  198. i63 := r.rand.Int63()
  199. r.Unlock()
  200. return i63
  201. }
  202. // It is not safe for cryptographic usage.
  203. func (r *Rand) Int63n(n int64) int64 {
  204. r.Lock()
  205. i63n := r.rand.Int63n(n)
  206. r.Unlock()
  207. return i63n
  208. }
  209. // Distributed pseudo-exponentially to test for various cases
  210. // It is not safe for cryptographic usage.
  211. func (r *Rand) Uint16Exp() uint16 {
  212. bits := r.Uint32() % 16
  213. if bits == 0 {
  214. return 0
  215. }
  216. n := uint16(1 << (bits - 1))
  217. n += uint16(r.Int31()) & ((1 << (bits - 1)) - 1)
  218. return n
  219. }
  220. // Distributed pseudo-exponentially to test for various cases
  221. // It is not safe for cryptographic usage.
  222. func (r *Rand) Uint32Exp() uint32 {
  223. bits := r.Uint32() % 32
  224. if bits == 0 {
  225. return 0
  226. }
  227. n := uint32(1 << (bits - 1))
  228. n += uint32(r.Int31()) & ((1 << (bits - 1)) - 1)
  229. return n
  230. }
  231. // Distributed pseudo-exponentially to test for various cases
  232. // It is not safe for cryptographic usage.
  233. func (r *Rand) Uint64Exp() uint64 {
  234. bits := r.Uint32() % 64
  235. if bits == 0 {
  236. return 0
  237. }
  238. n := uint64(1 << (bits - 1))
  239. n += uint64(r.Int63()) & ((1 << (bits - 1)) - 1)
  240. return n
  241. }
  242. // It is not safe for cryptographic usage.
  243. func (r *Rand) Float32() float32 {
  244. r.Lock()
  245. f32 := r.rand.Float32()
  246. r.Unlock()
  247. return f32
  248. }
  249. // It is not safe for cryptographic usage.
  250. func (r *Rand) Float64() float64 {
  251. r.Lock()
  252. f64 := r.rand.Float64()
  253. r.Unlock()
  254. return f64
  255. }
  256. // It is not safe for cryptographic usage.
  257. func (r *Rand) Time() time.Time {
  258. return time.Unix(int64(r.Uint64Exp()), 0)
  259. }
  260. // RandBytes returns n random bytes from the OS's source of entropy ie. via crypto/rand.
  261. // It is not safe for cryptographic usage.
  262. func (r *Rand) Bytes(n int) []byte {
  263. // cRandBytes isn't guaranteed to be fast so instead
  264. // use random bytes generated from the internal PRNG
  265. bs := make([]byte, n)
  266. for i := 0; i < len(bs); i++ {
  267. bs[i] = byte(r.Int() & 0xFF)
  268. }
  269. return bs
  270. }
  271. // RandIntn returns, as an int, a non-negative pseudo-random number in [0, n).
  272. // It panics if n <= 0.
  273. // It is not safe for cryptographic usage.
  274. func (r *Rand) Intn(n int) int {
  275. r.Lock()
  276. i := r.rand.Intn(n)
  277. r.Unlock()
  278. return i
  279. }
  280. // RandPerm returns a pseudo-random permutation of n integers in [0, n).
  281. // It is not safe for cryptographic usage.
  282. func (r *Rand) Perm(n int) []int {
  283. r.Lock()
  284. perm := r.rand.Perm(n)
  285. r.Unlock()
  286. return perm
  287. }
  288. // NOTE: This relies on the os's random number generator.
  289. // For real security, we should salt that with some seed.
  290. // See github.com/tendermint/go-crypto for a more secure reader.
  291. func cRandBytes(numBytes int) []byte {
  292. b := make([]byte, numBytes)
  293. _, err := crand.Read(b)
  294. if err != nil {
  295. PanicCrisis(err)
  296. }
  297. return b
  298. }