A simple and secure storage system for Flutter. Supports Android, iOS, macOS, Windows and web !
I'm currently creating a TOTP app in which secrets are encrypted with a master password, and I need a secure place to store the encryption/decryption key.
That's why I decided to create my own solution : Simple Secure Storage (SSS).
- Very very simple and straightforward.
- Supports Android, iOS, macOS, Windows, Linux, and the web.
- Supports caching data for faster access.
- Secure.
- Again, very simple.
Simple Secure Storage uses :
- Keychain on Apple devices, which has been supported since iOS 2.0 and macOS 10.6.
EncryptedSharedPreferenceson Android, which requires a minimum SDK version of 21.wincred.hon Windows, which seems to be available since Windows XP.- An
org.freedesktop.secretsservice implementation on Linux. A secret service must be available (gnome_keyringis commonly installed). sembast_webandcipherlibon the web.
Run the following command to add Simple Secure Storage to your Flutter project :
flutter pub add simple_secure_storageThen follow the instructions for each platform you target.
As stated on Android Developers,
Warning
The preference file should not be backed up with Auto Backup.
When restoring the file it is likely the key used to encrypt it will no longer be present.
You should exclude all EncryptedSharedPreferences from backup using backup rules.
Either disable Auto Backup entirely in your AndroidManifest.xml :
<application
<!-- Some settings -->
android:allowBackup="false"
android:fullBackupContent="false">or exclude only the encrypted shared preferences using
backup rules.
The sharedpref path is the namespace value passed to the plugin's initialize method.
In Xcode, go to Project settings > Capabilities and enable Keychain Sharing (see this).
These requirements are typically already satisfied by default on most desktop environments.
- D-Bus support.
- A running Secret Service implementation (such as GNOME Keyring, KDE Wallet, or another implementation providing the
org.freedesktop.secretsD-Bus service).
Tip
Starting from 0.3.0, installing GNOME libsecret is no longer required. The Linux implementation uses the Secret Service D-Bus API directly.
Here's a simple example to get started.
// Initialize the plugin before using it, typically in your `main()` method.
// You can provide a namespace and application name through `InitializationOptions`.
if (kIsWeb) {
// To secure your data on Flutter web, we have to encrypt it using a password and a salt.
await SimpleSecureStorage.initialize(WebInitializationOptions(keyPassword: 'password', encryptionSalt: 'salt'));
} else {
await SimpleSecureStorage.initialize(); // You can also pass `InitializationOptions` here.
}
// Write a value.
await SimpleSecureStorage.write('key', 'value');
// Check if a value exists.
print(await SimpleSecureStorage.has('key')); // Will print "true".
// Read a value.
print(await SimpleSecureStorage.read('key')); // Will print "value".
// Remove a value.
await SimpleSecureStorage.delete('key');
// Clear everything.
await SimpleSecureStorage.clear();In the previous snippet, everything is being read "on demand". You may want to access your data
without always having to write await. To do so, use the class CachedSimpleSecureStorage.
It behaves exactly like the snippet above, except you call await CachedSimpleSecureStorage.getInstance()
instead of await SimpleSecureStorage.initialize().
If you want a more complete example, feel free to check and run this one, which is available on GitHub. To run the example app on Apple platforms (iOS and macOS), configure the development team in the project settings.
There are several ways to contribute to this project :