当前位置: 首页 > news >正文

HarmonyOS NEXT - 数据持久化存储(key,value进行AES加密处理)

demo 地址: https://github.com/iotjin/JhHarmonyDemo
代码不定时更新,请前往github查看最新代码

鸿蒙的数据持久化是通过PersistentStorage, PersistentStorage官方文档

PersistentStorage是应用程序中的可选单例对象。此对象的作用是持久化存储选定的AppStorage属性,以确保这些属性在应用程序重新启动时的值与应用程序关闭时的值相同。

官方建议是通过PersistentStorageAppStorage联合使用

  • PersistentStorage将选定的AppStorage属性保留在设备磁盘上。应用程序通过API,以决定哪些AppStorage属性应借助PersistentStorage持久化。UI和业务逻辑不直接访问PersistentStorage中的属性,所有属性访问都是对AppStorage的访问,AppStorage中的更改会自动同步到PersistentStorage。
  • PersistentStorage和AppStorage中的属性建立双向同步。应用开发通常通过AppStorage访问PersistentStorage,另外还有一些接口可以用于管理持久化属性,但是业务逻辑始终是通过AppStorage获取和设置属性的。

demo这里使用的是三方库数进行据存储 @pura/harmony-utils,这是一个常用工具类的三方库,使用的里面的PreferencesUtil
存数据时先对key和value 进行加密处理,然后再存到本地,这样本地数据都是密文
aes加密工具类 JhEncryptUtils,这里使用了三方库@ohos/crypto-js

需要先安装两个三方库

ohpm  install @ohos/crypto-js 
ohpm i @pura/harmony-utils

OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包

我习惯把三方库封装一下再在项目调用,这样万一需要更换的话会方便点

调用

    console.log('-------------------本地加密存储-------------------')JhAESPreferencesUtils.saveString('testStr', '这是测试本地加密存储的字符串')const testStr = JhAESPreferencesUtils.getString('testStr')console.log(`testStr : ${testStr}`)JhAESPreferencesUtils.saveBool('testBool', true)const testBool = JhAESPreferencesUtils.getBool('testBool')console.log(`testBool : ${testBool}`)JhAESPreferencesUtils.saveNumber('testInt', 1111)const testInt = JhAESPreferencesUtils.getNumber('testInt')console.log(`testInt : ${testInt}`)JhAESPreferencesUtils.saveNumber('testDouble', 222.333354)const testDouble = JhAESPreferencesUtils.getNumber('testDouble')console.log(`testDouble : ${testDouble}`)const dic = { 'a': 'aaa', 'b': 'bbb', 'c': 'ccc' } as Record<string, string>console.log('原始dic', JSON.stringify(dic))JhAESPreferencesUtils.saveModel('testDic', dic)const testDic = JhAESPreferencesUtils.getModel('testDic')console.log('testDic', JSON.stringify(testDic))// 取不存在的keyconsole.log('-------------------取不存在的key----------------------')const testStr2 = JhAESPreferencesUtils.getString('testStr222')console.log(`testStr2原始 : ${testStr2}`)const test = testStr2 == '' ? '1' : '2222'console.log(test)console.log(`testStr2 : ${testStr2}`)const testBool2 = JhAESPreferencesUtils.getBool('testBool222')console.log(`testBool2 : ${testBool2}`)const testInt2 = JhAESPreferencesUtils.getNumber('testInt222')console.log(`testInt2: ${testInt2}`)const testDouble2 = JhAESPreferencesUtils.getNumber('testDouble222')console.log(`testDouble2 : ${testDouble2}`)const testDic2 = JhAESPreferencesUtils.getModel('testDic222')console.log(`testDic2 : ${testDic2}`)

打印结果

-------------------本地加密存储-------------------
testStr : 这是测试本地加密存储的字符串
testBool : true
testInt : 1111
testDouble : 222.333354
原始dic {"a":"aaa","b":"bbb","c":"ccc"}
testDic {"a":"aaa","b":"bbb","c":"ccc"}
-------------------取不存在的key----------------------
testStr2原始 :
1
testStr2 :
testBool2 : false
testInt2: 0
testDouble2 : 0
testDic2 : null

JhAESPreferencesUtils

JhEncryptUtils 看这里 HarmonyOS NEXT - 使用crypto-js实现Base64、MD5、AES加解密(CBC/PKCS7)

