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

发布于:2024-08-20 ⋅ 阅读:(192) ⋅ 点赞:(0)

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

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

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))

    // 取不存在的key
    console.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 {
  /// 存 String
  public static saveString(key: string, value: string) {
    key = JhEncryptUtils.aesEncrypt(key)
    value = JhEncryptUtils.aesEncrypt(value)
    PreferencesUtil.putSync(key, value)
  }

  /// 取 String
  public static getString(key: string): string | null {
    key = JhEncryptUtils.aesEncrypt(key)
    const enValue = PreferencesUtil.getStringSync(key)
    return JhEncryptUtils.aesDecrypt(enValue)
  }

  /// 存 boolean
  public static saveBool(key: string, value: boolean) {
    const newValue = value == true ? 'TRUE' : 'FALSE'
    JhAESPreferencesUtils.saveString(key, newValue)
  }

  /// 取 boolean
  public static getBool(key: string): boolean {
    const value = JhAESPreferencesUtils.getString(key)
    return value == 'TRUE' ? true : false
  }

  /// 存 number
  public static saveNumber(key: string, value: number) {
    const newValue = value.toString()
    JhAESPreferencesUtils.saveString(key, newValue)
  }

  /// 取 number
  public static getNumber(key: string): number {
    let value = JhAESPreferencesUtils.getString(key)
    value = (value == '' || value == null) ? '0' : value
    return Number(value)
  }

  /// 存 Model
  public static saveModel(key: string, value: Object) {
    let jsonString: string = JSON.stringify(value)
    JhAESPreferencesUtils.saveString(key, jsonString)
  }

  /// 取 Model
  public 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 {
  /// 存 String
  public static saveString(key: string, value: string) {
    PreferencesUtil.putSync(key, value)
  }

  /// 取 String
  public static getString(key: string): string | null {
    return PreferencesUtil.getStringSync(key)
  }

  /// 存 boolean
  public static saveBool(key: string, value: boolean) {
    const newValue = value == true ? 'TRUE' : 'FALSE'
    JhAESPreferencesUtils.saveString(key, newValue)
  }

  /// 取 boolean
  public static getBool(key: string): boolean {
    const value = JhAESPreferencesUtils.getString(key)
    return value == 'TRUE' ? true : false
  }

  /// 存 number
  public static saveNumber(key: string, value: number) {
    const newValue = value.toString()
    JhAESPreferencesUtils.saveString(key, newValue)
  }

  /// 取 number
  public static getNumber(key: string): number {
    let value = JhAESPreferencesUtils.getString(key)
    value = (value == '' || value == null) ? '0' : value
    return Number(value)
  }

  /// 存 Model
  public static saveModel(key: string, value: Object) {
    let jsonString: string = JSON.stringify(value)
    JhAESPreferencesUtils.saveString(key, jsonString)
  }

  /// 取 Model
  public 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)
  }
}

网站公告

今日签到

点亮在社区的每一天
去签到