memguard 2.1.4
memguard: ^2.1.4 copied to clipboard
Flutter library that safeguards memory, resources, and secure storage with Rust-backed performance.
π‘οΈ MemGuard #
Hybrid secure storage for Flutter with zero-leak memory protection and hardware-backed encryption.
MemGuard combines ultra-fast Rust FFI memory storage with hardware-backed platform security (Android KeyStore, iOS Keychain) to give you the best of both worlds: blazing performance for ephemeral data and military-grade protection for persistent secrets.
β¨ Features #
π Dual Storage Modes #
Memory Storage (Rust FFI)
- β‘ Zero-copy direct access β no Dart VM overhead
- π₯ Ephemeral by design β data vanishes on app close
- π§ Protected memory β explicit zeroing prevents leaks
- π Optional encryption β AES-256-GCM in Rust
- π― Perfect for: Session tokens, API keys, temporary credentials
Device Secure Storage (Platform Channels)
- π Hardware-backed encryption β Android KeyStore (StrongBox/TEE)
- πΎ Persistent across restarts β survives app kills
- π‘οΈ Zero platform channel leaks β only boolean flags transmitted
- ποΈ Disk-backed with Rust cache β fast reads after first access
- π― Perfect for: User credentials, refresh tokens, sensitive settings
π Zero-Leak Architecture #
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β YOUR DART CODE β
β β’ Never touches sensitive data directly β
β β’ Only receives boolean status flags β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β MemGuard Plugin β β You are here
β (Dart API Layer) β
βββββββββββ¬ββββββββββββ
β
βββββββββββ΄ββββββββββ
βΌ βΌ
ββββββββββββββββββββ ββββββββββββββββββββββ
β Rust FFI β β Platform Channel β
β (Memory Cache) β β (Kotlin/Swift) β
β β β β
β β’ Direct access β β β’ KeyStore/Keychainβ
β β’ Zero-copy β β β’ Encrypted files β
β β’ Protected mem β β β’ No plaintext β
ββββββββββββββββββββ ββββββββββββββββββββββ
Critical Design Decision:
- Memory mode: Data lives ONLY in Rust β Dart never sees plaintext
- Secure mode: Platform channels return
true/false/nullβ Dart fetches from Rust cache - Result: Zero attack surface in Dart VM and platform channels
π Quick Start #
Installation #
dependencies:
memguard: ^2.1.4
Basic Usage #
import 'package:memguard/memguard.dart';
void main() {
runApp(
MemGuard(
// Choose your storage mode
storageType: StorageType.memory, // or StorageType.deviceSecure
// Memory options (for memory mode)
enableEncryptionMemory: true,
autoCleanupMemory: true,
cleanupIntervalMemory: Duration(minutes: 10),
// Debug logging
showLog: true,
child: MyApp(),
),
);
}
// Then use anywhere in your app:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
Future<void> saveToken() async {
// Store securely
await MemGuardStatic.store('auth_token', 'super_secret_jwt');
// Retrieve
final token = await MemGuardStatic.retrieve('auth_token');
print('Token: $token');
// Check existence
final exists = await MemGuardStatic.contains('auth_token');
// Delete
await MemGuardStatic.delete('auth_token');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('MemGuard Demo')),
body: Center(
child: ElevatedButton(
onPressed: saveToken,
child: Text('Test Secure Storage'),
),
),
);
}
}
π API Reference #
Storage Operations #
// Store data
await MemGuardStatic.store('key', 'value');
// Retrieve data
String? value = await MemGuardStatic.retrieve('key');
// Check if key exists
bool exists = await MemGuardStatic.contains('key');
// Delete key
await MemGuardStatic.delete('key');
// Get storage statistics
Map<String, dynamic> stats = await MemGuardStatic.getStats();
// Cleanup memory (memory mode only)
MemGuardStatic.cleanupMemory();
// Cleanup all storage
MemGuardStatic.cleanupAll();
Advanced: Zero-Copy Operations (Memory Mode Only) #
For maximum performance, access data directly in Rust memory without copying to Dart:
// Perform cryptographic operations without copying to Dart
final signature = await MemGuardStatic.performSecureOperation<String>(
'private_key',
(dataPtr, length) {
// Direct pointer access β zero copies!
final bytes = dataPtr.asTypedList(length);
// Perform operation (e.g., sign with private key)
final signature = yourCryptoLib.sign(bytes, message);
return signature;
// Buffer automatically zeroed after this scope
},
);
Use cases:
- Cryptographic signing without key exposure
- Hash computation on sensitive data
- Encryption/decryption with in-memory keys
- Any operation where data must never touch Dart heap
π― Storage Mode Comparison #
| Feature | Memory Storage | Device Secure Storage |
|---|---|---|
| Speed | β‘ Instant (Rust FFI) | π Fast (disk + cache) |
| Persistence | β Ephemeral | β Survives restarts |
| Encryption | Optional (Rust AES) | β Hardware-backed |
| Auto-cleanup | β Configurable | β Manual only |
| Zero-copy | β Yes | β No |
| Best for | Session data | Long-term secrets |
When to Use Memory Storage #
MemGuard(
storageType: StorageType.memory,
enableEncryptionMemory: true,
autoCleanupMemory: true,
cleanupIntervalMemory: Duration(minutes: 5),
child: MyApp(),
)
β Perfect for:
- Session tokens (expire on app close)
- Temporary API keys
- In-flight credentials
- Crypto operations (use
performSecureOperation) - Any data that shouldn't persist
When to Use Device Secure Storage #
MemGuard(
storageType: StorageType.deviceSecure,
child: MyApp(),
)
β Perfect for:
- User login credentials
- OAuth refresh tokens
- Biometric authentication keys
- App settings with sensitive data
- Anything that must survive app restart
π§ Configuration Options #
Memory Storage Configuration #
MemGuard(
storageType: StorageType.memory,
// Enable AES-256-GCM encryption in Rust
enableEncryptionMemory: true,
// Auto-cleanup on app background/close
autoCleanupMemory: true,
// How often to run cleanup (if auto enabled)
cleanupIntervalMemory: Duration(minutes: 10),
// Enable debug logging
showLog: true,
child: MyApp(),
)
Device Secure Storage Configuration #
MemGuard(
storageType: StorageType.deviceSecure,
// Enable Rust cache for faster reads (recommended)
enableEncryptionMemory: true,
// Enable debug logging
showLog: true,
child: MyApp(),
)
π‘οΈ Security Features #
What MemGuard Protects Against #
β
Memory Dumps β Sensitive data never stored in Dart VM heap
β
Platform Channel Interception β Only boolean flags transmitted
β
Heap Inspection β Rust memory is protected and zeroed
β
Data Tampering β GCM authentication tags verify integrity
β
Key Extraction β Hardware-backed keys never leave secure enclave
β
Root Access (partial) β Keys resist extraction even on rooted devices
Attack Resistance #
// β BAD: Traditional secure storage
await secureStorage.write('token', 'secret'); // Plaintext in Dart heap!
// β
GOOD: MemGuard memory storage
await MemGuardStatic.store('token', 'secret'); // Only in Rust protected memory
// β
BETTER: MemGuard device secure
MemGuard(storageType: StorageType.deviceSecure, ...); // Hardware encryption + Rust cache
What MemGuard Does NOT Protect Against #
β Physical device seizure by sophisticated attackers
β Compromised OS/kernel (malware with root/system privileges)
β User-authorized access (screen recording, accessibility services)
β State-level adversaries with hardware forensics tools
Threat Model: MemGuard is designed for production app security against common attack vectors (malware, memory dumps, network interception). It is NOT a substitute for end-to-end encryption or protection against nation-state actors.
π Storage Statistics #
final stats = await MemGuardStatic.getStats();
print(stats);
// Memory Storage Output:
// {
// "items_count": 5,
// "total_size_bytes": 2048,
// "encrypted_items": 5,
// "platform": "android",
// "rust_version": "1.70.0",
// "storage_type": "memory"
// }
// Device Secure Storage Output:
// {
// "storage_type": "hardware_backed_keystore",
// "encryption_type": "aes_256_gcm",
// "key_strength": "256_bit",
// "rust_initialized": true,
// "items_count": 3,
// "total_size_bytes": 4096,
// "directory_path": "/data/user/0/com.app/files/memguard_secure",
// "timestamp": 1701234567890
// }
π¨ Convenience Extensions #
String Extension #
// Store string directly
await "my_secret_value".storeAs('api_key');
// Works with any storage type
await "refresh_token_xyz".storeAs('refresh_token');
Context Extension #
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Access MemGuard core directly
final core = context.memguard;
// Check initialization status
print('Storage type: ${core.currentStorageType}');
print('Rust ready: ${core.isRustInitialized}');
return Container();
}
}
π Lifecycle Management #
MemGuard automatically handles app lifecycle events:
// Memory Storage: Auto-cleanup on background
MemGuard(
storageType: StorageType.memory,
autoCleanupMemory: true, // β Cleans on app pause/background
child: MyApp(),
)
// Lifecycle events handled:
// β’ AppLifecycleState.paused β cleanupMemory()
// β’ AppLifecycleState.inactive β cleanupMemory()
// β’ AppLifecycleState.detached β cleanupAll()
Manual cleanup:
// Clean memory only (memory mode)
MemGuardStatic.cleanupMemory();
// Clean everything (all modes)
MemGuardStatic.cleanupAll();
π Error Handling #
try {
await MemGuardStatic.store('key', 'value');
} catch (e) {
if (e is StateError) {
// MemGuard not initialized
print('Initialize MemGuard first!');
} else if (e is UnsupportedError) {
// Feature not available (e.g., zero-copy on device secure)
print('Operation not supported for this storage type');
} else if (e is ArgumentError) {
// Storage type mismatch
print('Wrong storage type specified');
} else {
// Other errors (platform exceptions, etc.)
print('Storage error: $e');
}
}
Common errors:
StateError: MemGuard is not initializedβ Wrap your app inMemGuardwidgetUnsupportedError: Secure buffer operations only available for memory storageβ UseStorageType.memoryfor zero-copyArgumentError: Requested storage type does not matchβ Don't mix storage types in same session
π± Platform Support #
| Platform | Memory Storage | Device Secure | Status |
|---|---|---|---|
| Android | β Full | β Full | Tested β |
| iOS | β οΈ Untested | β οΈ Untested | Needs Testing |
| Windows | β οΈ Untested | β οΈ Untested | Needs Testing |
| macOS | β οΈ Untested | β οΈ Untested | Needs Testing |
| Linux | β Full | β Not Available | Tested β |
Android Requirements #
- Minimum: API 23 (Android 6.0 Marshmallow)
- Recommended: API 28+ (Android 9.0 Pie) for StrongBox support
π§ Linux Compatibility #
β Supported #
- Ubuntu 20.04+ β (22.04 tested)
- Debian 11+ β
- Fedora 34+ β
- Arch Linux β
- Manjaro β
- Pop!_OS 20.04+ β
- Linux Mint 20+ β
- Elementary OS 6+ β
Requires glibc 2.31+ and 64-bit (x86_64).
ποΈ Architecture #
MemGuard is built on top of MemGuard Core β the native Rust + Kotlin foundation that powers the zero-leak security model.
Stack:
- Dart Layer (this plugin): High-level Flutter API
- Rust FFI: Protected memory allocation and cryptography
- Kotlin Platform: Android KeyStore integration and encrypted file I/O
- Native Libraries: Compiled
.sobinaries for ARM/x86
See the MemGuard Core README for low-level implementation details.
π§ͺ Testing #
// Example test
void main() {
test('Memory storage lifecycle', () async {
MemGuardStatic.initMemoryStorage(
enableEncryptionMemory: true,
autoCleanupMemory: false,
cleanupIntervalMemory: Duration(minutes: 10),
);
await MemGuardStatic.store('test_key', 'test_value');
final value = await MemGuardStatic.retrieve('test_key');
expect(value, 'test_value');
await MemGuardStatic.delete('test_key');
final deleted = await MemGuardStatic.retrieve('test_key');
expect(deleted, isNull);
});
}
π€ Contributing #
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Areas for Contribution #
- β iOS/macOS platform implementation
- β Windows/Linux platform implementation
- β Additional encryption algorithms
- β Performance benchmarks
- β Integration tests
- β Documentation improvements
π Acknowledgments #
Built with:
- Rust FFI for memory-safe protected storage
- Android KeyStore for hardware-backed encryption
- flutter_fastlog for high-performance logging
- MemGuard Core for the native foundation
β οΈ Security Notice #
MemGuard implements defense-in-depth secure storage with zero-leak architecture. However, no client-side storage is absolutely secure. Always:
- β Validate critical operations server-side
- β Use certificate pinning for network requests
- β Implement request signing for API calls
- β Rotate sensitive credentials regularly
- β Enable biometric authentication when available
For high-security applications, combine MemGuard with additional layers of protection (HSM, remote attestation, etc.).
π Related Projects #
- MemGuard Core - Native Rust + Kotlin foundation
π¬ Support #
- π Issue Tracker
- π¬ Discussions