forked from quic-go/quic-go
send entropy in packets, validate entropy in received ACKs
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user