Ejemplo n.º 1
0
func getRandomLinux(p []byte) (ok bool) { // 调用GetRandom
	once.Do(pickStrategy) // 只执行一次pickStrategy
	if !useSyscall {
		return false
	}
	n, err := unix.GetRandom(p, 0)
	return n == len(p) && err == nil
}
Ejemplo n.º 2
0
func pickStrategy() {
	// Test whether we should use the system call or /dev/urandom.
	// We'll fall back to urandom if:
	// - the kernel is too old (before 3.17)
	// - the machine has no entropy available (early boot + no hardware
	//   entropy source?) and we want to avoid blocking later.
	var buf [1]byte
	n, err := unix.GetRandom(buf[:], unix.GRND_NONBLOCK)
	useSyscall = n == 1 && err == nil
}