// 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.//go:build unix// Unix cryptographically secure pseudorandom number// generator.package randimport ()const urandomDevice = "/dev/urandom"func init() {ifboring.Enabled {Reader = boring.RandReaderreturn }Reader = &reader{}}// A reader satisfies reads by reading from urandomDevicetype reader struct { f io.Reader mu sync.Mutex used atomic.Uint32// Atomic: 0 - never used, 1 - used, but f == nil, 2 - used, and f != nil}// altGetRandom if non-nil specifies an OS-specific function to get// urandom-style randomness.var altGetRandom func([]byte) (err error)func warnBlocked() {println("crypto/rand: blocked for 60 seconds waiting to read random data from the kernel")}func ( *reader) ( []byte) ( int, error) {boring.Unreachable()if .used.CompareAndSwap(0, 1) {// First use of randomness. Start timer to warn about // being blocked on entropy not being available. := time.AfterFunc(time.Minute, warnBlocked)defer .Stop() }ifaltGetRandom != nil && altGetRandom() == nil {returnlen(), nil }if .used.Load() != 2 { .mu.Lock()if .used.Load() != 2 { , := os.Open(urandomDevice)if != nil { .mu.Unlock()return0, } .f = hideAgainReader{} .used.Store(2) } .mu.Unlock() }returnio.ReadFull(.f, )}// hideAgainReader masks EAGAIN reads from /dev/urandom.// See golang.org/issue/9205type hideAgainReader struct { r io.Reader}func ( hideAgainReader) ( []byte) ( int, error) { , = .r.Read()iferrors.Is(, syscall.EAGAIN) { = nil }return}
The pages are generated with Goldsv0.7.0-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.