是否有一种简单的方法可以将加密字段功能添加到 Kotlin 的 JetBrains Exposed 数据库框架?
从 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))
}
添加依赖:
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)
}