写点什么

Flutter OHOS flutter_keychain(字符串安全存储)

作者:flfljh
  • 2024-12-19
    湖南
  • 本文字数:1538 字

    阅读完需:约 5 分钟

flutter_keychain

一个支持通过 Keychain 和 Keystore 支持字符串安全存储的 Flutter 插件


如果您有其他类型想要存储,则需要序列化为 UTF-8 字符串。

使用

import 'package:flutter_keychain/flutter_keychain.dart';...
// Get valuevar value = await FlutterKeychain.get(key: "key");
// Put valueawait FlutterKeychain.put(key: "key", value: "value");
// Remove itemawait FlutterKeychain.remove(key: "key");
// Clear the secure storeawait FlutterKeychain.clear();
复制代码

鸿蒙 OS 代码

RSA 密钥封装

async encrypt(input: string): Promise<string{
const options: huks.HuksOptions = {
  properties: this.getAesEncryptProperties(),
  inData: StringToUint8Array(input)
}
try {
  let huksSessionHandle: huks.HuksSessionHandle = await huks.initSession(this.keyAlias, options)
  let handle: number = huksSessionHandle.handle
  let huksReturnResult: huks.HuksReturnResult = await huks.finishSession(handle, options)
  let cipherData: Uint8Array = huksReturnResult.outData as Uint8Array
  return Uint8ArrayToString(cipherData)
} catch (err) {
  Log.e(TAG, "Failed to encrypt, err =" + JSON.stringify(err))
}
return ''
  }

  async decrypt(input: string): Promise<string{
const options: huks.HuksOptions = {
  properties: this.getAesDecryptProperties(),
  inData: StringToUint8Array(input)
}
try {
  let huksSessionHandle: huks.HuksSessionHandle = await huks.initSession(this.keyAlias, options)
  let handle: number = huksSessionHandle.handle
  let huksReturnResult: huks.HuksReturnResult = await huks.finishSession(handle, options)
  let cipherData: Uint8Array = huksReturnResult.outData as Uint8Array
  return Uint8ArrayToString(cipherData)
} catch (err) {
  Log.e(TAG, "Failed to decrypt, err =" + JSON.stringify(err))
}
return ''
  }

存储

      put(result: MethodResult, key: string, value: string) {this.encryptor?.encrypt(value).then((value) => {  if (value) {const data = StringToUint8Array(value)this.preferences?.put(key, data)this.preferences?.flush()  }}).finally(() => {  result.success(null)})  }
复制代码

获取

      get(result: MethodResult, key: string) {if (this.preferences) {  this.preferences.get(key, null).then(async (encryptedValue) => {if (encryptedValue) {  try {const data = Uint8ArrayToString(encryptedValue as Uint8Array)let decryptValue = await this.encryptor?.decrypt(data)result.success(decryptValue)  } catch (err) {result.error(this.resultErrorName, err?.message, err)  }} else {  result.success(null)}  })} else {  result.success(null)}  }
复制代码

移除

   remove(result: MethodResult, key: string) {this.preferences?.delete(key).then(() => {  this.preferences?.flush()}).finally(() => {  result.success(null)})  }
复制代码

清空

   clear(result: MethodResult) {this.preferences?.clear().then(() => {  this.preferences?.flush()}).finally(() => {  result.success(null)})  }
复制代码

刷新

   flush() {try {  let promise = this.preferences?.flush();  promise?.then(() => {Log.i(TAG, "Succeeded in flushing.");  }).catch((err: ESObject) => {Log.w(TAG, "Failed to flush." + JSON.stringify(err));  })} catch (err) {  Log.w(TAG, "Failed to flush." + JSON.stringify(err));}  }
复制代码


用户头像

flfljh

关注

还未添加个人签名 2024-10-29 加入

还未添加个人简介

评论

发布
暂无评论
Flutter OHOS flutter_keychain(字符串安全存储)_flfljh_InfoQ写作社区