使用 JetBrains Exposed 时加密字段

问题描述 投票:0回答:2

是否有一种简单的方法可以将加密字段功能添加到 Kotlin 的 JetBrains Exposed 数据库框架?

https://github.com/JetBrains/Exposed

encryption kotlin dao jetbrains-ide
2个回答
0
投票

从 VarCharColumnType 派生 SecureVarchar 列类型并在表中使用此类型。

class SecureVarCharColumnType(val l: Int = 255, c: String? = null) : VarCharColumnType(l, c) {

    override fun valueToDB(value: Any?): Any? = value?.let {
        return notNullValueToDB(Crypto.encrypt(it.toString()));
    }

    override fun valueFromDB(value: Any): Any {
        return Crypto.decrypt(value.toString())
    }
}

object Users : IntIdTable("user") {
    val email = super.registerColumn<String>("email", SecureVarCharColumnType(255))
 
}

0
投票

添加依赖:

implementation("org.jetbrains.exposed:exposed-crypt:$exposed_version")

带有加密列的表如下所示:

object ATable : Table() {
    val id = integer("id").autoIncrement()
    val columEncrypted = encryptedVarchar(
        name = "colum_encrypted",
        cipherTextLength = 1024,
        Algorithms.AES_256_PBE_GCM("password", "salt")
    ).nullable()
    
    override val primaryKey = PrimaryKey(id)
}
© www.soinside.com 2019 - 2024. All rights reserved.