A Python library for secure file deletion, in-memory secret handling and cryptographic erasure.
Project description
ZeroizePy
High-assurance secure deletion, secure memory handling and cryptographic erasure for Python.
ZeroizePy provides a modern, cross-platform suite of primitives for handling sensitive data safely. It is designed for applications that require defense-in-depth: password managers, HSM glue code, data-at-rest protection, secure messaging and high-security Python systems.
It includes:
- Locked, zeroizable RAM (SecureMemory)
- Secure file wiping (multi-pass overwrite, free-space wiping)
- Cryptographic erasure (destroy a key → destroy the data)
- AES-GCM authenticated encryption helpers
- Secure temporary sessions (auto-delete on exit)
Installation
pip install zeroizepy
Quick Start (Hello World)
from zeroizepy.file import secure_delete
from zeroizepy.crypto import CryptoKey, encrypt_data
# Securely delete a file
secure_delete("secret.txt", passes=3)
# Encrypt something with a secure key
key = CryptoKey.generate(32)
ct = encrypt_data(b"hello world", key)
print("Encrypted:", ct.ciphertext.hex())
Overview of Protection Layers
ZeroizePy implements five protection layers:
-
Secure Memory
- Explicitly zeroizable buffers
- Locked RAM (non-swappable when libsodium is available)
- Safe handling for sensitive in-process secrets
- Guaranteed wipe on .close() or context exit
-
File Wiping
- Multi-pass secure deletion: random or fixed patterns (
secure_delete()) - Full overwrite of file contents before unlinking
- Free-space wiping (
wipe_free_space()) to overwrite unallocated disk blocks - Symlink-aware deletion controls
- Multi-pass secure deletion: random or fixed patterns (
-
Cryptographic Erasure
- AES-GCM encryption with authenticated metadata (
CryptoKey) CryptoKeyobjects that can be destroyed in memory- Destroy the key → all encrypted data irreversibly lost
- Designed for SSDs, COW filesystems, and other overwrite-hostile storage
- AES-GCM encryption with authenticated metadata (
-
Secure Temporary Sessions
SecureSessiontracks temporary files, memory regions and secrets- Automatically zeroes memory and deletes files on exit
- Ideal for one-shot secure operations
-
OS-Level Erase Wrappers (Advanced)
- Interfaces for: hdparm, NVMe Secure Erase, APFS diskutil, BitLocker
- Extremely dangerous and destructive if misused — disabled by default
- Intended for expert operators only
Quick Examples
Cryptography Module
Generate Key, Encrypt, and Cryptographically Erase
from zeroizepy.crypto import CryptoKey, encrypt_data, decrypt_data, cryptographic_erase_key
from zeroizepy.utils import secure_clear
key = CryptoKey.generate(32)
with open("secret.txt", "rb") as f:
plaintext = bytearray(f.read())
ct = encrypt_data(plaintext, key)
with open("secret.enc", "wb") as f:
f.write(ct.nonce + ct.ciphertext)
secure_clear(plaintext)
recovered = decrypt_data(ct, key)
print("Recovered:", recovered.decode())
cryptographic_erase_key(key)
Encrypt/Decrypt with AAD
key = CryptoKey.generate()
pt = b"SENSITIVE-DATA"
aad = b"context-info"
ct = encrypt_data(pt, key, associated_data=aad)
recovered = decrypt_data(ct, key, associated_data=aad)
print(recovered)
Secure Memory
from zeroizepy.memory import SecureMemory, secret_bytes
s = SecureMemory.alloc(32)
s.write(b"supersecret")
print(s.read(11))
s.zero()
s.close()
sec = secret_bytes(b"topsecret")
print(sec.read(9))
sec.close()
File Wiping
from zeroizepy.file import secure_delete, wipe_free_space
secure_delete("secret.txt", passes=3, pattern="random")
wipe_free_space("/tmp", dry_run=True)
Secure Session
from zeroizepy.session import SecureSession
with SecureSession() as session:
temp_file = session.create_temp_file(".txt")
secret = session.create_secret(b"password123")
with open(temp_file, "wb") as f:
f.write(secret.get_bytes())
# On exit: memory zeroed, temp files deleted
Limitations & Security Notes
Cross-Platform Notes
| Feature | POSIX (Linux/macOS) | Windows |
|---|---|---|
| Symlink Handling | Fully supported; follow_symlinks honored |
Some behaviors differ; tests skipped where behavior differs |
| Sparse File Detection | Heuristics applied | Sparse heuristics differ; warnings may differ |
chmod(0) Permission Model |
Enforced; deletion may raise FileAccessError |
Behavior differs; some tests skipped |
| SecureMemory Zeroing | Zeroing observable in tests | Observing zeroing is unreliable due to Python memory copies and OS protections |
| Memory Locking | mlock available (libsodium recommended) |
VirtualLock less effective; libsodium strongly preferred |
- Immutable Python objects (
bytes,str) cannot be zeroed. Preferbytearrayormemoryview - Libsodium recommended for locked memory
- Always use
.close()or context manager for memory zeroing - File system or OS limitations may prevent complete erasure on SSDs or COW filesystems
Threat Model
ZeroizePy provides high-assurance secure deletion primitives, but only within realistic limits of modern operating systems.
ZeroizePy Defends Against:
- Forensic recovery of overwritten files
- Accidental retention of plaintext in process memory
- Recovery after cryptographic key destruction
- Undeleted temp files and leaked memory buffers
- Users mistakenly loading sensitive data into Python
bytes
ZeroizePy cannot defend against:
- Full-disk snapshots (btrfs, ZFS, VM snapshots)
- SSD wear-leveling remapping (overwriting does not guarantee physical erasure)
- Kernel-level memory scanners
- DMA attacks or root access attackers
- Malware that runs inside the same Python process
- Cloud-provider persistent block-level backups you do not control
Partial Mitigations
| Risk | What ZeroizePy Does |
|---|---|
| SSD wear-leveling | Recommend crypto-erasure |
| Python copies memory | Encourages bytearray/memoryview |
| OS won’t allow locked pages | Falls back with warnings |
| Hard failures during deletion | Raises FileAccessError |
Testing
pytest
Some tests skip on Windows due to OS differences.
License
MIT License — free for commercial, open-source, academic, and integrated use.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zeroizepy-1.2.0.tar.gz.
File metadata
- Download URL: zeroizepy-1.2.0.tar.gz
- Upload date:
- Size: 26.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
239cc428a80032b7fd19fe9f65798d361004640330036af573285a44335be4c5
|
|
| MD5 |
3707adbb6a1377d631f0f8e7e2d683ad
|
|
| BLAKE2b-256 |
766284d0bc1317e9b4b664326f6881cc246b671856deca8309e1b13ef729ab4a
|
File details
Details for the file zeroizepy-1.2.0-py3-none-any.whl.
File metadata
- Download URL: zeroizepy-1.2.0-py3-none-any.whl
- Upload date:
- Size: 22.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79bf9dcb1a04cc43f402b52fe10d6bd1f31a56e059d045fde36a0c179433ff39
|
|
| MD5 |
9fd6e1b0206b7a927d6dad4513c39421
|
|
| BLAKE2b-256 |
c39dd7ad04f047c20e58c993360a903cd926530e66430ac76b176e3430efe0bc
|