///  JhPreferencesUtils.ets
///
///  Created by iotjin on 2024/08/09. 
///  description:  AES 数据存储(封装第三方)import { PreferencesUtil } from '@pura/harmony-utils';
import { JhEncryptUtils } from './JhEncryptUtils';/// AES 加密存储
export class JhAESPreferencesUtils {/// 存 Stringpublic static saveString(key: string, value: string) {key = JhEncryptUtils.aesEncrypt(key)value = JhEncryptUtils.aesEncrypt(value)PreferencesUtil.putSync(key, value)}/// 取 Stringpublic static getString(key: string): string | null {key = JhEncryptUtils.aesEncrypt(key)const enValue = PreferencesUtil.getStringSync(key)return JhEncryptUtils.aesDecrypt(enValue)}/// 存 booleanpublic static saveBool(key: string, value: boolean) {const newValue = value == true ? 'TRUE' : 'FALSE'JhAESPreferencesUtils.saveString(key, newValue)}/// 取 booleanpublic static getBool(key: string): boolean {const value = JhAESPreferencesUtils.getString(key)return value == 'TRUE' ? true : false}/// 存 numberpublic static saveNumber(key: string, value: number) {const newValue = value.toString()JhAESPreferencesUtils.saveString(key, newValue)}/// 取 numberpublic static getNumber(key: string): number {let value = JhAESPreferencesUtils.getString(key)value = (value == '' || value == null) ? '0' : valuereturn Number(value)}/// 存 Modelpublic static saveModel(key: string, value: Object) {let jsonString: string = JSON.stringify(value)JhAESPreferencesUtils.saveString(key, jsonString)}/// 取 Modelpublic static getModel(key: string): Object | null {let jsonStr = JhAESPreferencesUtils.getString(key)return jsonStr ? JSON.parse(jsonStr) : null}/// 移除单个public static delete(key: string) {key = JhEncryptUtils.aesEncrypt(key)PreferencesUtil.deleteSync(key)}/// 移除所有public static clear() {PreferencesUtil.clear()}/// 根据key检查缓存值是否存在public static has(key: string): boolean {key = JhEncryptUtils.aesEncrypt(key)return PreferencesUtil.hasSync(key)}
}/// 数据存储(不使用AES加密)
export class JhPreferencesUtils {/// 存 Stringpublic static saveString(key: string, value: string) {PreferencesUtil.putSync(key, value)}/// 取 Stringpublic static getString(key: string): string | null {return PreferencesUtil.getStringSync(key)}/// 存 booleanpublic static saveBool(key: string, value: boolean) {const newValue = value == true ? 'TRUE' : 'FALSE'JhAESPreferencesUtils.saveString(key, newValue)}/// 取 booleanpublic static getBool(key: string): boolean {const value = JhAESPreferencesUtils.getString(key)return value == 'TRUE' ? true : false}/// 存 numberpublic static saveNumber(key: string, value: number) {const newValue = value.toString()JhAESPreferencesUtils.saveString(key, newValue)}/// 取 numberpublic static getNumber(key: string): number {let value = JhAESPreferencesUtils.getString(key)value = (value == '' || value == null) ? '0' : valuereturn Number(value)}/// 存 Modelpublic static saveModel(key: string, value: Object) {let jsonString: string = JSON.stringify(value)JhAESPreferencesUtils.saveString(key, jsonString)}/// 取 Modelpublic static getModel(key: string): Object | null {let jsonStr = JhAESPreferencesUtils.getString(key)return jsonStr ? JSON.parse(jsonStr) : null}/// 移除单个public static remove(key: string) {PreferencesUtil.deleteSync(key)}/// 移除所有public static clear() {PreferencesUtil.clear()}/// 根据key检查缓存值是否存在public static has(key: string): boolean {return PreferencesUtil.hasSync(key)}
}

http://www.mrgr.cn/news/3488.html

相关文章:

  • Linux内核实践(一)驱动DS18B20传感器的完整流程解析(涵盖字符设备、单总线、设备树等)
  • 国外代理IP选择:IP池的大小有何影响
  • 面试高频-深拷贝和浅拷贝
  • C/C++控制台贪吃蛇游戏的实现
  • esbuild中的Base64 Loader:轻松将文件编码为Base64字符串并嵌入代码
  • linux(arm)移植 macchanger
  • 有了这4款工具,你就知道电脑怎么录屏了!
  • CTRL-C论文解析
  • JS(三)——更改html内数据
  • MySQL 重复数据操作
  • 微服务多个模块启动,端口被占用,yml配置文件读不到
  • 【48 Pandas+Pyecharts | 2024年巴黎奥运会奖牌数据分析可视化】
  • 废品回收小程序,开启上门回收模式
  • 【ARM Hypervisor And SMMU 系列 5 -- SMMU 和 IOMMU技术】
  • 计算机基础之Cache的缓存命中率不随其容量线性增加的原理
  • nginx主配置文件说明
  • win10 / win11 永久暂停自动更新方法
  • LLM小模型系列研究(01)
  • STM32- 笔记2
  • CSS:display和visiblity