mirror of
https://github.com/safing/portbase
synced 2025-04-19 17:09:09 +00:00
35 lines
671 B
Go
35 lines
671 B
Go
package rng
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"fmt"
|
|
)
|
|
|
|
func osFeeder(ctx context.Context) error {
|
|
entropyBytes := minFeedEntropy / 8
|
|
feeder := NewFeeder()
|
|
defer feeder.CloseFeeder()
|
|
|
|
for {
|
|
// gather
|
|
osEntropy := make([]byte, entropyBytes)
|
|
n, err := rand.Read(osEntropy)
|
|
if err != nil {
|
|
return fmt.Errorf("could not read entropy from os: %w", err)
|
|
}
|
|
if n != entropyBytes {
|
|
return fmt.Errorf("could not read enough entropy from os: got only %d bytes instead of %d", n, entropyBytes)
|
|
}
|
|
|
|
// feed
|
|
select {
|
|
case feeder.input <- &entropyData{
|
|
data: osEntropy,
|
|
entropy: entropyBytes * 8,
|
|
}:
|
|
case <-ctx.Done():
|
|
return nil
|
|
}
|
|
}
|
|
}
|