-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorng.go
56 lines (45 loc) · 1.14 KB
/
gorng.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Check: https://blog.gopheracademy.com/advent-2017/a-tale-of-two-rands/
*/
package gorng
import (
"crypto/rand"
"encoding/binary"
"math/big"
mrand "math/rand"
)
var Rng = randInit()
type cryptoSource struct{}
func (s *cryptoSource) Seed(seed int64) {}
// Uint64 returns a securely generated int64 value.
func (s *cryptoSource) Uint64() (value uint64) {
binary.Read(rand.Reader, binary.BigEndian, &value)
return value
}
func (s *cryptoSource) Int63() int64 {
return int64(s.Uint64() & ^uint64(1<<63))
}
func randInit() (myRand *mrand.Rand) {
var src mrand.Source64 = &cryptoSource{}
myRand = mrand.New(src)
return myRand
}
// GenerateRandomBytes returns securely generated random bytes.
// It will return an error if fails
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
// GenerateRandomInt returns securely generated random integer.
// It will return an error if fails
func GenerateRandomInt(n int64) (int, error) {
num, err := rand.Int(rand.Reader, big.NewInt(n))
if err != nil {
return -1, err
}
return int(num.Int64()), nil
}