v / vlib / crypto / rand / rand_darwin.c.v
19 lines · 15 sloc · 519 bytes · a87a4d73b9ab25cfff0822f4e94cf2a2d9e64323
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. 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 <sys/random.h>
8
9fn C.getentropy(buf voidptr, buflen usize) i32
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 := []u8{len: bytes_needed}
14 status := C.getentropy(buffer.data, bytes_needed)
15 if status != 0 {
16 return &ReadError{}
17 }
18 return buffer
19}
20