Source file src/crypto/rand/rand_getentropy.go
1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build (darwin && !ios) || openbsd 6 7 package rand 8 9 import ( 10 "internal/syscall/unix" 11 ) 12 13 func init() { 14 altGetRandom = getEntropy 15 } 16 17 func getEntropy(p []byte) error { 18 // getentropy(2) returns a maximum of 256 bytes per call 19 for i := 0; i < len(p); i += 256 { 20 end := i + 256 21 if len(p) < end { 22 end = len(p) 23 } 24 err := unix.GetEntropy(p[i:end]) 25 if err != nil { 26 return err 27 } 28 } 29 return nil 30 } 31