mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 14:33:41 +00:00
8e8ad9004f
This eliminates the 'wasi' build tag in favor of 'GOOS=wasip1', introduced in Go 1.21. For backwards compatablity, -target=wasi is a synonym for -target=wasip1.
39 lines
674 B
Go
39 lines
674 B
Go
//go:build linux && !baremetal && !wasip1
|
|
|
|
// This implementation of crypto/rand uses the /dev/urandom pseudo-file to
|
|
// generate random numbers.
|
|
// TODO: convert to the getentropy or getrandom libc function on Linux once it
|
|
// is more widely supported.
|
|
|
|
package rand
|
|
|
|
import (
|
|
"syscall"
|
|
)
|
|
|
|
func init() {
|
|
Reader = &reader{}
|
|
}
|
|
|
|
type reader struct {
|
|
fd int
|
|
}
|
|
|
|
func (r *reader) Read(b []byte) (n int, err error) {
|
|
if len(b) == 0 {
|
|
return
|
|
}
|
|
|
|
// Open /dev/urandom first if needed.
|
|
if r.fd == 0 {
|
|
fd, err := syscall.Open("/dev/urandom", syscall.O_RDONLY, 0)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
r.fd = fd
|
|
}
|
|
|
|
// Read from the file.
|
|
return syscall.Read(r.fd, b)
|
|
}
|