Capture notification lifecycle events securely

This commit is contained in:
ajp_anton
2026-07-23 08:08:11 +00:00
parent 7da14dd10f
commit 6d77a43c55
6 changed files with 176 additions and 7 deletions
@@ -1,6 +1,5 @@
package se.ajpanton.notificationlog.data
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.spec.GCMParameterSpec
@@ -8,13 +7,11 @@ import javax.crypto.spec.GCMParameterSpec
/** Small, format-neutral AES-GCM codec used for every persisted log payload. */
class AesGcmCipher(
private val key: SecretKey,
private val secureRandom: SecureRandom = SecureRandom(),
) {
fun encrypt(plainText: ByteArray): EncryptedPayload {
val initializationVector = ByteArray(IV_LENGTH_BYTES).also(secureRandom::nextBytes)
val cipher = Cipher.getInstance(TRANSFORMATION)
cipher.init(Cipher.ENCRYPT_MODE, key, GCMParameterSpec(TAG_LENGTH_BITS, initializationVector))
return EncryptedPayload(initializationVector, cipher.doFinal(plainText))
cipher.init(Cipher.ENCRYPT_MODE, key)
return EncryptedPayload(cipher.iv, cipher.doFinal(plainText))
}
fun decrypt(payload: EncryptedPayload): ByteArray {