FLUTTER ECOSYSTEM

gslender/get_secure_storage

get_storage 的安全版本,这是一个用 Dart 完全编写的快速、轻量且同步的键值存储

get_secure_storage 项目封面
Stars
4
Forks
5
最近推送(UTC)
2024年4月10日
项目状态
未归档
GSlender GitHub avatar
GITHUB User

GSlender ↗

语言DartRubySwiftHTMLKotlinObjective-C

此仓库发布的插件

使用的依赖

依赖清单 6 项

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

展开 / 收起项目 README

get_secure_storage

A cryptography Secure version of GetStorage originally written by Jonny Borges (https://github.com/jonataslaw/get_storage).

GetSecureStorage is a secure, fast, extra light and synchronous key-value in memory, which backs up data to disk at each operation. It is written entirely in Dart and is based on the Cryptography dart package.

The cryptography library used is https://pub.dev/packages/cryptography

The algorithm used is 128bit AES-CTR with MAC sha256

Supports Android, iOS, Web, Mac, Linux, and Windows. Can store String, int, double, Map and List

Add to your pubspec:
dependencies:
  get_secure_storage:
Install it

You can install packages from the command line:

with Flutter:

$  flutter packages get
Import it

Now in your Dart code, you can use:

import 'package:get_secure_storage/get_secure_storage.dart';
Initialize storage driver with await:
main() async {
  await GetSecureStorage.init(password: 'strongpassword');
  runApp(App());
}
use GetSecureStorage through an instance or use directly GetSecureStorage().read('key')
final box = GetSecureStorage(password: 'strongpassword');
To write information you must use write :
box.write('quote', 'GetSecureStorage is the best');
To read values you use read:
print(box.read('quote'));
// out: GetSecureStorage is the best

To remove a key, you can use remove:
box.remove('quote');
To listen changes you can use listen:
Function? disposeListen;
disposeListen = box.listen((){
  print('box changed');
});
If you subscribe to events, be sure to dispose them when using:
disposeListen?.call();
To listen changes on key you can use listenKey:
box.listenKey('key', (value){
  print('new key is $value');
});
To erase your container:
box.erase();
If you want to create different containers, simply give it a name. You can listen to specific containers, and also delete them.
GetSecureStorage g = GetSecureStorage(container:'MyStorage', password: 'strongpassword');
To initialize specific container:
await GetSecureStorage.init(container:'MyStorage', password: 'strongpassword');

SharedPreferences Implementation

class MyPref {
  static final _otherBox = () => GetSecureStorage(container:'MyPref', password: 'strongpassword');

  final username = ''.val('username');
  final age = 0.val('age');
  final price = 1000.val('price', getBox: _otherBox);

  // or
  final username2 = ReadWriteValue('username', '');
  final age2 = ReadWriteValue('age', 0);
  final price2 = ReadWriteValue('price', '', _otherBox);
}

...

void updateAge() {
  final age = 0.val('age');
  // or 
  final age = ReadWriteValue('age', 0, () => box);
  // or 
  final age = Get.find<MyPref>().age;

  age.val = 1; // will save to box
  final realAge = age.val; // will read from box
}