From 5c7d0f235d9db23491dee9247b50a9291bbeb172 Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Wed, 27 Nov 2024 21:19:49 +0800 Subject: [PATCH] :hammer: Base64 implementation using kotlin.io, better multiplatform equivalent implementation --- .../kotlin/com/crosspaste/utils/CodecsUtils.kt | 12 ++++++++++-- .../com/crosspaste/utils/CodecsUtils.desktop.kt | 9 --------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/crosspaste/utils/CodecsUtils.kt b/composeApp/src/commonMain/kotlin/com/crosspaste/utils/CodecsUtils.kt index 020a42a6..a5abb062 100644 --- a/composeApp/src/commonMain/kotlin/com/crosspaste/utils/CodecsUtils.kt +++ b/composeApp/src/commonMain/kotlin/com/crosspaste/utils/CodecsUtils.kt @@ -5,6 +5,8 @@ import io.ktor.utils.io.core.* import okio.Path import okio.buffer import okio.use +import kotlin.io.encoding.Base64 +import kotlin.io.encoding.ExperimentalEncodingApi expect fun getCodecsUtils(): CodecsUtils @@ -20,9 +22,15 @@ interface CodecsUtils { val sha256: Hasher - fun base64Encode(bytes: ByteArray): String + @OptIn(ExperimentalEncodingApi::class) + fun base64Encode(bytes: ByteArray): String { + return Base64.encode(bytes) + } - fun base64Decode(string: String): ByteArray + @OptIn(ExperimentalEncodingApi::class) + fun base64Decode(string: String): ByteArray { + return Base64.decode(string) + } fun hash(bytes: ByteArray): String { val (hash1, hash2) = CROSSPASTE_HASH.hash128x64(bytes) diff --git a/composeApp/src/desktopMain/kotlin/com/crosspaste/utils/CodecsUtils.desktop.kt b/composeApp/src/desktopMain/kotlin/com/crosspaste/utils/CodecsUtils.desktop.kt index e76e933c..4f08b2dc 100644 --- a/composeApp/src/desktopMain/kotlin/com/crosspaste/utils/CodecsUtils.desktop.kt +++ b/composeApp/src/desktopMain/kotlin/com/crosspaste/utils/CodecsUtils.desktop.kt @@ -3,7 +3,6 @@ package com.crosspaste.utils import dev.whyoleg.cryptography.CryptographyProvider import dev.whyoleg.cryptography.algorithms.SHA256 import java.io.ByteArrayOutputStream -import java.util.Base64 actual fun getCodecsUtils(): CodecsUtils { return DesktopCodecsUtils @@ -17,14 +16,6 @@ object DesktopCodecsUtils : CodecsUtils { override val sha256 = provider.get(SHA256).hasher() - override fun base64Encode(bytes: ByteArray): String { - return Base64.getEncoder().encodeToString(bytes) - } - - override fun base64Decode(string: String): ByteArray { - return Base64.getDecoder().decode(string) - } - override fun hashByArray(array: Array): String { if (array.isEmpty()) { throw IllegalArgumentException("Array is empty")