From 460be8cbf38a7fe4554dd734b0129074a2285e66 Mon Sep 17 00:00:00 2001 From: KotlinGeekDev Date: Tue, 17 Feb 2026 01:03:19 +0100 Subject: [PATCH] Foundations(iOS Sourceset): Provide implementation for AESCBC. --- .../quartz/utils/ciphers/AESCBC.ios.kt | 49 ++++++++++++++----- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/quartz/src/iosMain/kotlin/com/vitorpamplona/quartz/utils/ciphers/AESCBC.ios.kt b/quartz/src/iosMain/kotlin/com/vitorpamplona/quartz/utils/ciphers/AESCBC.ios.kt index 9eb3aedce..4be429a12 100644 --- a/quartz/src/iosMain/kotlin/com/vitorpamplona/quartz/utils/ciphers/AESCBC.ios.kt +++ b/quartz/src/iosMain/kotlin/com/vitorpamplona/quartz/utils/ciphers/AESCBC.ios.kt @@ -20,23 +20,48 @@ */ package com.vitorpamplona.quartz.utils.ciphers +import com.vitorpamplona.quartz.utils.Log +import dev.whyoleg.cryptography.CryptographyProvider +import dev.whyoleg.cryptography.DelicateCryptographyApi +import dev.whyoleg.cryptography.algorithms.AES +import dev.whyoleg.cryptography.providers.apple.Apple + actual class AESCBC actual constructor( actual val keyBytes: ByteArray, actual val iv: ByteArray, ) : NostrCipher { - actual override fun name(): String { - TODO("Not yet implemented") - } + private val cryptoProvider = CryptographyProvider.Apple + private val aesCbc = cryptoProvider.get(AES.CBC) - actual override fun encrypt(bytesToEncrypt: ByteArray): ByteArray { - TODO("Not yet implemented") - } + private val keyDecoder = + aesCbc + .keyDecoder() + .decodeFromByteArrayBlocking(format = AES.Key.Format.RAW, keyBytes) - actual override fun decrypt(bytesToDecrypt: ByteArray): ByteArray { - TODO("Not yet implemented") - } + private fun cipher() = keyDecoder.cipher() - actual override fun decryptOrNull(bytesToDecrypt: ByteArray): ByteArray? { - TODO("Not yet implemented") - } + actual override fun name(): String = "aes-cbc" + + @OptIn(DelicateCryptographyApi::class) + actual override fun encrypt(bytesToEncrypt: ByteArray): ByteArray = + with(cipher()) { + encryptWithIvBlocking(iv, bytesToEncrypt) + } + + @OptIn(DelicateCryptographyApi::class) + actual override fun decrypt(bytesToDecrypt: ByteArray): ByteArray = + with(cipher()) { + decryptWithIvBlocking(iv, bytesToDecrypt) + } + + @OptIn(DelicateCryptographyApi::class) + actual override fun decryptOrNull(bytesToDecrypt: ByteArray): ByteArray? = + try { + with(cipher()) { + decryptWithIvBlocking(iv, bytesToDecrypt) + } + } catch (e: Exception) { + Log.w("AESCBC", "Failed to decrypt", e) + null + } }