secure-types 0.2.63

Secure data types that protect sensitive data in memory via locking and zeroization.
Documentation
use secure_types::SecureString;

fn main() {
   // Obviously this is an example, you should not hardcode sensitive data in your code
   let exposed_string = String::from("my_secret");
   let mut secure_string = SecureString::from(exposed_string); // exposed string is moved into the SecureString

   // The memory is locked and protected here. Direct access is not possible.

   // Use a scope to safely access the content as a &str.
   secure_string.unlock_str(|unlocked_str| {
      assert_eq!(unlocked_str, "my_secret");
      println!("The secret is: {}", unlocked_str);
   }); // The memory is automatically locked again when the scope ends.

   // Use mut scope if you need to pass it as a mutable reference
   secure_string.push_str("_password");

   secure_string.unlock_str(|unlocked_str| {
      assert_eq!(unlocked_str, "my_secret_password");
      println!("The secret is: {}", unlocked_str);
   });

   // Everytime we access the SecureString we are unlocking the memory and making it accessible.
   // Any operations you do during that time ideally should be very fast so you don't keep the data exposed for too long.
}