-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonce.go
77 lines (63 loc) · 1.43 KB
/
nonce.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package o3
import (
"crypto/rand"
"encoding/binary"
)
type nonce struct {
nonce [24]byte
}
func (n nonce) bytes() *[24]byte {
return &n.nonce
}
func (n nonce) byteSlice() []byte {
return n.nonce[0:24]
}
func (n nonce) prefix() [16]byte {
var ret [16]byte
copy(ret[0:16], n.nonce[0:16])
return ret
}
func (n *nonce) setPrefix(prefix [16]byte) {
copy(n.nonce[0:16], prefix[0:16])
}
func (n *nonce) set(nonce []byte) {
copy(n.nonce[0:24], nonce)
}
func (n nonce) counter() uint64 {
return binary.LittleEndian.Uint64(n.nonce[16:24])
}
func (n *nonce) setCounter(c uint64) {
binary.LittleEndian.PutUint64(n.nonce[16:24], c)
}
func (n *nonce) increaseCounter() {
binary.LittleEndian.PutUint64(n.nonce[16:24], binary.LittleEndian.Uint64(n.nonce[16:24])+1)
}
func (n *nonce) initialize(prefix [16]byte, c uint64) {
n.setPrefix(prefix)
n.setCounter(c)
}
// NewRandomNonce returns a fully random 24-byte nonce
func newRandomNonce() nonce {
var n nonce
if _, err := rand.Read(n.nonce[0:24]); err != nil {
//Error.Println(err)
}
return n
}
// NewNonce returns a new 16-byte nonce with
// a counter value set to 1.
func newNonce() nonce {
var n nonce
if _, err := rand.Read(n.nonce[0:16]); err != nil {
//Error.Println(err)
}
n.setCounter(1)
return n
}
// NewPrefixedNonce returns a new Nonce with the given prefix the
// counter set to 1
func newPrefixedNonce(prefix [16]byte) nonce {
var n nonce
n.initialize(prefix, 1)
return n
}