forked from nimbus/go-minilock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
42 lines (37 loc) · 781 Bytes
/
utils.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
package minilock
import (
"bytes"
"crypto/rand"
"encoding/binary"
)
func randBytes(i int) ([]byte, error) {
rand_bytes := make([]byte, i)
read, err := rand.Read(rand_bytes)
if err != nil {
return nil, err
}
if read != i {
return nil, NotEnoughRandomnessError
}
return rand_bytes, nil
}
func makeFullNonce() ([]byte, error) {
return randBytes(24)
}
func toLittleEndian(i int32) ([]byte, error) {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, i)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func fromLittleEndian(buf []byte) (int32, error) {
var output int32
buf_reader := bytes.NewReader(buf)
err := binary.Read(buf_reader, binary.LittleEndian, &output)
if err != nil {
return 0, err
}
return output, nil
}