-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (61 loc) · 1.27 KB
/
main.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
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
"crypto/sha256"
cpu "github.com/shirou/gopsutil/cpu"
)
// Seeds the thread with a combination of time now and the pid
func getSeed() {
pid := os.Getpid()
var pid64 int64
pid64 = int64(pid)
timeNow := time.Now().UTC().UnixNano()
seedInt := pid64 + timeNow
rand.Seed(seedInt)
}
// Used to get a random int.
func getRandomInt(min, max int) int {
return min + rand.Intn(max-min)
}
// Actually get the random output
func getEntropy() ([]byte, error) {
d, err := time.ParseDuration("1s")
if err != nil {
return nil, err
}
info, err := cpu.Percent(d, false)
if err != nil {
return nil, err
}
var infoTotal int
for i := range info {
usage := info[i]
var intVal int
intVal = int(usage)
infoTotal = infoTotal + intVal
}
hash := sha256.New()
infoString := strconv.Itoa(infoTotal)
randInt := getRandomInt(0, 99999999)
intString := strconv.Itoa(randInt)
randString := intString + infoString
hash.Write([]byte(randString))
entropyValue := hash.Sum(nil)
return entropyValue, nil
}
// Keep running until we exit
func main() {
for {
output, err := getEntropy()
if err != nil {
fmt.Println("Error occured getting entropy:", err)
os.Exit(1)
}
fmt.Println(string(output))
getSeed()
}
}