diff src/main/kotlin/name/blackcap/passman/Database.kt @ 8:698c4a3d758d

Some code clean-up.
author David Barts <n5jrn@me.com>
date Fri, 23 Sep 2022 20:59:52 -0700
parents f245b9a53495
children c69665ff37d0
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/Database.kt	Tue Sep 20 21:54:32 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Database.kt	Fri Sep 23 20:59:52 2022 -0700
@@ -114,14 +114,24 @@
     fun makeKey(name: String): Long = Hashing.hash(encryption.encryptFromString0(name.lowercase()))
 }
 
-public fun ResultSet.getDecryptedString(columnIndex: Int, encryption: Encryption) =
-    encryption.decryptToString(getBytes(columnIndex))
+public fun ResultSet.getDecryptedString(columnIndex: Int, encryption: Encryption): String? {
+    return encryption.decryptToString(getBytes(columnIndex) ?: return null)
+}
 
-public fun ResultSet.getDecrypted(columnIndex: Int, encryption: Encryption) =
-    encryption.decrypt(getBytes(columnIndex))
+public fun ResultSet.getDecrypted(columnIndex: Int, encryption: Encryption): CharArray? {
+    return encryption.decrypt(getBytes(columnIndex) ?: return null)
+}
 
-public fun PreparedStatement.setEncryptedString(columnIndex: Int, value: String, encryption: Encryption) =
-    setBytes(columnIndex, encryption.encryptFromString(value))
+public fun PreparedStatement.setEncryptedString(columnIndex: Int, value: String?, encryption: Encryption) =
+    if (value == null) {
+        setNull(columnIndex, Types.BLOB)
+    } else {
+        setBytes(columnIndex, encryption.encryptFromString(value))
+    }
 
-public fun PreparedStatement.setEncrypted(columnIndex: Int, value: CharArray, encryption: Encryption) =
-    setBytes(columnIndex, encryption.encrypt(value))
+public fun PreparedStatement.setEncrypted(columnIndex: Int, value: CharArray?, encryption: Encryption) =
+    if (value == null) {
+        setNull(columnIndex, Types.BLOB)
+    } else {
+        setBytes(columnIndex, encryption.encrypt(value))
+    }