diff --git a/vlib/crypto/rand/rand_freebsd.c.v b/vlib/crypto/rand/rand_freebsd.c.v new file mode 100644 index 0000000000..4d4d5e5cf7 --- /dev/null +++ b/vlib/crypto/rand/rand_freebsd.c.v @@ -0,0 +1,16 @@ +// Copyright (c) 2022 John Lloyd. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. + +module rand + +#include + +fn C.arc4random_buf(p &byte, n usize) + +// read returns an array of `bytes_needed` random bytes read from the OS. +pub fn read(bytes_needed int) ?[]u8 { + mut buffer := unsafe { malloc_noscan(bytes_needed) } + C.arc4random_buf(buffer, bytes_needed) + return unsafe { buffer.vbytes(bytes_needed) } +} diff --git a/vlib/crypto/rand/rand_openbsd.c.v b/vlib/crypto/rand/rand_openbsd.c.v new file mode 100644 index 0000000000..4d4d5e5cf7 --- /dev/null +++ b/vlib/crypto/rand/rand_openbsd.c.v @@ -0,0 +1,16 @@ +// Copyright (c) 2022 John Lloyd. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. + +module rand + +#include + +fn C.arc4random_buf(p &byte, n usize) + +// read returns an array of `bytes_needed` random bytes read from the OS. +pub fn read(bytes_needed int) ?[]u8 { + mut buffer := unsafe { malloc_noscan(bytes_needed) } + C.arc4random_buf(buffer, bytes_needed) + return unsafe { buffer.vbytes(bytes_needed) } +}