Foundations(iOS Sourceset): Provide implementation for AESCBC.
This commit is contained in:
@@ -20,23 +20,48 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.quartz.utils.ciphers
|
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 class AESCBC actual constructor(
|
||||||
actual val keyBytes: ByteArray,
|
actual val keyBytes: ByteArray,
|
||||||
actual val iv: ByteArray,
|
actual val iv: ByteArray,
|
||||||
) : NostrCipher {
|
) : NostrCipher {
|
||||||
actual override fun name(): String {
|
private val cryptoProvider = CryptographyProvider.Apple
|
||||||
TODO("Not yet implemented")
|
private val aesCbc = cryptoProvider.get(AES.CBC)
|
||||||
}
|
|
||||||
|
|
||||||
actual override fun encrypt(bytesToEncrypt: ByteArray): ByteArray {
|
private val keyDecoder =
|
||||||
TODO("Not yet implemented")
|
aesCbc
|
||||||
}
|
.keyDecoder()
|
||||||
|
.decodeFromByteArrayBlocking(format = AES.Key.Format.RAW, keyBytes)
|
||||||
|
|
||||||
actual override fun decrypt(bytesToDecrypt: ByteArray): ByteArray {
|
private fun cipher() = keyDecoder.cipher()
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
actual override fun decryptOrNull(bytesToDecrypt: ByteArray): ByteArray? {
|
actual override fun name(): String = "aes-cbc"
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
@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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user