-
-
Save simonsan/ef145bacef1cc2d44ac7ed0f29995363 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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