如何将加密的 Hive Box 调用到类中?

发布于 2025-01-15 03:28:46 字数 2260 浏览 1 评论 0原文

我需要加密一个配置单元框,其中将在 Api_Page_() 中调用框 'user_api' 以接收用户输入并存储在所述框内。但是,encryptedBox 未在类中定义。 Hive 文档显示加密代码将在 main() 函数内部完成,我已经完成了,但我不确定如何在 main()< /代码>。 非常感谢任何帮助或建议!

我的代码:

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';



Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // HIVE ENCRYPTION----------------------------------------
  const secureStorage = FlutterSecureStorage();

  final encryptionKey = await secureStorage.read(key: 'key');
  if (encryptionKey == null) {
    final key = Hive.generateSecureKey();
    await secureStorage.write(
      key: 'key',
      value: base64Encode(key),
    );
  }
  final key = await secureStorage.read(key: 'key');
  final encryptKey = base64Url.decode(key);
  print('Encryption key: $encryptKey');
  // HIVE ENCRYPTION----------------------------------------


  // HIVE INIT---------------------------------------------
  Directory directory = await getApplicationDocumentsDirectory();
  Hive.init(directory.path);
  await Hive.initFlutter();
  final encryptedBox = Hive.openBox<String>('user_api', encryptionCipher: HiveAesCipher(encryptKey));     // Initially Opens Box on App Start
  // HIVE INIT---------------------------------------------



  runApp(myApp());
}


class myApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    
    return const MaterialApp(
      debugShowCheckedModeBanner: false,    // Removes Debug Banner. [Delete before app release]
      title: 'App Title Placeholder',
      home: API_Page_()                   // Calls API_Page_ class from api_page.dart
    );
  }
}

class API_Page_ extends StatelessWidget {
  const API_Page_({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RaisedButton(onPressed: () { var eBox = encryptedBox<String>('user_api');
        },
      )
    );
  }
}

I am needing to encrypt a hive box, in which the box 'user_api' is to be called in Api_Page_() to receive user input to store inside said box. However, the encryptedBox is not defined within the class. The Hive Docs display the encryption code is to be done inside of the main() function, which I have done, but I am unsure of how to take the box outside of main().
Any help or advice is greatly appreciated!

My Code:

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';



Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // HIVE ENCRYPTION----------------------------------------
  const secureStorage = FlutterSecureStorage();

  final encryptionKey = await secureStorage.read(key: 'key');
  if (encryptionKey == null) {
    final key = Hive.generateSecureKey();
    await secureStorage.write(
      key: 'key',
      value: base64Encode(key),
    );
  }
  final key = await secureStorage.read(key: 'key');
  final encryptKey = base64Url.decode(key);
  print('Encryption key: $encryptKey');
  // HIVE ENCRYPTION----------------------------------------


  // HIVE INIT---------------------------------------------
  Directory directory = await getApplicationDocumentsDirectory();
  Hive.init(directory.path);
  await Hive.initFlutter();
  final encryptedBox = Hive.openBox<String>('user_api', encryptionCipher: HiveAesCipher(encryptKey));     // Initially Opens Box on App Start
  // HIVE INIT---------------------------------------------



  runApp(myApp());
}


class myApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    
    return const MaterialApp(
      debugShowCheckedModeBanner: false,    // Removes Debug Banner. [Delete before app release]
      title: 'App Title Placeholder',
      home: API_Page_()                   // Calls API_Page_ class from api_page.dart
    );
  }
}

class API_Page_ extends StatelessWidget {
  const API_Page_({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RaisedButton(onPressed: () { var eBox = encryptedBox<String>('user_api');
        },
      )
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文