Skip to content

Instantly share code, notes, and snippets.

@benma
Last active October 17, 2020 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benma/463ef5459bef29c7d47591c5c1cff906 to your computer and use it in GitHub Desktop.
Save benma/463ef5459bef29c7d47591c5c1cff906 to your computer and use it in GitHub Desktop.
use secrecy::{ExposeSecret, Secret};
#[derive(Debug)]
struct EncryptionKey(Secret<[u8; 32]>);
fn get_encryption_key() -> EncryptionKey {
let key = EncryptionKey(Secret::new(*b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
println!("Pointer at creation: {:p}", key.0.expose_secret().as_ptr());
key
}
fn main() {
let encryption_key = get_encryption_key();
let ptr = encryption_key.0.expose_secret().as_ptr();
println!(
"Pointer when using: {:p}",
encryption_key.0.expose_secret().as_ptr()
);
println!("Using key to encrypt stuff: {:?}", encryption_key);
println!("About to drop.");
drop(encryption_key);
println!("Dropped.");
println!("memory: {:?}", unsafe {
core::slice::from_raw_parts(ptr, 32)
});
}
/// Output:
/// Pointer at creation: 0x7ffc1c00d2b8
/// Pointer when using: 0x7ffc1c00d2b8
/// Using key to encrypt stuff: EncryptionKey(Secret([REDACTED [u8; 32]]))
/// About to drop.
/// Dropped.
/// memory: [97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment