2 Star 1 Fork 0

张小德 / MXKeyValue

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
张小德 提交于 2024-03-26 13:25 . Update README.md

MXKeyValue

介绍

基于Sqlite的,支持加密、自定义加密方式的KV数据库

库引用: 替换1.3.1 为最新版本

    implementation 'com.gitee.zhangmengxiong:MXKeyValue:1.3.1'

使用方法

val KV = MXKeyValue.Builder("kvdb_kv_v1")
             .setCrypt(KVAESCrypt("27e2125d0a11a9aa65b9c9773673bc2a"))
             .setStore(KVSqliteStore())
             .build(MyApp.appContext)

// 清理所有KV
KV.cleanAll()

// 获取所有KV
KV.getAll()

// 清理失效的KeyValue
KV.cleanExpire()

// 设置KV
KV.set(key, value)

// 设置KV的有效期 1分钟 后失效
KV.set(key, value, 1.toDuration(DurationUnit.MINUTES))

// 获取Value
KV.get("test_expire_key")

// 获取Value ,如果不存在则返回默认值
KV.get("test_expire_key", default = "默认值")

// 删除key
KV.delete("test_expire_key")

// 从SharedPreferences拷贝所有数据
KV.cloneFromSharedPreferences("sp_name")

加密相关

MXKeyValue内置两种加密方式:

  • KVNoCrypt()
  • KVAESCrypt("加密字符")

MXNoSecret

KVNoCrypt = 不加密,存储Value=设置的Value

MXAESSecret

KVAESCrypt = AES对称加密

  • 存储的value=encrypt(设置的Value)
  • 读取的Value=decrypt(存储的Value)

注意:KVAESCrypt初始化的加密字符在app上线后不能修改,否则会导致数据读取错误!

自定义加密方式

  • 需要实现IMXCrypt接口
  • generalSalt() = 生成当条记录的混淆字段
  • encrypt方法 = 源数据Value->存储Value
  • decrypt方法 = 存储Value->源数据Value
class MyCrypt : IKVCrypt {
    private val divider = "$$$$$$$$$$$$"
    override fun generalSalt(): String {
        return UUID.randomUUID().toString().replace("-", "")
    }

    override fun encrypt(key: String, value: String, salt: String): String? {
        return "$key$divider$value"
    }

    override fun decrypt(key: String, secretValue: String, salt: String): String? {
        return secretValue.split(divider).lastOrNull()
    }
}

使用方法:

val KV = MXKeyValue.Builder(MyApp.appContext, "kvdb_kv_v1")
             .setCrypt(MyCrypt())
             .setStore(KVSqliteStore())
             .build()
1
https://gitee.com/zhangmengxiong/MXKeyValue.git
git@gitee.com:zhangmengxiong/MXKeyValue.git
zhangmengxiong
MXKeyValue
MXKeyValue
master

搜索帮助