...

Source file src/crypto/rand/rand.go

Documentation: crypto/rand

		 1  // Copyright 2010 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  // Package rand implements a cryptographically secure
		 6  // random number generator.
		 7  package rand
		 8  
		 9  import "io"
		10  
		11  // Reader is a global, shared instance of a cryptographically
		12  // secure random number generator.
		13  //
		14  // On Linux and FreeBSD, Reader uses getrandom(2) if available, /dev/urandom otherwise.
		15  // On OpenBSD, Reader uses getentropy(2).
		16  // On other Unix-like systems, Reader reads from /dev/urandom.
		17  // On Windows systems, Reader uses the RtlGenRandom API.
		18  // On Wasm, Reader uses the Web Crypto API.
		19  var Reader io.Reader
		20  
		21  // Read is a helper function that calls Reader.Read using io.ReadFull.
		22  // On return, n == len(b) if and only if err == nil.
		23  func Read(b []byte) (n int, err error) {
		24  	return io.ReadFull(Reader, b)
		25  }
		26  
		27  // batched returns a function that calls f to populate a []byte by chunking it
		28  // into subslices of, at most, readMax bytes.
		29  func batched(f func([]byte) error, readMax int) func([]byte) error {
		30  	return func(out []byte) error {
		31  		for len(out) > 0 {
		32  			read := len(out)
		33  			if read > readMax {
		34  				read = readMax
		35  			}
		36  			if err := f(out[:read]); err != nil {
		37  				return err
		38  			}
		39  			out = out[read:]
		40  		}
		41  		return nil
		42  	}
		43  }
		44  

View as plain text