当前位置: 首页 > 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)}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【Spring Boot】全局异常处理
  • 第三章 LVS(DR模式)+keepalived群集【重要】
  • Java技术面试(一面)
  • 单调栈《数组模拟》
  • 极狐 GitLab 依赖扫描:助力开发者管理软件供应链
  • SQL数据抽样:精准洞察的高效策略
  • 开放平台: 签名密钥、回调地址、ip白名单管理。
  • excel导入
  • vue3中引入插件报ts报错Could not find a declaration file for module
  • 学懂C++(二十四):高级教程——C++ 多线程编程中 std::thread 的深入详解
  • java 面试 PDF 资料整理
  • 【vue】浏览器兼容相关
  • 关于近期安卓开发书籍阅读观后感
  • 【自动驾驶】ROS中参数服务器通信(c++)
  • R语言文本挖掘-万字详细解析tm包
  • 【前端学习】-粗谈选择器
  • angular2开源库收集
  • CentOS从零开始部署Nodejs项目
  • laravel5.5 视图共享数据
  • mysql 数据库四种事务隔离级别
  • React系列之 Redux 架构模式
  • 闭包--闭包作用之保存(一)
  • 第十八天-企业应用架构模式-基本模式
  • 基于web的全景—— Pannellum小试
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 如何实现 font-size 的响应式
  • 微信公众号开发小记——5.python微信红包
  • 小程序button引导用户授权
  • 验证码识别技术——15分钟带你突破各种复杂不定长验证码
  • 追踪解析 FutureTask 源码
  • 《码出高效》学习笔记与书中错误记录
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • ​Java基础复习笔记 第16章:网络编程
  • ​字​节​一​面​
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (175)FPGA门控时钟技术
  • (poj1.2.1)1970(筛选法模拟)
  • (二)丶RabbitMQ的六大核心
  • (二开)Flink 修改源码拓展 SQL 语法
  • (回溯) LeetCode 78. 子集
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • (新)网络工程师考点串讲与真题详解
  • (一)Java算法:二分查找
  • (转)http协议
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • (转)Windows2003安全设置/维护
  • (最优化理论与方法)第二章最优化所需基础知识-第三节:重要凸集举例
  • .form文件_SSM框架文件上传篇
  • .form文件_一篇文章学会文件上传
  • .NET 5种线程安全集合
  • .NET Core 发展历程和版本迭代
  • .net 逐行读取大文本文件_如何使用 Java 灵活读取 Excel 内容 ?
  • .NET/C# 将一个命令行参数字符串转换为命令行参数数组 args