From f107db3312bc59b771282dae4d49dafa4b254b36 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Sun, 21 Jun 2020 22:06:09 +0200 Subject: [PATCH] Sketching further API --- .../ionspin/kotlin/crypto/CryptoProvider.kt | 30 +++++++++++++------ .../authenticated/XChaCha20Poly1305Test.kt | 2 +- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/CryptoProvider.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/CryptoProvider.kt index 712a138..ec2eccd 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/CryptoProvider.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/CryptoProvider.kt @@ -70,21 +70,29 @@ object Primitives : CryptoProvider { } -inline class EncryptableString(val content: String) : Encryptable { - override fun encryptableData(): UByteArray { +inline class EncryptableString(val content: String) : Encryptable { + override fun toEncryptableForm(): UByteArray { return content.encodeToUByteArray() } + override fun fromEncryptableForm(): (UByteArray) -> EncryptableString { + return { uByteArray -> + EncryptableString(uByteArray.toByteArray().decodeToString()) + } + } + fun asString() : String = content + } fun String.asEncryptableString() : EncryptableString { return EncryptableString(this) } -interface Encryptable { - fun encryptableData() : UByteArray +interface Encryptable { + fun toEncryptableForm() : UByteArray + fun fromEncryptableForm() : (UByteArray) -> T } data class HashedData(val hash: UByteArray) { @@ -101,7 +109,9 @@ data class SymmetricKey(val value : UByteArray) { } } -data class EncryptedData(val encrypted: UByteArray) +data class EncryptedData internal constructor(val ciphertext: UByteArray, val nonce: UByteArray) { + +} object PublicApi { @@ -115,16 +125,18 @@ object PublicApi { } } object Symmetric { - fun encrypt(key: SymmetricKey, data : Encryptable, additionalData : UByteArray = ubyteArrayOf()) : EncryptedData { + fun encrypt(key: SymmetricKey, data : Encryptable<*>, additionalData : UByteArray = ubyteArrayOf()) : EncryptedData { if (key.value.size != 32) { throw RuntimeException("Invalid key size! Required 32, supplied ${key.value.size}") } val nonce = SRNG.getRandomBytes(24) - return EncryptedData(XChaCha20Poly1305Pure.encrypt(key.value, nonce, data.encryptableData(), additionalData) + nonce) + return EncryptedData(XChaCha20Poly1305Pure.encrypt(key.value, nonce, data.toEncryptableForm(), additionalData), nonce) + } - fun decrypt(encryptedData : EncryptedData) : T { - TODO() + fun > decrypt(key: SymmetricKey, encryptedData : EncryptedData, additionalData: UByteArray, byteArrayDeserializer : (UByteArray) -> T) : T { + return byteArrayDeserializer(XChaCha20Poly1305Pure.decrypt(key.value, encryptedData.nonce, encryptedData.ciphertext, additionalData)) + } } } \ No newline at end of file diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt index edc6239..cda5e1b 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt @@ -90,7 +90,7 @@ class XChaCha20Poly1305Test { } - + @Test fun updateableXChaCha20Poly1305() { assertTrue {