Source File
rand.go
Belonging Package
crypto/rand
// Copyright 2010 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// Package rand implements a cryptographically secure// random number generator.package randimport (_)// Reader is a global, shared instance of a cryptographically// secure random number generator. It is safe for concurrent use.//// - On Linux, FreeBSD, Dragonfly, and Solaris, Reader uses getrandom(2).// - On legacy Linux (< 3.17), Reader opens /dev/urandom on first use.// - On macOS, iOS, and OpenBSD Reader, uses arc4random_buf(3).// - On NetBSD, Reader uses the kern.arandom sysctl.// - On Windows, Reader uses the ProcessPrng API.// - On js/wasm, Reader uses the Web Crypto API.// - On wasip1/wasm, Reader uses random_get.//// In FIPS 140-3 mode, the output passes through an SP 800-90A Rev. 1// Deterministric Random Bit Generator (DRBG).var Reader io.Readerfunc init() {if boring.Enabled {Reader = boring.RandReaderreturn}Reader = &reader{}}type reader struct {drbg.DefaultReader}func ( *reader) ( []byte) ( int, error) {boring.Unreachable()if fips140.Enabled {drbg.Read()} else {sysrand.Read()}return len(), nil}// fatal is [runtime.fatal], pushed via linkname.////go:linkname fatalfunc fatal(string)// Read fills b with cryptographically secure random bytes. It never returns an// error, and always fills b entirely.//// Read calls [io.ReadFull] on [Reader] and crashes the program irrecoverably if// an error is returned. The default Reader uses operating system APIs that are// documented to never return an error on all but legacy Linux systems.func ( []byte) ( int, error) {// We don't want b to escape to the heap, but escape analysis can't see// through a potentially overridden Reader, so we special-case the default// case which we can keep non-escaping, and in the general case we read into// a heap buffer and copy from it.if , := Reader.(*reader); {_, = .Read()} else {:= make([]byte, len())_, = io.ReadFull(Reader, )copy(, )}if != nil {fatal("crypto/rand: failed to read random data (see https://go.dev/issue/66821): " + .Error())panic("unreachable") // To be sure.}return len(), nil}
![]() |
The pages are generated with Golds v0.7.9-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |