-
Notifications
You must be signed in to change notification settings - Fork 1
/
source.go
50 lines (41 loc) · 1.12 KB
/
source.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
// Package cryptorand provides a math/rand.Source64 implementation of crypto/rand
package cryptorand
import (
crand "crypto/rand"
"fmt"
"io"
"math"
"math/big"
"math/rand"
)
type source struct{ io.Reader }
var maxInt63 = new(big.Int).SetUint64(1 << 63)
var maxUint64 = new(big.Int).Add(new(big.Int).SetUint64(math.MaxUint64), new(big.Int).SetInt64(1))
// Source is a math/rand.Source64 backed by crypto/rand.
// Calling Seed() will result in a panic.
var Source rand.Source
// NewSource returns a new rand.Source64 backed by the given random source.
// Calling Seed() will result in a panic.
func NewSource(rand io.Reader) rand.Source {
return source{rand}
}
func init() {
Source = NewSource(crand.Reader)
}
func (s source) Int63() int64 {
i, err := crand.Int(s, maxInt63)
if err != nil {
panic(fmt.Errorf("crypto/rand.Int returned error: %v", err))
}
return i.Int64()
}
func (s source) Uint64() uint64 {
i, err := crand.Int(s, maxUint64)
if err != nil {
panic(fmt.Errorf("crypto/rand.Int returned error: %v", err))
}
return i.Uint64()
}
func (source) Seed(int64) {
panic("Seed() is not allowed on cryptorand.Source")
}