Skip to content
Snippets Groups Projects
Unverified Commit 21bbf9e6 authored by Angelo Capossele's avatar Angelo Capossele Committed by GitHub
Browse files

Fix entropy of prng (#422)

* :bug: fix entropy of prng

* :recycle: simplify pseudoR
parent 6b60d47d
No related branches found
No related tags found
No related merge requests found
package prng package prng
import ( import (
"bytes" "math/rand"
"encoding/binary"
"time" "time"
) )
...@@ -62,12 +61,10 @@ func (utrng *UnixTimestampPrng) send() { ...@@ -62,12 +61,10 @@ func (utrng *UnixTimestampPrng) send() {
now := utrng.timeSourceFunc() now := utrng.timeSourceFunc()
// reduce to last resolution // reduce to last resolution
timePoint := now - (now % utrng.resolution) timePoint := now - (now % utrng.resolution)
// convert to float64
buf := bytes.NewBuffer(make([]byte, 0, 8)) // add entropy and convert to float64
if err := binary.Write(buf, binary.LittleEndian, timePoint); err != nil { pseudoR := rand.New(rand.NewSource(timePoint)).Float64()
panic(err)
}
pseudoR := float64(binary.BigEndian.Uint64(buf.Bytes()[:8])>>11) / (1 << 53)
// skip slow consumers // skip slow consumers
select { select {
case utrng.c <- pseudoR: case utrng.c <- pseudoR:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment