simple_secure_storage 0.4.3
simple_secure_storage: ^0.4.3 copied to clipboard
Simple and secure storage for Flutter. Supports Android, iOS, macOS, Windows and web !
Simple Secure Storage #
A simple and secure storage system for Flutter. Supports Android, iOS, macOS, Windows and web !
Motivations #
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).
Features #
- Very very simple and straightforward.
- Supports Android, iOS, macOS, Windows, Linux, and the web.
- Supports caching data for faster access.
- Secure.
- Again, very simple.
Getting started #
Minimum requirements #
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.
Installation #
Run the following command to add Simple Secure Storage to your Flutter project :
flutter pub add simple_secure_storage
Then follow the instructions for each platform you target.
Android
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.
macOS
In Xcode, go to Project settings > Capabilities and enable Keychain Sharing (see this).
Linux
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.
Note
If you need the previous libsecret-based backend, explicitly add
simple_secure_storage_linux_libsecret
to your application's dependencies. This optional, unmaintained compatibility fallback
preserves the implementation from before PR #15. Its README explains how to select
it, install the required native libraries, and keep access to existing data.
Code snippet #
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().
Repeated or concurrent calls to getInstance() share the same initialization and return
the same instance once its cache has loaded. Initialization options are only applied when
creating the instance. If initialization or cache loading fails, a later call can retry.
Use refreshCache() to explicitly reload values changed outside the cached instance.
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.
Contributions #
There are several ways to contribute to this project :