Skip to content

Commit 1288f56

Browse files
ZahraErfaniZahraErfani
ZahraErfani
authored and
ZahraErfani
committed
encryption key
1 parent 9755611 commit 1288f56

9 files changed

+94
-5
lines changed

lib/data/hive/hive_helpers.dart

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import 'dart:convert';
12
import 'dart:developer';
23
import 'dart:io';
34

45
import 'package:flutter/foundation.dart';
6+
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
57
import 'package:hive_flutter/adapters.dart';
68
import 'package:path/path.dart';
79
import 'package:path_provider/path_provider.dart';
@@ -28,15 +30,39 @@ class HiveHelper {
2830
}
2931

3032
await Hive.initFlutter();
33+
const secureStorage = FlutterSecureStorage();
34+
final encryprionKey = await secureStorage.read(key: 'key');
35+
if (encryprionKey == null) {
36+
final key = Hive.generateSecureKey();
37+
await secureStorage.write(
38+
key: 'key',
39+
value: base64UrlEncode(key),
40+
);
41+
}
42+
43+
final key = await secureStorage.read(key: 'key');
44+
final encryptionKey = base64Url.decode(key!);
3145

3246
Hive.registerAdapter(SubTaskAdapter());
3347
Hive.registerAdapter(TaskAdapter());
3448
Hive.registerAdapter(SubCategoryAdapter());
35-
await Hive.openBox("subtask");
36-
await Hive.openBox("task");
37-
await Hive.openBox("subCategory");
49+
await Hive.openBox(
50+
"subtask",
51+
encryptionCipher: HiveAesCipher(encryptionKey),
52+
);
53+
await Hive.openBox(
54+
"task",
55+
encryptionCipher: HiveAesCipher(encryptionKey),
56+
);
57+
await Hive.openBox(
58+
"subCategory",
59+
encryptionCipher: HiveAesCipher(encryptionKey),
60+
);
3861
Hive.registerAdapter(CategoryTaskAdapter());
39-
await Hive.openBox("category");
62+
await Hive.openBox(
63+
"category",
64+
encryptionCipher: HiveAesCipher(encryptionKey),
65+
);
4066
}
4167

