| 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 | |
| 5 | module rand |
| 6 | |
| 7 | #include <sys/random.h> |
| 8 | |
| 9 | fn C.getentropy(buf voidptr, buflen usize) i32 |
| 10 | |
| 11 | // read returns an array of `bytes_needed` random bytes read from the OS. |
| 12 | pub 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 | |