v2 / vlib / crypto / rand / rand_openbsd.c.v
16 lines · 12 sloc · 481 bytes · cc220e60a5a0cc787b68ae357c8ecfd2dc561b6f
Raw
1// Copyright (c) 2022 John Lloyd. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4
5module rand
6
7#include <stdlib.h>
8
9fn C.arc4random_buf(p &u8, n usize)
10
11// read returns an array of `bytes_needed` random bytes read from the OS.
12pub fn read(bytes_needed int) ![]u8 {
13 mut buffer := unsafe { malloc_noscan(bytes_needed) }
14 C.arc4random_buf(buffer, bytes_needed)
15 return unsafe { buffer.vbytes(bytes_needed) }
16}
17