4268
static void close(String name) async {

lib/screen/home/home.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Home extends StatefulWidget {
1717
}
1818

1919
class _HomeState extends State<Home> {
20+
final encryptedTaskBox = Hive.box(HiveBoxNames.task);
21+
2022
@override
2123
Widget build(BuildContext context) {
2224
return Scaffold(
@@ -26,7 +28,8 @@ class _HomeState extends State<Home> {
2628
),
2729
drawer: const MyCustomDrawer(),
2830
body: ValueListenableBuilder(
29-
valueListenable: Hive.box(HiveBoxNames.task).listenable(),
31+
// valueListenable: Hive.box(HiveBoxNames.task).listenable(),
32+
valueListenable: encryptedTaskBox.listenable(),
3033
builder: (context, Box box, widget) {
3134
if (box.isEmpty) {
3235
return const Center(

linux/flutter/generated_plugin_registrant.cc

+4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
#include "generated_plugin_registrant.h"
88

99
#include <audioplayers_linux/audioplayers_linux_plugin.h>
10+
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
1011
#include <record_linux/record_linux_plugin.h>
1112

1213
void fl_register_plugins(FlPluginRegistry* registry) {
1314
g_autoptr(FlPluginRegistrar) audioplayers_linux_registrar =
1415
fl_plugin_registry_get_registrar_for_plugin(registry, "AudioplayersLinuxPlugin");
1516
audioplayers_linux_plugin_register_with_registrar(audioplayers_linux_registrar);
17+
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
18+
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
19+
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
1620
g_autoptr(FlPluginRegistrar) record_linux_registrar =
1721
fl_plugin_registry_get_registrar_for_plugin(registry, "RecordLinuxPlugin");
1822
record_linux_plugin_register_with_registrar(record_linux_registrar);

linux/flutter/generated_plugins.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
list(APPEND FLUTTER_PLUGIN_LIST
66
audioplayers_linux
7+
flutter_secure_storage_linux
78
record_linux
89
)
910

macos/Flutter/GeneratedPluginRegistrant.swift

+2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import FlutterMacOS
66
import Foundation
77

88
import audioplayers_darwin
9+
import flutter_secure_storage_macos
910
import path_provider_foundation
1011
import record_macos
1112
import sqflite
1213

1314
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
1415
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
16+
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
1517
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
1618
RecordMacosPlugin.register(with: registry.registrar(forPlugin: "RecordMacosPlugin"))
1719
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))

pubspec.lock

+48
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,54 @@ packages:
315315
url: "https://pub.flutter-io.cn"
316316
source: hosted
317317
version: "2.0.9"
318+
flutter_secure_storage:
319+
dependency: "direct main"
320+
description:
321+
name: flutter_secure_storage
322+
sha256: "98352186ee7ad3639ccc77ad7924b773ff6883076ab952437d20f18a61f0a7c5"
323+
url: "https://pub.flutter-io.cn"
324+
source: hosted
325+
version: "8.0.0"
326+
flutter_secure_storage_linux:
327+
dependency: transitive
328+
description:
329+
name: flutter_secure_storage_linux
330+
sha256: "0912ae29a572230ad52d8a4697e5518d7f0f429052fd51df7e5a7952c7efe2a3"
331+
url: "https://pub.flutter-io.cn"
332+
source: hosted
333+
version: "1.1.3"
334+
flutter_secure_storage_macos:
335+
dependency: transitive
336+
description:
337+
name: flutter_secure_storage_macos
338+
sha256: "083add01847fc1c80a07a08e1ed6927e9acd9618a35e330239d4422cd2a58c50"
339+
url: "https://pub.flutter-io.cn"
340+
source: hosted
341+
version: "3.0.0"
342+
flutter_secure_storage_platform_interface:
343+
dependency: transitive
344+
description:
345+
name: flutter_secure_storage_platform_interface
346+
sha256: b3773190e385a3c8a382007893d678ae95462b3c2279e987b55d140d3b0cb81b
347+
url: "https://pub.flutter-io.cn"
348+
source: hosted
349+
version: "1.0.1"
350+
flutter_secure_storage_web:
351+
dependency: transitive
352+
description:
353+
name: flutter_secure_storage_web
354+
sha256: "42938e70d4b872e856e678c423cc0e9065d7d294f45bc41fc1981a4eb4beaffe"
355+
url: "https://pub.flutter-io.cn"
356+
source: hosted
357+
version: "1.1.1"
358+
flutter_secure_storage_windows:
359+
dependency: transitive
360+
description:
361+
name: flutter_secure_storage_windows
362+
sha256: fc2910ec9b28d60598216c29ea763b3a96c401f0ce1d13cdf69ccb0e5c93c3ee
363+
url: "https://pub.flutter-io.cn"
364+
source: hosted
365+
version: "2.0.0"
318366
flutter_test:
319367
dependency: "direct dev"
320368
description: flutter

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies:
5656
file_picker: ^5.2.6
5757
syncfusion_flutter_datepicker: ^20.4.53
5858
intl: 0.17.0
59+
flutter_secure_storage: ^8.0.0
5960

6061
dev_dependencies:
6162
flutter_test:

windows/flutter/generated_plugin_registrant.cc

+3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
#include "generated_plugin_registrant.h"
88

99
#include <audioplayers_windows/audioplayers_windows_plugin.h>
10+
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
1011
#include <permission_handler_windows/permission_handler_windows_plugin.h>
1112
#include <record_windows/record_windows_plugin_c_api.h>
1213

1314
void RegisterPlugins(flutter::PluginRegistry* registry) {
1415
AudioplayersWindowsPluginRegisterWithRegistrar(
1516
registry->GetRegistrarForPlugin("AudioplayersWindowsPlugin"));
17+
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
18+
registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin"));
1619
PermissionHandlerWindowsPluginRegisterWithRegistrar(
1720
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
1821
RecordWindowsPluginCApiRegisterWithRegistrar(

windows/flutter/generated_plugins.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
list(APPEND FLUTTER_PLUGIN_LIST
66
audioplayers_windows
7+
flutter_secure_storage_windows
78
permission_handler_windows
89
record_windows
910
)

0 commit comments

Comments
 (0)