diff --git a/module-extkt-crypto/src/commonMain/kotlin/compression/deflate.kt b/module-extkt-crypto/src/commonMain/kotlin/compression/deflate.kt index 1ac930f..416ad79 100644 --- a/module-extkt-crypto/src/commonMain/kotlin/compression/deflate.kt +++ b/module-extkt-crypto/src/commonMain/kotlin/compression/deflate.kt @@ -12,6 +12,10 @@ expect fun String.deflate(level: Int /* 0-9 */): ByteArray expect fun ByteArray.inflate(): ByteArray +expect fun ByteArray.inflateOrNull(): ByteArray? + // deflate => string expect fun ByteArray.inflateToString(): String + +expect fun ByteArray.inflateToStringOrNull(): String? diff --git a/module-extkt-crypto/src/commonMain/kotlin/compression/gzip.kt b/module-extkt-crypto/src/commonMain/kotlin/compression/gzip.kt index 067db44..9b99a91 100644 --- a/module-extkt-crypto/src/commonMain/kotlin/compression/gzip.kt +++ b/module-extkt-crypto/src/commonMain/kotlin/compression/gzip.kt @@ -12,6 +12,10 @@ expect fun String.gzip(): ByteArray expect fun ByteArray.gunzip(): ByteArray +expect fun ByteArray.gunzipOrNull(): ByteArray? + // gzip => string expect fun ByteArray.gunzipToString(): String + +expect fun ByteArray.gunzipToStringOrNull(): String? diff --git a/module-extkt-crypto/src/commonMain/kotlin/text/base64.kt b/module-extkt-crypto/src/commonMain/kotlin/text/base64.kt index ea0d84e..5eca824 100644 --- a/module-extkt-crypto/src/commonMain/kotlin/text/base64.kt +++ b/module-extkt-crypto/src/commonMain/kotlin/text/base64.kt @@ -28,14 +28,22 @@ expect fun String.encodeBase64UrlSafe(offset: Int, length: Int = this.length): S expect fun String.decodeBase64(): ByteArray +expect fun String.decodeBase64OrNull(): ByteArray? + // base64url => byte[] expect fun String.decodeBase64UrlSafe(): ByteArray +expect fun String.decodeBase64UrlSafeOrNull(): ByteArray? + // base64 => string expect fun String.decodeBase64ToString(): String +expect fun String.decodeBase64ToStringOrNull(): String? + // base64url => string expect fun String.decodeBase64UrlSafeToString(): String + +expect fun String.decodeBase64UrlSafeToStringOrNull(): String? diff --git a/module-extkt-crypto/src/commonMain/kotlin/text/hex.kt b/module-extkt-crypto/src/commonMain/kotlin/text/hex.kt index 4942c8b..3e8d226 100644 --- a/module-extkt-crypto/src/commonMain/kotlin/text/hex.kt +++ b/module-extkt-crypto/src/commonMain/kotlin/text/hex.kt @@ -12,6 +12,10 @@ expect fun String.encodeHex(): String expect fun String.decodeHex(): ByteArray +expect fun String.decodeHexOrNull(): ByteArray? + // hex => string expect fun String.decodeHexToString(): String + +expect fun String.decodeHexToStringOrNull(): String? diff --git a/module-extkt-crypto/src/jsMain/kotlin/compression/deflate.js.kt b/module-extkt-crypto/src/jsMain/kotlin/compression/deflate.js.kt index 542cc07..24bf7bf 100644 --- a/module-extkt-crypto/src/jsMain/kotlin/compression/deflate.js.kt +++ b/module-extkt-crypto/src/jsMain/kotlin/compression/deflate.js.kt @@ -12,6 +12,14 @@ actual fun ByteArray.inflate(): ByteArray { TODO("Not yet implemented") } +actual fun ByteArray.inflateOrNull(): ByteArray? { + TODO("Not yet implemented") +} + actual fun ByteArray.inflateToString(): String { TODO("Not yet implemented") } + +actual fun ByteArray.inflateToStringOrNull(): String? { + TODO("Not yet implemented") +} diff --git a/module-extkt-crypto/src/jsMain/kotlin/compression/gzip.js.kt b/module-extkt-crypto/src/jsMain/kotlin/compression/gzip.js.kt index 68098a9..2514b96 100644 --- a/module-extkt-crypto/src/jsMain/kotlin/compression/gzip.js.kt +++ b/module-extkt-crypto/src/jsMain/kotlin/compression/gzip.js.kt @@ -12,6 +12,14 @@ actual fun ByteArray.gunzip(): ByteArray { TODO("Not yet implemented") } +actual fun ByteArray.gunzipOrNull(): ByteArray? { + TODO("Not yet implemented") +} + actual fun ByteArray.gunzipToString(): String { TODO("Not yet implemented") } + +actual fun ByteArray.gunzipToStringOrNull(): String? { + TODO("Not yet implemented") +} diff --git a/module-extkt-crypto/src/jsMain/kotlin/text/base64.js.kt b/module-extkt-crypto/src/jsMain/kotlin/text/base64.js.kt index 9c68450..9ccad17 100644 --- a/module-extkt-crypto/src/jsMain/kotlin/text/base64.js.kt +++ b/module-extkt-crypto/src/jsMain/kotlin/text/base64.js.kt @@ -1,5 +1,6 @@ package org.cufy.text +import kotlin.coroutines.cancellation.CancellationException import kotlin.io.encoding.Base64 import kotlin.io.encoding.ExperimentalEncodingApi @@ -65,16 +66,55 @@ actual fun String.decodeBase64(): ByteArray { return Base64.decode(this) } +@OptIn(ExperimentalEncodingApi::class) +actual fun String.decodeBase64OrNull(): ByteArray? { + return try { + Base64.decode(this) + } catch (_: IllegalArgumentException) { + null + } +} + @OptIn(ExperimentalEncodingApi::class) actual fun String.decodeBase64UrlSafe(): ByteArray { val base64 = base64UrlSafeToBase64(this) return Base64.decode(base64) } +@OptIn(ExperimentalEncodingApi::class) +actual fun String.decodeBase64UrlSafeOrNull(): ByteArray? { + return try { + val base64 = base64UrlSafeToBase64(this) + Base64.decode(base64) + } catch (_: IllegalArgumentException) { + null + } +} + actual fun String.decodeBase64ToString(): String { return atob(this) } +actual fun String.decodeBase64ToStringOrNull(): String? { + return try { + atob(this) + } catch (e: CancellationException) { + throw e + } catch (_: Throwable) { + null + } +} + actual fun String.decodeBase64UrlSafeToString(): String { return atobUrlSafe(this) } + +actual fun String.decodeBase64UrlSafeToStringOrNull(): String? { + return try { + atobUrlSafe(this) + } catch (e: CancellationException) { + throw e + } catch (_: Throwable) { + null + } +} diff --git a/module-extkt-crypto/src/jsMain/kotlin/text/hex.js.kt b/module-extkt-crypto/src/jsMain/kotlin/text/hex.js.kt index 310da22..79e5536 100644 --- a/module-extkt-crypto/src/jsMain/kotlin/text/hex.js.kt +++ b/module-extkt-crypto/src/jsMain/kotlin/text/hex.js.kt @@ -12,6 +12,14 @@ actual fun String.decodeHex(): ByteArray { TODO("Not yet implemented") } +actual fun String.decodeHexOrNull(): ByteArray? { + TODO("Not yet implemented") +} + actual fun String.decodeHexToString(): String { TODO("Not yet implemented") } + +actual fun String.decodeHexToStringOrNull(): String? { + TODO("Not yet implemented") +} diff --git a/module-extkt-crypto/src/jvmMain/kotlin/compression/deflate.jvm.kt b/module-extkt-crypto/src/jvmMain/kotlin/compression/deflate.jvm.kt index 96d323d..bfcfdb1 100644 --- a/module-extkt-crypto/src/jvmMain/kotlin/compression/deflate.jvm.kt +++ b/module-extkt-crypto/src/jvmMain/kotlin/compression/deflate.jvm.kt @@ -8,6 +8,7 @@ import java.util.zip.Deflater import java.util.zip.DeflaterOutputStream import java.util.zip.Inflater import java.util.zip.InflaterInputStream +import kotlin.coroutines.cancellation.CancellationException actual fun ByteArray.deflate(level: Int): ByteArray { val baos = ByteArrayOutputStream() @@ -33,6 +34,16 @@ actual fun ByteArray.inflate(): ByteArray { return inflate.use { it.readBytes() } } +actual fun ByteArray.inflateOrNull(): ByteArray? { + return try { + inflate() + } catch (e: CancellationException) { + throw e + } catch (_: RuntimeException) { + null + } +} + actual fun ByteArray.inflateToString(): String { val bais = ByteArrayInputStream(this) val inflater = Inflater() @@ -40,3 +51,13 @@ actual fun ByteArray.inflateToString(): String { val isr = InputStreamReader(inflate) return isr.use { it.readText() } } + +actual fun ByteArray.inflateToStringOrNull(): String? { + return try { + inflateToString() + } catch (e: CancellationException) { + throw e + } catch (_: RuntimeException) { + null + } +} diff --git a/module-extkt-crypto/src/jvmMain/kotlin/compression/gzip.jvm.kt b/module-extkt-crypto/src/jvmMain/kotlin/compression/gzip.jvm.kt index 4397938..8379e90 100644 --- a/module-extkt-crypto/src/jvmMain/kotlin/compression/gzip.jvm.kt +++ b/module-extkt-crypto/src/jvmMain/kotlin/compression/gzip.jvm.kt @@ -6,6 +6,7 @@ import java.io.InputStreamReader import java.io.OutputStreamWriter import java.util.zip.GZIPInputStream import java.util.zip.GZIPOutputStream +import kotlin.coroutines.cancellation.CancellationException actual fun ByteArray.gzip(): ByteArray { val baos = ByteArrayOutputStream() @@ -28,9 +29,29 @@ actual fun ByteArray.gunzip(): ByteArray { return gzip.use { it.readBytes() } } +actual fun ByteArray.gunzipOrNull(): ByteArray? { + return try { + gunzip() + } catch (e: CancellationException) { + throw e + } catch (_: RuntimeException) { + null + } +} + actual fun ByteArray.gunzipToString(): String { val bais = ByteArrayInputStream(this) val gzip = GZIPInputStream(bais) val isr = InputStreamReader(gzip) return isr.use { it.readText() } } + +actual fun ByteArray.gunzipToStringOrNull(): String? { + return try { + gunzipToString() + } catch (e: CancellationException) { + throw e + } catch (_: RuntimeException) { + null + } +} diff --git a/module-extkt-crypto/src/jvmMain/kotlin/text/base64.jvm.kt b/module-extkt-crypto/src/jvmMain/kotlin/text/base64.jvm.kt index a204fff..acae2af 100644 --- a/module-extkt-crypto/src/jvmMain/kotlin/text/base64.jvm.kt +++ b/module-extkt-crypto/src/jvmMain/kotlin/text/base64.jvm.kt @@ -40,14 +40,38 @@ actual fun String.decodeBase64(): ByteArray { return Base64.decode(this) } +actual fun String.decodeBase64OrNull(): ByteArray? { + return try { + Base64.decode(this) + } catch (_: IllegalArgumentException) { + null + } +} + actual fun String.decodeBase64UrlSafe(): ByteArray { return Base64.urlSafeDecode(this) } +actual fun String.decodeBase64UrlSafeOrNull(): ByteArray? { + return try { + Base64.urlSafeDecode(this) + } catch (_: IllegalArgumentException) { + null + } +} + actual fun String.decodeBase64ToString(): String { return decodeBase64().decodeToString() } +actual fun String.decodeBase64ToStringOrNull(): String? { + return decodeBase64OrNull()?.decodeToString() +} + actual fun String.decodeBase64UrlSafeToString(): String { return decodeBase64UrlSafe().decodeToString() } + +actual fun String.decodeBase64UrlSafeToStringOrNull(): String? { + return decodeBase64UrlSafeOrNull()?.decodeToString() +} diff --git a/module-extkt-crypto/src/jvmMain/kotlin/text/hex.jvm.kt b/module-extkt-crypto/src/jvmMain/kotlin/text/hex.jvm.kt index 76e9672..6d45c4a 100644 --- a/module-extkt-crypto/src/jvmMain/kotlin/text/hex.jvm.kt +++ b/module-extkt-crypto/src/jvmMain/kotlin/text/hex.jvm.kt @@ -8,6 +8,14 @@ actual fun String.decodeHex(): ByteArray { return com.google.crypto.tink.subtle.Hex.decode(this) } +actual fun String.decodeHexOrNull(): ByteArray? { + return try { + com.google.crypto.tink.subtle.Hex.decode(this) + } catch (_: IllegalArgumentException) { + null + } +} + actual fun String.encodeHex(): String { return encodeToByteArray().encodeHex() } @@ -15,3 +23,7 @@ actual fun String.encodeHex(): String { actual fun String.decodeHexToString(): String { return decodeHex().decodeToString() } + +actual fun String.decodeHexToStringOrNull(): String? { + return decodeHexOrNull()?.decodeToString() +}