2016-02-06 19:20:54 +01:00
|
|
|
package cryptocore
|
2015-09-03 18:22:18 +02:00
|
|
|
|
|
|
|
import (
|
2015-10-04 14:36:20 +02:00
|
|
|
"crypto/rand"
|
2015-12-13 20:10:52 +01:00
|
|
|
"encoding/binary"
|
2016-12-10 11:50:16 +01:00
|
|
|
"log"
|
2015-09-03 18:22:18 +02:00
|
|
|
)
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// RandBytes gets "n" random bytes from /dev/urandom or panics
|
2015-09-13 17:55:07 +02:00
|
|
|
func RandBytes(n int) []byte {
|
|
|
|
b := make([]byte, n)
|
2015-09-03 18:22:18 +02:00
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
2016-12-10 11:50:16 +01:00
|
|
|
log.Panic("Failed to read random bytes: " + err.Error())
|
2015-09-03 18:22:18 +02:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// RandUint64 returns a secure random uint64
|
2015-12-11 19:54:53 +01:00
|
|
|
func RandUint64() uint64 {
|
|
|
|
b := RandBytes(8)
|
|
|
|
return binary.BigEndian.Uint64(b)
|
|
|
|
}
|
|
|
|
|
2015-12-19 14:41:39 +01:00
|
|
|
type nonceGenerator struct {
|
2017-06-07 23:08:43 +02:00
|
|
|
nonceLen int // bytes
|
2015-09-03 18:22:18 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
// Get a random "nonceLen"-byte nonce
|
2015-12-19 14:41:39 +01:00
|
|
|
func (n *nonceGenerator) Get() []byte {
|
2017-06-09 21:52:26 +02:00
|
|
|
return randPrefetcher.read(n.nonceLen)
|
2015-09-03 18:22:18 +02:00
|
|
|
}
|