+freebsd.ha (912B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use errors; 5 use io; 6 use rt; 7 8 // Fills the given buffer with cryptographically random data. If the system is 9 // unable to provide random data, abort. If you need to handle errors or want to 10 // use whatever random data the system can provide, even if less than the 11 // requested amount, use [[stream]] instead. 12 export fn buffer(buf: []u8) void = { 13 let n = 0z; 14 for (n < len(buf)) { 15 match (rt::getrandom(buf[n..]: *[*]u8, len(buf), 0)) { 16 case let err: rt::errno => 17 switch (err) { 18 case rt::EINTR => void; 19 case => 20 abort(); 21 }; 22 case let z: size => 23 n += z; 24 }; 25 }; 26 }; 27 28 fn rand_reader(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = { 29 assert(s == stream); 30 match (rt::getrandom(buf: *[*]u8, len(buf), 0)) { 31 case let err: rt::errno => 32 return errors::errno(err); 33 case let n: size => 34 return n; 35 }; 36 };