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
/// Zero out memory using `memset_s()`.
///
/// Unlike `memset()`, any call to the `memset_s()` function shall be
/// evaluated strictly, i.e. callers of `memset_s()` can safely assume that
/// it has been executed and not "optimized away" by the compiler.
pub fn secure_zero_memory(bytes: &mut [u8]) {
    #[link(name = "c")]
    extern "C" {
        fn memset_s(dest: *mut u8, dest_len: usize, byte: isize, n: usize) -> isize;
    }

    unsafe {
        assert_eq!(memset_s(bytes.as_mut_ptr(), bytes.len(), 0, bytes.len()), 0);
    }
}