pub struct SecureArray<T, const LENGTH: usize>where
T: Zeroize,{ /* private fields */ }
Expand description
A fixed-size array allocated in a secure memory region.
SecureArray
provides the same core security guarantees as the other types in this
crate, including zeroization on drop and optional memory locking/encryption when
It is ideal for secrets of a known, fixed length.
§Program Termination
Direct indexing (e.g., array[0]
) on a locked array will cause the operating system
to terminate the process with an access violation error. Always use the provided
scope methods (unlocked_scope
, unlocked_mut_scope
) for safe access.
§Examples
use secure_types::SecureArray;
let key_data = [1u8; 32];
let secure_key: SecureArray<u8, 32> = SecureArray::new(key_data).unwrap();
secure_key.unlocked_scope(|unlocked_slice| {
assert_eq!(unlocked_slice.len(), 32);
assert_eq!(unlocked_slice[0], 1);
});
Implementations§
Source§impl<T, const LENGTH: usize> SecureArray<T, LENGTH>where
T: Zeroize,
impl<T, const LENGTH: usize> SecureArray<T, LENGTH>where
T: Zeroize,
Sourcepub fn empty() -> Result<Self, Error>
pub fn empty() -> Result<Self, Error>
Creates an empty (but allocated) SecureArray. The memory is allocated but not initialized, and it’s the caller’s responsibility to fill it.
Sourcepub fn new(content: [T; LENGTH]) -> Result<Self, Error>
pub fn new(content: [T; LENGTH]) -> Result<Self, Error>
Creates a new SecureArray from a given array.
pub fn len(&self) -> usize
pub fn as_ptr(&self) -> *const T
pub fn as_mut_ptr(&mut self) -> *mut u8
Sourcepub fn unlocked_scope<F, R>(&self, f: F) -> R
pub fn unlocked_scope<F, R>(&self, f: F) -> R
Provides scoped, immutable access to the array’s data as a slice.
Sourcepub fn unlocked_mut_scope<F, R>(&mut self, f: F) -> R
pub fn unlocked_mut_scope<F, R>(&mut self, f: F) -> R
Provides scoped, mutable access to the array’s data as a mutable slice.