Skip to content

Commit 7c900fd

Browse files
committed
feat(crypto): orNull variants
1 parent 153818d commit 7c900fd

12 files changed

Lines changed: 162 additions & 0 deletions

File tree

module-extkt-crypto/src/commonMain/kotlin/compression/deflate.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ expect fun String.deflate(level: Int /* 0-9 */): ByteArray
1212

1313
expect fun ByteArray.inflate(): ByteArray
1414

15+
expect fun ByteArray.inflateOrNull(): ByteArray?
16+
1517
// deflate => string
1618

1719
expect fun ByteArray.inflateToString(): String
20+
21+
expect fun ByteArray.inflateToStringOrNull(): String?

module-extkt-crypto/src/commonMain/kotlin/compression/gzip.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ expect fun String.gzip(): ByteArray
1212

1313
expect fun ByteArray.gunzip(): ByteArray
1414

15+
expect fun ByteArray.gunzipOrNull(): ByteArray?
16+
1517
// gzip => string
1618

1719
expect fun ByteArray.gunzipToString(): String
20+
21+
expect fun ByteArray.gunzipToStringOrNull(): String?

module-extkt-crypto/src/commonMain/kotlin/text/base64.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,22 @@ expect fun String.encodeBase64UrlSafe(offset: Int, length: Int = this.length): S
2828

2929
expect fun String.decodeBase64(): ByteArray
3030

31+
expect fun String.decodeBase64OrNull(): ByteArray?
32+
3133
// base64url => byte[]
3234

3335
expect fun String.decodeBase64UrlSafe(): ByteArray
3436

37+
expect fun String.decodeBase64UrlSafeOrNull(): ByteArray?
38+
3539
// base64 => string
3640

3741
expect fun String.decodeBase64ToString(): String
3842

43+
expect fun String.decodeBase64ToStringOrNull(): String?
44+
3945
// base64url => string
4046

4147
expect fun String.decodeBase64UrlSafeToString(): String
48+
49+
expect fun String.decodeBase64UrlSafeToStringOrNull(): String?

module-extkt-crypto/src/commonMain/kotlin/text/hex.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ expect fun String.encodeHex(): String
1212

1313
expect fun String.decodeHex(): ByteArray
1414

15+
expect fun String.decodeHexOrNull(): ByteArray?
16+
1517
// hex => string
1618

1719
expect fun String.decodeHexToString(): String
20+
21+
expect fun String.decodeHexToStringOrNull(): String?

module-extkt-crypto/src/jsMain/kotlin/compression/deflate.js.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ actual fun ByteArray.inflate(): ByteArray {
1212
TODO("Not yet implemented")
1313
}
1414

15+
actual fun ByteArray.inflateOrNull(): ByteArray? {
16+
TODO("Not yet implemented")
17+
}
18+
1519
actual fun ByteArray.inflateToString(): String {
1620
TODO("Not yet implemented")
1721
}
22+
23+
actual fun ByteArray.inflateToStringOrNull(): String? {
24+
TODO("Not yet implemented")
25+
}

module-extkt-crypto/src/jsMain/kotlin/compression/gzip.js.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ actual fun ByteArray.gunzip(): ByteArray {
1212
TODO("Not yet implemented")
1313
}
1414

15+
actual fun ByteArray.gunzipOrNull(): ByteArray? {
16+
TODO("Not yet implemented")
17+
}
18+
1519
actual fun ByteArray.gunzipToString(): String {
1620
TODO("Not yet implemented")
1721
}
22+
23+
actual fun ByteArray.gunzipToStringOrNull(): String? {
24+
TODO("Not yet implemented")
25+
}

module-extkt-crypto/src/jsMain/kotlin/text/base64.js.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.cufy.text
22

3+
import kotlin.coroutines.cancellation.CancellationException
34
import kotlin.io.encoding.Base64
45
import kotlin.io.encoding.ExperimentalEncodingApi
56

@@ -65,16 +66,55 @@ actual fun String.decodeBase64(): ByteArray {
6566
return Base64.decode(this)
6667
}
6768

69+
@OptIn(ExperimentalEncodingApi::class)
70+
actual fun String.decodeBase64OrNull(): ByteArray? {
71+
return try {
72+
Base64.decode(this)
73+
} catch (_: IllegalArgumentException) {
74+
null
75+
}
76+
}
77+
6878
@OptIn(ExperimentalEncodingApi::class)
6979
actual fun String.decodeBase64UrlSafe(): ByteArray {
7080
val base64 = base64UrlSafeToBase64(this)
7181
return Base64.decode(base64)
7282
}
7383

84+
@OptIn(ExperimentalEncodingApi::class)
85+
actual fun String.decodeBase64UrlSafeOrNull(): ByteArray? {
86+
return try {
87+
val base64 = base64UrlSafeToBase64(this)
88+
Base64.decode(base64)
89+
} catch (_: IllegalArgumentException) {
90+
null
91+
}
92+
}
93+
7494
actual fun String.decodeBase64ToString(): String {
7595
return atob(this)
7696
}
7797

98+
actual fun String.decodeBase64ToStringOrNull(): String? {
99+
return try {
100+
atob(this)
101+
} catch (e: CancellationException) {
102+
throw e
103+
} catch (_: Throwable) {
104+
null
105+
}
106+
}
107+
78108
actual fun String.decodeBase64UrlSafeToString(): String {
79109
return atobUrlSafe(this)
80110
}
111+
112+
actual fun String.decodeBase64UrlSafeToStringOrNull(): String? {
113+
return try {
114+
atobUrlSafe(this)
115+
} catch (e: CancellationException) {
116+
throw e
117+
} catch (_: Throwable) {
118+
null
119+
}
120+
}

module-extkt-crypto/src/jsMain/kotlin/text/hex.js.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ actual fun String.decodeHex(): ByteArray {
1212
TODO("Not yet implemented")
1313
}
1414

15+
actual fun String.decodeHexOrNull(): ByteArray? {
16+
TODO("Not yet implemented")
17+
}
18+
1519
actual fun String.decodeHexToString(): String {
1620
TODO("Not yet implemented")
1721
}
22+
23+
actual fun String.decodeHexToStringOrNull(): String? {
24+
TODO("Not yet implemented")
25+
}

module-extkt-crypto/src/jvmMain/kotlin/compression/deflate.jvm.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.util.zip.Deflater
88
import java.util.zip.DeflaterOutputStream
99
import java.util.zip.Inflater
1010
import java.util.zip.InflaterInputStream
11+
import kotlin.coroutines.cancellation.CancellationException
1112

1213
actual fun ByteArray.deflate(level: Int): ByteArray {
1314
val baos = ByteArrayOutputStream()
@@ -33,10 +34,30 @@ actual fun ByteArray.inflate(): ByteArray {
3334
return inflate.use { it.readBytes() }
3435
}
3536

37+
actual fun ByteArray.inflateOrNull(): ByteArray? {
38+
return try {
39+
inflate()
40+
} catch (e: CancellationException) {
41+
throw e
42+
} catch (_: RuntimeException) {
43+
null
44+
}
45+
}
46+
3647
actual fun ByteArray.inflateToString(): String {
3748
val bais = ByteArrayInputStream(this)
3849
val inflater = Inflater()
3950
val inflate = InflaterInputStream(bais, inflater)
4051
val isr = InputStreamReader(inflate)
4152
return isr.use { it.readText() }
4253
}
54+
55+
actual fun ByteArray.inflateToStringOrNull(): String? {
56+
return try {
57+
inflateToString()
58+
} catch (e: CancellationException) {
59+
throw e
60+
} catch (_: RuntimeException) {
61+
null
62+
}
63+
}

module-extkt-crypto/src/jvmMain/kotlin/compression/gzip.jvm.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.io.InputStreamReader
66
import java.io.OutputStreamWriter
77
import java.util.zip.GZIPInputStream
88
import java.util.zip.GZIPOutputStream
9+
import kotlin.coroutines.cancellation.CancellationException
910

1011
actual fun ByteArray.gzip(): ByteArray {
1112
val baos = ByteArrayOutputStream()
@@ -28,9 +29,29 @@ actual fun ByteArray.gunzip(): ByteArray {
2829
return gzip.use { it.readBytes() }
2930
}
3031

32+
actual fun ByteArray.gunzipOrNull(): ByteArray? {
33+
return try {
34+
gunzip()
35+
} catch (e: CancellationException) {
36+
throw e
37+
} catch (_: RuntimeException) {
38+
null
39+
}
40+
}
41+
3142
actual fun ByteArray.gunzipToString(): String {
3243
val bais = ByteArrayInputStream(this)
3344
val gzip = GZIPInputStream(bais)
3445
val isr = InputStreamReader(gzip)
3546
return isr.use { it.readText() }
3647
}
48+
49+
actual fun ByteArray.gunzipToStringOrNull(): String? {
50+
return try {
51+
gunzipToString()
52+
} catch (e: CancellationException) {
53+
throw e
54+
} catch (_: RuntimeException) {
55+
null
56+
}
57+
}

0 commit comments

Comments
 (0)