new: secure store

This commit is contained in:
lollipopkit
2023-11-03 00:23:57 -06:00
parent e80d115e4f
commit a9f9a1650e
13 changed files with 209 additions and 105 deletions

View File

@@ -1,10 +1,39 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:toolbox/data/res/path.dart';
class SecureStore {
SecureStore._();
static const _secureStorage = FlutterSecureStorage();
static HiveAesCipher? _cipher;
static const _hiveKey = 'hive_key';
static Future<void> init() async {
final encryptionKeyString = await _secureStorage.read(key: _hiveKey);
if (encryptionKeyString == null) {
final key = Hive.generateSecureKey();
await _secureStorage.write(
key: _hiveKey,
value: base64UrlEncode(key),
);
}
final key = await _secureStorage.read(key: _hiveKey);
if (key == null) {
throw Exception('Failed to init SecureStore');
}
final encryptionKeyUint8List = base64Url.decode(key);
_cipher = HiveAesCipher(encryptionKeyUint8List);
}
}
class PersistentStore<E> {
late final Box<E> box;
@@ -12,7 +41,10 @@ class PersistentStore<E> {
PersistentStore(this.boxName);
Future<void> init() async => box = await Hive.openBox(boxName);
Future<void> init() async => box = await Hive.openBox(
boxName,
encryptionCipher: SecureStore._cipher,
);
/// Get all db filenames.
///