zeroize 0.1.2

(Alpha quality preview) Securely zero memory while avoiding compiler optimizations: unified 'secure_zero_memory()' wrapper for secure intrinsic functions for zeroing memory, using FFI to invoke OS intrinsics on stable (with support for Linux, Windows, OS X/iOS, FreeBSD, OpenBSD, NetBSD, DragonflyBSD), or the unstable 'volatile_set_memory()` intrinsic on nightly. No insecure fallbacks, no dependencies, no std, no functionality besides securely zeroing memory.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/// Zero out memory using `explicit_bzero()`.
///
/// The `explicit_bzero()` is a non-standard function which performs the
/// same task as `bzero()`, but differs in that it guarantees that compiler
/// optimizations will not remove the erase operation if the compiler
/// deduces that the operation is "unnecessary".
pub fn secure_zero_memory(bytes: &mut [u8]) {
    #[cfg_attr(not(target_os = "windows"), link(name = "c"))]
    extern "C" {
        fn explicit_bzero(dest: *mut u8, n: usize);
    }

    unsafe {
        explicit_bzero(bytes.as_mut_ptr(), bytes.len());
    }
}