Capture notification lifecycle events securely
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user