pub struct SecureRandom;Expand description
Stateless wrapper around Apple’s default CSPRNG.
Implementations§
Source§impl SecureRandom
impl SecureRandom
Sourcepub fn fill(buffer: &mut [u8]) -> Result<()>
pub fn fill(buffer: &mut [u8]) -> Result<()>
Fill an existing buffer with cryptographically secure random bytes.
§Errors
Returns an error if Security.framework rejects the random-byte request.
Sourcepub fn bytes(length: usize) -> Result<Vec<u8>>
pub fn bytes(length: usize) -> Result<Vec<u8>>
Allocate a new Vec<u8> and fill it with cryptographically secure random bytes.
§Errors
Returns an error if Security.framework rejects the random-byte request.
Examples found in repository?
examples/01_smoke.rs (line 18)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let account = "doom-fish-smoke";
5 let service = format!("doom-fish-smoke-test-{}", std::process::id());
6
7 let _ = Keychain::delete(account, &service);
8 Keychain::set(account, &service, "hunter2")?;
9
10 let value = Keychain::get(account, &service)?;
11 assert_eq!(value, "hunter2");
12
13 let accounts = Keychain::list_accounts(&service)?;
14 assert!(accounts.iter().any(|candidate| candidate == account));
15
16 Keychain::delete(account, &service)?;
17
18 let random = SecureRandom::bytes(32)?;
19 assert!(random.iter().any(|&byte| byte != 0));
20
21 println!("✅ security keychain + RNG OK");
22 Ok(())
23}Auto Trait Implementations§
impl Freeze for SecureRandom
impl RefUnwindSafe for SecureRandom
impl Send for SecureRandom
impl Sync for SecureRandom
impl Unpin for SecureRandom
impl UnsafeUnpin for SecureRandom
impl UnwindSafe for SecureRandom
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more