-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathColumnEncryptionConverter.kt
57 lines (48 loc) · 1.73 KB
/
ColumnEncryptionConverter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package app.cash.jooq
import org.jooq.Converter
/**
* Encrypts a given string to and encodes it into a byte array and back for the specified column.
*
* If there is no encryption key associated with the fully qualified column name,
* nothing will be encrypted.
*/
class ColumnEncryptionConverter(
tableName: String,
columnName: String
) : Converter<ByteArray, ByteArray> {
private val fullyQualifiedColumnName = "$tableName.$columnName"
/**
* This is the common AAD being used with AEAD encryption for columns.
* It's a map of string->string values, that's serialized to a byte array, and contains:
* 1. "table_name" -> the table name
* 2. "column_name" -> the column name
*
* The map is then sorted, and each key-value pair is mapped to a string like
* `"key=value"`, and all pairs are then concatenated with `|`.
* Finally, the entire string of "key1=value1|key2=value2|..." is serialized to a byte array.
*/
private val aad = mapOf(
"table_name" to tableName,
"column_name" to columnName
).asSequence()
.sortedBy { (k, v) -> k + v }
.map { (k, v) -> "$k=$v" }
.joinToString("|")
.toByteArray(Charsets.UTF_8)
private val encryptionPrimitive: JooqKeyPrimitive by lazy {
RealJooqKeyPrimitive.getInstance()
}
override fun from(source: ByteArray): ByteArray {
return encryptionPrimitive.decrypt(fullyQualifiedColumnName, source, aad)
}
override fun to(source: ByteArray): ByteArray {
val encrypted = encryptionPrimitive.encrypt(fullyQualifiedColumnName, source, aad)
return if (!encrypted.contentEquals(source)) {
encrypted
} else {
return source
}
}
override fun fromType() = ByteArray::class.java
override fun toType() = ByteArray::class.java
}