-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
59 lines (47 loc) · 1.13 KB
/
Copy pathutil.go
File metadata and controls
59 lines (47 loc) · 1.13 KB
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
57
58
59
package srp
import (
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"math"
"math/big"
)
func randBytes(n int) ([]byte, error) {
b := make([]byte, n)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return nil, fmt.Errorf("unable to read random bytes: %w", err)
}
return b, nil
}
func randBigInt(n int) (*big.Int, error) {
b, err := randBytes(n)
if err != nil {
return nil, err
}
return new(big.Int).SetBytes(b), nil
}
func writeBytes(w io.Writer, b []byte) error {
if len(b) > math.MaxUint16 {
return ErrTooBig
}
//nolint:gosec
if err := binary.Write(w, binary.BigEndian, uint16(len(b))); err != nil {
return fmt.Errorf("unable to write length: %w", err)
}
if _, err := w.Write(b); err != nil {
return fmt.Errorf("unable t write bytes: %w", err)
}
return nil
}
func readBytes(r io.Reader) ([]byte, error) {
var length uint16
if err := binary.Read(r, binary.BigEndian, &length); err != nil {
return nil, fmt.Errorf("unable to read length: %w", err)
}
b := make([]byte, length)
if _, err := io.ReadFull(r, b); err != nil {
return nil, fmt.Errorf("unable to read bytes: %w", err)
}
return b, nil
}