Unified secure storage with built-in encryption and pluggable backends
dart pub add philiprehberger_secure_storeUnified secure storage with built-in encryption and pluggable backends
Add to your pubspec.yaml:
dependencies:
philiprehberger_secure_store: ^0.1.0
Then run:
dart pub get
import 'package:philiprehberger_secure_store/secure_store.dart';
final store = SecureStore(encryptionKey: 'my-secret-key');
await store.write('token', 'eyJhbGciOiJIUzI1...');
final token = await store.read('token');
await store.writeJson('user', {'name': 'Alice', 'role': 'admin'});
final user = await store.readJson('user');
print(user!['name']); // Alice
await store.writeBool('dark_mode', true);
await store.writeInt('login_count', 42);
final darkMode = await store.readBool('dark_mode'); // true
final count = await store.readInt('login_count'); // 42
For non-web platforms, import the file backend separately:
import 'package:philiprehberger_secure_store/secure_store.dart';
import 'package:philiprehberger_secure_store/file_backend.dart';
final store = SecureStore(
encryptionKey: 'my-key',
backend: FileBackend('/path/to/storage.json'),
);
await store.write('token', 'secret');
// Data persists across restarts
Implement StorageBackend for any storage system:
class MyDatabaseBackend implements StorageBackend {
@override
Future<String?> read(String key) async { /* ... */ }
@override
Future<void> write(String key, String value) async { /* ... */ }
// ... implement all methods
}
final store = SecureStore(
encryptionKey: 'key',
backend: MyDatabaseBackend(),
);
await store.containsKey('token'); // true
await store.allKeys(); // ['token', 'user', ...]
await store.delete('token');
await store.clear(); // remove everything
// Strict access — throws if key missing
final value = await store.readOrThrow('token');
SecureStore| Method | Description |
|---|---|
SecureStore(encryptionKey:, backend:) | Create with encryption key and optional backend |
.write(key, value) | Store an encrypted string |
.read(key) | Read and decrypt a string (null if missing) |
.writeJson(key, value) | Store an encrypted JSON object |
.readJson(key) | Read and decrypt a JSON object |
.writeBool(key, value) | Store an encrypted boolean |
.readBool(key) | Read a boolean |
.writeInt(key, value) | Store an encrypted integer |
.readInt(key) | Read an integer |
.delete(key) | Delete a key |
.containsKey(key) | Check if a key exists |
.allKeys() | List all stored keys |
.clear() | Delete all data |
.readOrThrow(key) | Read or throw KeyNotFoundError |
| Backend | Description |
|---|---|
MemoryBackend | In-memory (default, for testing) |
FileBackend(path) | JSON file on disk |
StorageBackend | Abstract interface for custom backends |
dart pub get
dart analyze --fatal-infos
dart test
If you find this project useful: