Securely wipe sensitive données de memory
Cryptographic clés et plaintext doit être zeroed de memory après utiliser. Apprendre pourquoi memset est non enough et comment en prevent compiler optimization.
- 1
After using a cryptographic key or plaintext, it must be overwritten with zeros in memory. Simply letting the variable go out of scope is insufficient — the data remains in memory until the page is reused. The challenge: compilers optimize away memset(key, 0, len) if the key is never read again.
- 2 Calculatrice de hash →
Hash sensitive data before processing if you need to retain a non-reversible representation. The hash output replaces the plaintext in memory — the original is wiped after hashing.
- 3 Encodeur Base64 →
Coder sensitive data for transmission, then wipe the original plaintext from memory. The encoded form is not the secret — it is the transport representation. Wipe both after use.
- 4
In C/C++, use explicit_bzero() or a volatile pointer to prevent the compiler from optimizing away the zeroing. In JavaScript, the language does not guarantee memory zeroing — use Uint8Array and overwrite all bytes, but be aware the GC may not immediately reclaim the memory. For truly sensitive operations, use a Web Worker and terminate it after use.
Questions fréquentes
- Why does the compiler optimize away memset?
- If the compiler sees that the buffer is never read after memset(buf, 0, len), it considers the zeroing a "dead store" and removes it for performance. Functions like explicit_bzero use volatile semantics to prevent this optimization.
- Is this relevant in JavaScript?
- JavaScript strings are immutable and cannot be zeroed — avoid storing secrets in strings. Use Uint8Array (mutable) and overwrite all bytes. However, the GC may copy data during compaction, leaving copies in memory. For maximum security, use a Worker and terminate it, or use WASM with manual memory management.