Created
April 8, 2019 14:19
-
-
Save jeromegn/ba3f694412979d21dafc9d625b8fcf04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"strconv" | |
"syscall" | |
"time" | |
"unsafe" | |
"golang.org/x/sys/unix" | |
) | |
type entropy struct { | |
bitCount int /* number of bits of entropy in data */ | |
byteCount int /* number of bytes of data in array */ | |
buf []byte | |
} | |
func main() { | |
start := time.Now() | |
for avail() < 2048 { | |
urnd, err := os.Open("/dev/urandom") | |
check(err) | |
rnd, err := os.Open("/dev/random") | |
check(err) | |
d := make([]byte, 512) | |
_, err = urnd.Read(d) | |
check(err) | |
e := entropy{4 * len(d), len(d), d} | |
ioctl(rnd.Fd(), unix.RNDADDENTROPY, uintptr(unsafe.Pointer(&e))) | |
} | |
elapsed := time.Since(start) | |
fmt.Printf("Finished entropizing with %d of bad entropy. took %s\n", avail(), elapsed) | |
} | |
func avail() int { | |
f, err := os.Open("/proc/sys/kernel/random/entropy_avail") | |
check(err) | |
r, err := ioutil.ReadAll(f) | |
check(err) | |
i, err := strconv.Atoi(string(r[:len(r)-1])) | |
check(err) | |
return i | |
} | |
func check(e error) { | |
if e != nil { | |
panic(e) | |
} | |
} | |
func ioctl(fd, op, arg uintptr) error { | |
_, _, ep := syscall.Syscall(syscall.SYS_IOCTL, fd, op, arg) | |
if ep != 0 { | |
return syscall.Errno(ep) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment