Skip to content

Commit

Permalink
feat(crypto): orNull variants
Browse files Browse the repository at this point in the history
  • Loading branch information
lsafer-meemer committed Dec 21, 2024
1 parent 153818d commit 7c900fd
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?
4 changes: 4 additions & 0 deletions module-extkt-crypto/src/commonMain/kotlin/compression/gzip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?
8 changes: 8 additions & 0 deletions module-extkt-crypto/src/commonMain/kotlin/text/base64.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?
4 changes: 4 additions & 0 deletions module-extkt-crypto/src/commonMain/kotlin/text/hex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
8 changes: 8 additions & 0 deletions module-extkt-crypto/src/jsMain/kotlin/compression/gzip.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
40 changes: 40 additions & 0 deletions module-extkt-crypto/src/jsMain/kotlin/text/base64.js.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cufy.text

import kotlin.coroutines.cancellation.CancellationException
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi

Expand Down Expand Up @@ -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
}
}
8 changes: 8 additions & 0 deletions module-extkt-crypto/src/jsMain/kotlin/text/hex.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
21 changes: 21 additions & 0 deletions module-extkt-crypto/src/jvmMain/kotlin/compression/deflate.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -33,10 +34,30 @@ 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()
val inflate = InflaterInputStream(bais, inflater)
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
}
}
21 changes: 21 additions & 0 deletions module-extkt-crypto/src/jvmMain/kotlin/compression/gzip.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
}
24 changes: 24 additions & 0 deletions module-extkt-crypto/src/jvmMain/kotlin/text/base64.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
12 changes: 12 additions & 0 deletions module-extkt-crypto/src/jvmMain/kotlin/text/hex.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ 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()
}

actual fun String.decodeHexToString(): String {
return decodeHex().decodeToString()
}

actual fun String.decodeHexToStringOrNull(): String? {
return decodeHexOrNull()?.decodeToString()
}

0 comments on commit 7c900fd

Please sign in to comment.