-
Notifications
You must be signed in to change notification settings - Fork 9
/
tickfeeder.go
77 lines (63 loc) · 1.73 KB
/
tickfeeder.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package rng
import (
"context"
"encoding/binary"
"time"
)
func getTickFeederTickDuration() time.Duration {
// be ready in 1/10 time of reseedAfterSeconds
msecsAvailable := reseedAfterSeconds * 100
// ex.: reseed after 10 minutes: msecsAvailable = 60000
// have full entropy after 5 minutes
// one tick generates 0,125 bits of entropy
ticksNeeded := minFeedEntropy * 8
// ex.: minimum entropy is 256: ticksNeeded = 2048
// msces between ticks
tickMsecs := msecsAvailable / ticksNeeded
// ex.: tickMsecs = 29(,296875)
// use a minimum of 10 msecs per tick for good entropy
// it would take 21 seconds to get full 256 bits of entropy with 10msec ticks
if tickMsecs < 10 {
tickMsecs = 10
}
return time.Duration(tickMsecs) * time.Millisecond
}
// tickFeeder is a really simple entropy feeder that adds the least significant bit of the current nanosecond unixtime to its pool every time it 'ticks'.
// The more work the program does, the better the quality, as the internal schedular cannot immediately run the goroutine when it's ready.
func tickFeeder(ctx context.Context) error {
var value int64
var pushes int
feeder := NewFeeder()
defer feeder.CloseFeeder()
tickDuration := getTickFeederTickDuration()
for {
// wait for tick
time.Sleep(tickDuration)
// add tick value
value = (value << 1) | (time.Now().UnixNano() % 2)
pushes++
if pushes >= 64 {
// convert to []byte
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(value))
// reset
pushes = 0
// feed
select {
case feeder.input <- &entropyData{
data: b,
entropy: 8,
}:
case <-ctx.Done():
return nil
}
} else {
// check if are done
select {
case <-ctx.Done():
return nil
default:
}
}
}
}