Black Lives Matter. Support the Equal Justice Initiative.

Source file src/crypto/rand/rand_getentropy.go

Documentation: crypto/rand

     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 || openbsd
     6  // +build darwin openbsd
     7  
     8  package rand
     9  
    10  import (
    11  	"internal/syscall/unix"
    12  )
    13  
    14  func init() {
    15  	altGetRandom = getEntropy
    16  }
    17  
    18  func getEntropy(p []byte) (ok bool) {
    19  	// getentropy(2) returns a maximum of 256 bytes per call
    20  	for i := 0; i < len(p); i += 256 {
    21  		end := i + 256
    22  		if len(p) < end {
    23  			end = len(p)
    24  		}
    25  		err := unix.GetEntropy(p[i:end])
    26  		if err != nil {
    27  			return false
    28  		}
    29  	}
    30  	return true
    31  }
    32  

View as plain text