security_framework/
random.rs1use security_framework_sys::random::{kSecRandomDefault, SecRandomCopyBytes, SecRandomRef};
4use std::io;
5
6pub struct SecRandom(SecRandomRef);
8
9unsafe impl Sync for SecRandom {}
10unsafe impl Send for SecRandom {}
11
12impl Default for SecRandom {
13 #[inline(always)]
14 fn default() -> Self {
15 unsafe { Self(kSecRandomDefault) }
16 }
17}
18
19impl SecRandom {
20 pub fn copy_bytes(&self, buf: &mut [u8]) -> io::Result<()> {
22 if unsafe { SecRandomCopyBytes(self.0, buf.len(), buf.as_mut_ptr().cast()) } == 0 {
23 Ok(())
24 } else {
25 Err(io::Error::last_os_error())
26 }
27 }
28}
29
30#[cfg(test)]
31mod test {
32 use super::*;
33
34 #[test]
35 fn basic() {
36 let mut buf = [0; 10];
37 SecRandom::default().copy_bytes(&mut buf).unwrap();
38 }
39}