-
Notifications
You must be signed in to change notification settings - Fork 14
/
random.go
53 lines (42 loc) · 1.01 KB
/
random.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
package otr3
import (
"crypto/rand"
"io"
"math/big"
)
func (c *Conversation) rand() io.Reader {
if c.Rand != nil {
return c.Rand
}
return rand.Reader
}
func randomInto(r io.Reader, b []byte) error {
if _, err := io.ReadFull(r, b); err != nil {
return errShortRandomRead
}
return nil
}
func randMPI(r io.Reader, b []byte) (*big.Int, error) {
if err := randomInto(r, b); err != nil {
return nil, err
}
return new(big.Int).SetBytes(b), nil
}
func randSecret(r io.Reader, b []byte) (secretKeyValue, error) {
if err := randomInto(r, b); err != nil {
return nil, err
}
return secretKeyValue(b), nil
}
func randSizedSecret(r io.Reader, size int) (secretKeyValue, error) {
return randSecret(r, make([]byte, size))
}
func (c *Conversation) randMPI(buf []byte) (*big.Int, error) {
return randMPI(c.rand(), buf)
}
func (c *Conversation) randSecret(buf []byte) (secretKeyValue, error) {
return randSecret(c.rand(), buf)
}
func (c *Conversation) randomInto(b []byte) error {
return randomInto(c.rand(), b)
}