diff --git a/CHANGELOG.md b/CHANGELOG.md index 74476c33e82..23739bba7db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # Changelog +## 0.4.21 + +* `Common` + * `JS` + * Extension `DataView#toByteArray` has been added + * Extension `ArrayBuffer#toByteArray` has been added + * Extension `ByteArray#toDataView` has been added + * Extension `ByteArray#toArrayBuffer` has been added +* `Coroutines` + * `JS` + * Extension `Blob#toByteArray` has been added +* `Crypto` + * Subproject has been created +* `Serialization` + * `Base64` + * Currently, it is not depended on `common` project and use `crypto` instead + ## 0.4.20 * `Serialization` diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Base64.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Base64.kt index cba5c9c01a7..26bdf061c77 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Base64.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Base64.kt @@ -12,12 +12,19 @@ private val BASE64_INVERSE_ALPHABET = IntArray(256) { internal fun Int.toBase64(): Char = BASE64_ALPHABET[this] internal fun Byte.fromBase64(): Byte = BASE64_INVERSE_ALPHABET[toInt() and 0xff].toByte() and BASE64_MASK +const val replacedInCrypto = "Replaced in crypto subproject" + +@Deprecated(replacedInCrypto, ReplaceWith("EncodedBase64String", "dev.inmo.micro_utils.crypto")) typealias EncodedBase64String = String +@Deprecated(replacedInCrypto, ReplaceWith("EncodedByteArray", "dev.inmo.micro_utils.crypto")) typealias EncodedByteArray = ByteArray +@Deprecated(replacedInCrypto, ReplaceWith("encodeBase64String()", "dev.inmo.micro_utils.crypto")) fun String.encodeBase64String(): EncodedBase64String = encodeToByteArray().encodeBase64String() +@Deprecated(replacedInCrypto, ReplaceWith("encodeBase64()", "dev.inmo.micro_utils.crypto")) fun String.encodeBase64(): EncodedByteArray = encodeToByteArray().encodeBase64() +@Deprecated(replacedInCrypto, ReplaceWith("encodeBase64String()", "dev.inmo.micro_utils.crypto")) fun ByteArray.encodeBase64String(): EncodedBase64String = buildString { var i = 0 while (this@encodeBase64String.size > i) { @@ -45,11 +52,15 @@ fun ByteArray.encodeBase64String(): EncodedBase64String = buildString { i += read } } +@Deprecated(replacedInCrypto, ReplaceWith("encodeBase64()", "dev.inmo.micro_utils.crypto")) fun ByteArray.encodeBase64(): EncodedByteArray = encodeBase64String().encodeToByteArray() +@Deprecated(replacedInCrypto, ReplaceWith("decodeBase64()", "dev.inmo.micro_utils.crypto")) fun EncodedBase64String.decodeBase64() = dropLastWhile { it == BASE64_PAD }.encodeToByteArray().decodeBase64() +@Deprecated(replacedInCrypto, ReplaceWith("decodeBase64String()", "dev.inmo.micro_utils.crypto")) fun EncodedBase64String.decodeBase64String() = decodeBase64().decodeToString() +@Deprecated(replacedInCrypto, ReplaceWith("decodeBase64()", "dev.inmo.micro_utils.crypto")) fun EncodedByteArray.decodeBase64(): ByteArray { val result = mutableListOf() val data = ByteArray(4) @@ -74,4 +85,5 @@ fun EncodedByteArray.decodeBase64(): ByteArray { return result.toByteArray() } +@Deprecated(replacedInCrypto, ReplaceWith("decodeBase64String()", "dev.inmo.micro_utils.crypto")) fun EncodedByteArray.decodeBase64String() = decodeBase64().decodeToString() diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/Base64Text.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/Base64Text.kt index 69104f077a1..8fdc9b95b7d 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/Base64Text.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/Base64Text.kt @@ -11,6 +11,7 @@ class Base64Text { val testText = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?""" val base64Text = """TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLgpTZWQgdXQgcGVyc3BpY2lhdGlzIHVuZGUgb21uaXMgaX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aWxsdW0gcXVpIGRvbG9yZW0gZXVtIGZ1Z2lhdCBxdW8gdm9sdXB0YXMgbnVsbGEgcGFyaWF0dXI/""" + @Test fun decodeEncode() { val encoded = testText.encodeBase64String() @@ -21,6 +22,7 @@ Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium dolor val urlTestText = "https://example.com?isitexample=1" val urlTestBase64Text = "aHR0cHM6Ly9leGFtcGxlLmNvbT9pc2l0ZXhhbXBsZT0x" + @Test fun decodeEncodeUrl() { val encoded = urlTestText.encodeBase64String() diff --git a/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/ByteArrayDataView.kt b/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/ByteArrayDataView.kt new file mode 100644 index 00000000000..71cb0468f89 --- /dev/null +++ b/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/ByteArrayDataView.kt @@ -0,0 +1,15 @@ +package dev.inmo.micro_utils.common + +import org.khronos.webgl.* + +fun DataView.toByteArray() = ByteArray(this.byteLength) { + getInt8(it) +} + +fun ArrayBuffer.toByteArray() = Int8Array(this) as ByteArray + +fun ByteArray.toDataView() = DataView(ArrayBuffer(size)).also { + forEachIndexed { i, byte -> it.setInt8(i, byte) } +} + +fun ByteArray.toArrayBuffer() = toDataView().buffer diff --git a/coroutines/src/jsMain/kotlin/dev.inmo.micro_utils.coroutines/ByteArrayBlob.kt b/coroutines/src/jsMain/kotlin/dev.inmo.micro_utils.coroutines/ByteArrayBlob.kt new file mode 100644 index 00000000000..e118398d56e --- /dev/null +++ b/coroutines/src/jsMain/kotlin/dev.inmo.micro_utils.coroutines/ByteArrayBlob.kt @@ -0,0 +1,10 @@ +package dev.inmo.micro_utils.coroutines + +import kotlinx.coroutines.await +import org.khronos.webgl.Int8Array +import org.w3c.fetch.Response +import org.w3c.files.Blob + +suspend fun Blob.toByteArray() = Int8Array( + Response(this).arrayBuffer().await() +) as ByteArray diff --git a/crypto/build.gradle b/crypto/build.gradle new file mode 100644 index 00000000000..5759368da46 --- /dev/null +++ b/crypto/build.gradle @@ -0,0 +1,30 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" + id "com.android.library" +} + +apply from: "$mppProjectWithSerializationPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api project(":micro_utils.common") + } + } + jsMain { + dependencies { + api npm("crypto-js", "$crypto_js_version") + } + } + } +} + +android { + sourceSets { + main { + java.srcDirs += [ "src/jvmMain/kotlin" ] + } + } +} diff --git a/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Base64.kt b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Base64.kt new file mode 100644 index 00000000000..8eff015e7b6 --- /dev/null +++ b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Base64.kt @@ -0,0 +1,77 @@ +package dev.inmo.micro_utils.crypto + +import kotlin.experimental.and + +private const val BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" +private const val BASE64_MASK: Byte = 0x3f +private const val BASE64_PAD = '=' +private val BASE64_INVERSE_ALPHABET = IntArray(256) { + BASE64_ALPHABET.indexOf(it.toChar()) +} + +internal fun Int.toBase64(): Char = BASE64_ALPHABET[this] +internal fun Byte.fromBase64(): Byte = BASE64_INVERSE_ALPHABET[toInt() and 0xff].toByte() and BASE64_MASK + +typealias EncodedBase64String = String +typealias EncodedByteArray = ByteArray + +fun SourceString.encodeBase64String(): EncodedBase64String = encodeToByteArray().encodeBase64String() +fun SourceString.encodeBase64(): EncodedByteArray = encodeToByteArray().encodeBase64() + +fun SourceBytes.encodeBase64String(): EncodedBase64String = buildString { + var i = 0 + while (this@encodeBase64String.size > i) { + val read = kotlin.math.min(3, this@encodeBase64String.size - i) + val data = ByteArray(3) { + if (it < read) { + this@encodeBase64String[it + i] + } else { + 0 + } + } + + val padSize = (data.size - read) * 8 / 6 + val chunk = ((data[0].toInt() and 0xFF) shl 16) or + ((data[1].toInt() and 0xFF) shl 8) or + (data[2].toInt() and 0xFF) + + for (index in data.size downTo padSize) { + val char = (chunk shr (6 * index)) and BASE64_MASK.toInt() + append(char.toBase64()) + } + + repeat(padSize) { append(BASE64_PAD) } + + i += read + } +} +fun SourceBytes.encodeBase64(): EncodedByteArray = encodeBase64String().encodeToByteArray() + +fun EncodedBase64String.decodeBase64(): SourceBytes = dropLastWhile { it == BASE64_PAD }.encodeToByteArray().decodeBase64() +fun EncodedBase64String.decodeBase64String(): SourceString = decodeBase64().decodeToString() + +fun EncodedByteArray.decodeBase64(): SourceBytes { + val result = mutableListOf() + val data = ByteArray(4) + (0 until size step 4).forEach { i -> + var read = 0 + for (j in 0 until 4) { + if (j + i < size) { + data[j] = get(j + i) + read++ + } else { + break + } + } + val chunk = data.foldIndexed(0) { index, result, current -> + result or (current.fromBase64().toInt() shl ((3 - index) * 6)) + } + for (index in data.size - 2 downTo (data.size - read)) { + val origin = (chunk shr (8 * index)) and 0xff + result.add(origin.toByte()) + } + } + + return result.toByteArray() +} +fun EncodedByteArray.decodeBase64String(): SourceString = decodeBase64().decodeToString() diff --git a/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Common.kt b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Common.kt new file mode 100644 index 00000000000..aa5556096f1 --- /dev/null +++ b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Common.kt @@ -0,0 +1,11 @@ +package dev.inmo.micro_utils.crypto + +/** + * This typealias shows that corresponding [ByteArray] is an raw data + */ +typealias SourceBytes = ByteArray +/** + * This typealias shows that corresponding [String] is an raw data + */ +typealias SourceString = String + diff --git a/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/MD5.kt b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/MD5.kt new file mode 100644 index 00000000000..76ad1fa9560 --- /dev/null +++ b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/MD5.kt @@ -0,0 +1,6 @@ +package dev.inmo.micro_utils.crypto + +typealias MD5 = String + +expect fun SourceBytes.md5(): MD5 +fun SourceString.md5(): MD5 = encodeToByteArray().md5() diff --git a/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Base64Text.kt b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Base64Text.kt new file mode 100644 index 00000000000..92c9170d714 --- /dev/null +++ b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Base64Text.kt @@ -0,0 +1,32 @@ +package dev.inmo.micro_utils.crypto + +import kotlin.test.Test +import kotlin.test.assertEquals + + + +class Base64Text { + + val testText = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?""" + val base64Text = """TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2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0gcXVpYSBkb2xvciBzaXQgYW1ldCwgY29uc2VjdGV0dXIsIGFkaXBpc2NpIHZlbGl0LCBzZWQgcXVpYSBub24gbnVtcXVhbSBlaXVzIG1vZGkgdGVtcG9yYSBpbmNpZHVudCB1dCBsYWJvcmUgZXQgZG9sb3JlIG1hZ25hbSBhbGlxdWFtIHF1YWVyYXQgdm9sdXB0YXRlbS4gVXQgZW5pbSBhZCBtaW5pbWEgdmVuaWFtLCBxdWlzIG5vc3RydW0gZXhlcmNpdGF0aW9uZW0gdWxsYW0gY29ycG9yaXMgc3VzY2lwaXQgbGFib3Jpb3NhbSwgbmlzaSB1dCBhbGlxdWlkIGV4IGVhIGNvbW1vZGkgY29uc2VxdWF0dXI/IFF1aXMgYXV0ZW0gdmVsIGV1bSBpdXJlIHJlcHJlaGVuZGVyaXQgcXVpIGluIGVhIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIHF1YW0gbmloaWwgbW9sZXN0aWFlIGNvbnNlcXVhdHVyLCB2ZWwgaWxsdW0gcXVpIGRvbG9yZW0gZXVtIGZ1Z2lhdCBxdW8gdm9sdXB0YXMgbnVsbGEgcGFyaWF0dXI/""" + + @Test + fun decodeEncode() { + val encoded = testText.encodeBase64String() + assertEquals(base64Text, encoded) + val decoded = encoded.decodeBase64String() + assertEquals(testText, decoded) + } + + val urlTestText = "https://example.com?isitexample=1" + val urlTestBase64Text = "aHR0cHM6Ly9leGFtcGxlLmNvbT9pc2l0ZXhhbXBsZT0x" + + @Test + fun decodeEncodeUrl() { + val encoded = urlTestText.encodeBase64String() + assertEquals(urlTestBase64Text, encoded) + val decoded = encoded.decodeBase64String() + assertEquals(urlTestText, decoded) + } +} \ No newline at end of file diff --git a/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/MD5Test.kt b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/MD5Test.kt new file mode 100644 index 00000000000..73eda2b665f --- /dev/null +++ b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/MD5Test.kt @@ -0,0 +1,17 @@ +package dev.inmo.micro_utils.crypto + +import kotlin.test.Test +import kotlin.test.assertEquals + +class MD5Test { + val source = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." + val expectedMD5 = "db89bb5ceab87f9c0fcc2ab36c189c2c" + @Test + fun testMD5() { + var i = 0 + source.md5().forEach { + assertEquals(expectedMD5[i], it) + i++ + } + } +} diff --git a/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt b/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt new file mode 100644 index 00000000000..3f5e90439f8 --- /dev/null +++ b/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt @@ -0,0 +1,9 @@ +package dev.inmo.micro_utils.crypto + +external interface CryptoJs { + fun MD5(data: String): String +} + +@JsModule("crypto-js") +@JsNonModule +external val CryptoJS: CryptoJs diff --git a/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/MD5Realization.kt b/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/MD5Realization.kt new file mode 100644 index 00000000000..76b6ab556d8 --- /dev/null +++ b/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/MD5Realization.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.crypto + +actual fun SourceBytes.md5(): MD5 = CryptoJS.MD5(decodeToString()) diff --git a/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/MD5Realization.kt b/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/MD5Realization.kt new file mode 100644 index 00000000000..f81644cdf4d --- /dev/null +++ b/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/MD5Realization.kt @@ -0,0 +1,9 @@ +package dev.inmo.micro_utils.crypto + +import java.math.BigInteger +import java.security.MessageDigest + +actual fun SourceBytes.md5(): MD5 = BigInteger( + 1, + MessageDigest.getInstance("MD5").digest(this) +).toString(16) diff --git a/crypto/src/main/AndroidManifest.xml b/crypto/src/main/AndroidManifest.xml new file mode 100644 index 00000000000..20f225901d6 --- /dev/null +++ b/crypto/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index cec7824686e..09bed5cb818 100644 --- a/gradle.properties +++ b/gradle.properties @@ -33,6 +33,10 @@ junit_version=4.12 test_ext_junit_version=1.1.2 espresso_core=3.3.0 +# JS NPM + +crypto_js_version=4.0.0 + # Dokka dokka_version=1.4.20 @@ -40,5 +44,5 @@ dokka_version=1.4.20 # Project data group=dev.inmo -version=0.4.20 -android_code_version=24 +version=0.4.21 +android_code_version=25 diff --git a/serialization/base64/build.gradle b/serialization/base64/build.gradle index 082ce99346a..403f8887d7b 100644 --- a/serialization/base64/build.gradle +++ b/serialization/base64/build.gradle @@ -10,7 +10,7 @@ kotlin { sourceSets { commonMain { dependencies { - api project(":micro_utils.common") + api project(":micro_utils.crypto") } } } diff --git a/serialization/base64/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/base64/Base64Serializer.kt b/serialization/base64/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/base64/Base64Serializer.kt index 1a623699599..435730f24ac 100644 --- a/serialization/base64/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/base64/Base64Serializer.kt +++ b/serialization/base64/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/base64/Base64Serializer.kt @@ -1,6 +1,6 @@ package dev.inmo.micro_utils.serialization.base64 -import dev.inmo.micro_utils.common.* +import dev.inmo.micro_utils.crypto.* import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializer import kotlinx.serialization.builtins.serializer diff --git a/settings.gradle b/settings.gradle index 6034af3c57d..7b43068aaa5 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,6 +2,7 @@ rootProject.name='micro_utils' String[] includes = [ ":common", + ":crypto", ":selector:common", ":pagination:common", ":pagination:exposed",