forked from coyim/otr3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
45 lines (36 loc) · 864 Bytes
/
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
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 randSizedMPI(r io.Reader, size int) (*big.Int, error) {
return randMPI(r, make([]byte, size))
}
func (c *Conversation) randSizedMPI(size int) (*big.Int, error) {
return randMPI(c.rand(), make([]byte, size))
}
func (c *Conversation) randMPI(buf []byte) (*big.Int, error) {
return randMPI(c.rand(), buf)
}
func (c *Conversation) randomInto(b []byte) error {
return randomInto(c.rand(), b)
}