aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/crypto/rand/rand_arc4random.go
blob: 8bfcbe8725104057d7bb66dc4d1b7f859f16cd0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//go:build darwin || tinygo.wasm
// +build darwin tinygo.wasm

// This implementation of crypto/rand uses the arc4random_buf function
// (available on both MacOS and WASI) to generate random numbers.
//
// Note: arc4random_buf (unlike what the name suggets) does not use the insecure
// RC4 cipher. Instead, it uses a high-quality cipher, varying by the libc
// implementation.

package rand

import "unsafe"

func init() {
	Reader = &reader{}
}

type reader struct {
}

func (r *reader) Read(b []byte) (n int, err error) {
	if len(b) != 0 {
		libc_arc4random_buf(unsafe.Pointer(&b[0]), uint(len(b)))
	}
	return len(b), nil
}

// void arc4random_buf(void *buf, size_t buflen);
//export arc4random_buf
func libc_arc4random_buf(buf unsafe.Pointer, buflen uint)