Flutter安全存储插件keystore_strongbox的使用
Flutter安全存储插件keystore_strongbox的使用
获取开始
这个项目是一个用于 Flutter 的插件包的起点,它包含专门的平台特定实现代码,适用于 Android 和/或 iOS。
对于 Flutter 开发的帮助,可以查看官方文档,里面有教程、示例、移动开发指南和完整的 API 参考。
完整示例代码
import 'dart:developer';
import 'package:biometricx/biometricx.dart';
import 'package:flutter/material.dart';
import 'package:keystore_strongbox/keystore_strongbox.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _keystoreStrongboxPlugin = KeystoreStrongbox();
[@override](/user/override)
void initState() {
super.initState();
}
/// 迁移初始化向量(IV)从旧密钥库到新密钥库。
void migrate() async {
/// 尝试在新密钥库中找到当前用户的IV
/// 如果为null,则从旧密钥库中检索IV并迁移至新密钥库
var newIV = await _keystoreStrongboxPlugin.getIV(
key: "indonesia",
);
log(newIV.toString());
if (newIV != null) return;
var oldIV = await _keystoreStrongboxPlugin.getIV(
key: "indonesia",
sharedPreferences: 'com.salkuadrat.biometricx',
);
if (oldIV == null) return;
await _keystoreStrongboxPlugin.setIV(
key: 'indonesia',
value: oldIV,
);
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: [
ElevatedButton(
onPressed: () async {
var res = await BiometricX.encrypt(
userAuthenticationRequired: false,
storeSharedPreferences: false,
tag: 'indonesia',
returnCipher: true,
messageKey: 'bali',
message: 'bali',
);
log('x ${res.data}');
},
child: const Text('加密OLD'),
),
ElevatedButton(
onPressed: () async {
var s = await _keystoreStrongboxPlugin.encrypt(
tag: 'indonesia',
message: 'bali',
);
log('s $s');
},
child: const Text('加密NEW'),
),
ElevatedButton(
onPressed: () async {
var s = await _keystoreStrongboxPlugin.decrypt(
tag: 'indonesia',
message: '/u6YB5EOyrwsYmAqLBh/AcHzNd4=',
);
log('s $s');
},
child: const Text('解密'),
),
ElevatedButton(
onPressed: () async {
migrate();
},
child: const Text('迁移'),
),
],
),
),
),
);
}
}
更多关于Flutter安全存储插件keystore_strongbox的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter安全存储插件keystore_strongbox的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中,keystore_strongbox
是一个用于安全存储敏感数据的插件,它利用 Android 的 Keystore 系统来保护加密密钥。Keystore 是一个安全的硬件模块,可以存储加密密钥,防止它们被提取或篡改。keystore_strongbox
插件特别适用于需要高安全性的应用场景。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 keystore_strongbox
插件的依赖:
dependencies:
flutter:
sdk: flutter
keystore_strongbox: ^0.0.1 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
使用插件
keystore_strongbox
插件提供了简单的 API 来存储和检索加密数据。以下是一个基本的使用示例:
import 'package:flutter/material.dart';
import 'package:keystore_strongbox/keystore_strongbox.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Keystore Strongbox Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
try {
await KeystoreStrongbox.storeData(
key: 'my_key',
value: 'my_sensitive_data',
);
print('Data stored successfully');
} catch (e) {
print('Failed to store data: $e');
}
},
child: Text('Store Data'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
try {
String? data = await KeystoreStrongbox.getData('my_key');
print('Retrieved data: $data');
} catch (e) {
print('Failed to retrieve data: $e');
}
},
child: Text('Retrieve Data'),
),
],
),
),
),
);
}
}