A production-ready, secure file storage system in Rust with military-grade encryption, optional compression, and zero-copy operations.
- XChaCha20-Poly1305 AEAD Encryption - Extended nonces, authenticated encryption for maximum security
- Optional Gzip Compression - Reduce storage footprint before encryption
- Secure Key Management - Cryptographically secure random key generation with OS-level RNG
- Zero Memory Leaks - Automatic key zeroization on drop using
zeroize - Async I/O - Built on Tokio for high-performance file operations
- Configurable Storage - Flexible storage directory and key file paths
- Metadata Tracking - Built-in file metadata support
Add to your Cargo.toml:
[dependencies]
securefs = { git = "https://github.com/HueCodes/Rust-Lock.git" }
tokio = { version = "1", features = ["full"] }use anyhow::Result;
use securefs::{config::Config, key_manager::KeyManager, storagefile_ops::SecureFileOps};
#[tokio::main]
async fn main() -> Result<()> {
// Load configuration
let cfg = Config::load("config.json")?;
// Initialize key manager and file operations
let km = KeyManager::new(&cfg)?;
let fs = SecureFileOps::new(km, cfg.storage_dir.clone())
.with_compression(true); // Enable compression
// Write encrypted data
fs.write_encrypted("secret.txt", b"Top secret data").await?;
// Read and decrypt
let data = fs.read_encrypted("secret.txt").await?;
println!("Decrypted: {}", String::from_utf8_lossy(&data));
Ok(())
}Create a config.json file:
{
"key_path": "./securefs.key",
"storage_dir": "./storage"
}The KeyManager handles secure key generation and storage:
- Automatic Generation - Creates a 256-bit key if none exists
- Secure Storage - Keys stored with restrictive file permissions (0600)
- Memory Safety - Keys automatically zeroized when dropped
- Validation - Ensures key file integrity on load
let km = KeyManager::new(&config)?;
// Key is securely loaded and ready for encryption operations
// Automatically cleaned from memory when km goes out of scopeUses XChaCha20-Poly1305 for authenticated encryption:
- 24-byte nonces - Extended nonce space prevents reuse
- AEAD - Authenticated Encryption with Associated Data
- Tag verification - Prevents tampering and corruption
let fs = SecureFileOps::new(km, storage_dir).with_compression(true);
fs.write_encrypted("large_file.txt", &large_data).await?;Data flow: plaintext → gzip → encrypt → storage
let fs = SecureFileOps::new(km, storage_dir);
fs.write_encrypted("file.txt", &data).await?;Data flow: plaintext → encrypt → storage
┌─────────────────────────────────────────┐
│ SecureFileOps │
│ - write_encrypted() │
│ - read_encrypted() │
│ - with_compression() │
└──────────────┬──────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Encryptor │
│ - XChaCha20-Poly1305 cipher │
│ - Nonce generation │
│ - AEAD operations │
└──────────────┬──────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ KeyManager │
│ - Secure key generation (OsRng) │
│ - Key loading/validation │
│ - Automatic zeroization │
└─────────────────────────────────────────┘
Run the test suite:
cargo testIntegration tests verify:
- Full encrypt/decrypt roundtrip
- Compression functionality
- Key management
- File operations
- Encryption overhead: ~microseconds per operation
- Compression ratio: Varies by data (text: ~60-70% reduction)
- Memory footprint: Minimal, keys are only 32 bytes
- Async I/O: Non-blocking file operations scale with workload
- No encryption key in memory longer than needed
- Nonces never reused (24-byte XChaCha20 nonces)
- Authenticated encryption prevents tampering
- Secure key generation using OS entropy (
OsRng) - Restrictive permissions on key files (Unix: 0600)
pub struct Config {
pub key_path: String, // Path to encryption key file
pub storage_dir: String, // Directory for encrypted files
}Load from JSON:
let cfg = Config::load("config.json")?;# Development build
cargo build
# Release build (optimized)
cargo build --release
# Run example
cargo run --bin securefsContributions welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'feat: add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
MIT License - see LICENSE for details
- Repository: https://github.com/HueCodes/Rust-Lock
- Issues: https://github.com/HueCodes/Rust-Lock/issues
This is cryptographic software. While it uses industry-standard algorithms and best practices, it has not been independently audited. Use in production at your own risk.
Built with 🦀 Rust