aboutsummaryrefslogtreecommitdiff
path: root/internal/cryptocore/nonce.go
blob: c800807b81e5abacb246bda85809752380c8d9ec (plain)
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
package cryptocore

import (
	"crypto/rand"
	"encoding/binary"
	"log"
)

// RandBytes gets "n" random bytes from /dev/urandom or panics
func RandBytes(n int) []byte {
	b := make([]byte, n)
	_, err := rand.Read(b)
	if err != nil {
		// crypto/rand.Read() is documented to never return an
		// error, so this should never happen. Still, better safe than sorry.
		log.Panic("Failed to read random bytes: " + err.Error())
	}
	return b
}

// RandUint64 returns a secure random uint64
func RandUint64() uint64 {
	b := RandBytes(8)
	return binary.BigEndian.Uint64(b)
}

type nonceGenerator struct {
	nonceLen int // bytes
}

// Get a random "nonceLen"-byte nonce
func (n *nonceGenerator) Get() []byte {
	return randPrefetcher.read(n.nonceLen)
}