Skip to main content

memzero

Function memzero 

Source
pub fn memzero<T: Default + Clone>(slice: &mut [T])
Expand description

Securely zero a slice’s memory

This function securely zeroes a slice’s memory, ensuring that the operation won’t be optimized out by the compiler. This is important for securely clearing sensitive data from memory.

§Security Considerations

  • This function ensures that the memory is actually zeroed, even if the compiler would normally optimize out the operation
  • It should be used whenever a slice containing sensitive data (like cryptographic keys or passwords) is no longer needed
  • Regular assignment (e.g., slice.iter_mut().for_each(|x| *x = T::default())) might be optimized out by the compiler and not actually clear the memory

§Example

use libsodium_rs as sodium;
use libsodium_rs::utils::vec_utils;

// Create a slice with sensitive data
let mut secret_key = [0x01, 0x02, 0x03, 0x04];

// Use the key for some operation...

// Securely clear the key from memory when done
vec_utils::memzero(&mut secret_key);
assert_eq!(secret_key, [0, 0, 0, 0]);

§Arguments

  • slice - The slice to zero