flutter_secure_storage
a flutter plugin to store data in secure storage:
- keychain is used for ios
- aes encryption is used for android. aes secret key is encrypted with rsa and rsa key is stored in keystore
- with v5.0.0 we can use encryptedsharedpreferences on android by enabling it in the android options like so:
androidoptions _getandroidoptions() => const androidoptions(
encryptedsharedpreferences: true,
);
for more information see the example app.
libsecret
is used for linux.
note keystore was introduced in android 4.3 (api level 18). the plugin wouldn’t work for earlier versions.
platform implementation
please note that this table represents the functions implemented in this repository and it is possible that changes haven’t yet been released on pub.dev
read | write | delete | containskey | readall | deleteall | |
---|---|---|---|---|---|---|
android | ||||||
ios | ||||||
windows | ||||||
linux | ||||||
macos | ||||||
web |
getting started
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
// create storage
final storage = new fluttersecurestorage();
// read value
string value = await storage.read(key: key);
// read all values
map<string, string> allvalues = await storage.readall();
// delete value
await storage.delete(key: key);
// delete all
await storage.deleteall();
// write value
await storage.write(key: key, value: value);
this allows us to be able to fetch secure values while the app is backgrounded, by specifying first_unlock or first_unlock_this_device. the default if not specified is unlocked.
an example:
final options = iosoptions(accessibility: iosaccessibility.first_unlock);
await storage.write(key: key, value: value, ioptions: options);
configure android version
in [project]/android/app/build.gradle
set minsdkversion
to >= 18.
android {
...
defaultconfig {
...
minsdkversion 18
...
}
}
note by default android backups data on google drive. it can cause exception java.security.invalidkeyexception:failed to unwrap key.
you need to
- disable autobackup, details
- exclude sharedprefs
fluttersecurestorage
used by the plugin, details
configure web version
flutter secure storage uses an experimental implementation using webcrypto. use at your own risk at this time. feedback welcome to improve it. the intent is that the browser is creating the private key, and as a result, the encrypted strings in local_storage are not portable to other browsers or other machines and will only work on the same domain.
it is very important that you have http strict forward secrecy enabled and the proper headers applied to your responses or you could be subject to a javascript hijack.
please see:
- https://developer.mozilla.org/en-us/docs/web/http/headers/strict-transport-security
- https://www.netsparker.com/blog/web-security/http-security-headers/
configure linux version
you need libsecret-1-dev
and libjsoncpp-dev
on your machine to build the project, and libsecret-1-0
and libjsoncpp1
to run the application (add it as a dependency after packaging your app). if you using snapcraft to build the project use the following
parts:
uet-lms:
source: .
plugin: flutter
flutter-target: lib/main.dart
build-packages:
- libsecret-1-dev
- libjsoncpp-dev
stage-packages:
- libsecret-1-dev
- libjsoncpp1-dev
configure macos version
you also need to add keychain sharing as capability to your macos runner.
integration tests
run the following command from example
directory
flutter drive --target=test_driver/app.dart
Comments are closed.