send entropy in packets, validate entropy in received ACKs

This commit is contained in:
Marten Seemann
2016-04-17 14:21:36 +07:00
parent 922a2975e8
commit 1097698c4b
3 changed files with 57 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ package utils
import (
"bytes"
"crypto/rand"
"io"
)
@@ -123,6 +124,20 @@ func Min(a, b int) int {
return b
}
// RandomBit returns a cryptographically secure random bit (encoded as true / false)
func RandomBit() (bool, error) {
// ToDo: it's probably more efficient to read a bigger slice of random numbers at once and to cache them somewhere
b := make([]byte, 1)
_, err := rand.Read(b)
if err != nil {
return false, err
}
if uint8(b[0])%2 == 0 {
return false, nil
}
return true, nil
}
// Uint32Slice attaches the methods of sort.Interface to []uint32, sorting in increasing order.
type Uint32Slice []uint32

View File

@@ -110,6 +110,14 @@ var _ = Describe("Utils", func() {
})
})
Context("Rand", func() {
It("returns either true or false", func() {
val, err := RandomBit()
Expect(err).ToNot(HaveOccurred())
Expect(val).To(SatisfyAny(Equal(true), Equal(false)))
})
})
Context("ReadUintN", func() {
It("reads n bytes", func() {
m := map[uint8]uint64{