From 4d0ad826a0db201cd52e3d5cef8f9d8b78087001 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 28 Apr 2022 23:32:35 +0600 Subject: [PATCH 1/5] Update libs.versions.toml --- gradle/libs.versions.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8a12bd46d70..303f159ff2c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,9 +11,9 @@ jb-dokka = "1.6.21" klock = "2.7.0" uuid = "0.4.0" -ktor = "2.0.0" +ktor = "2.0.1" -gh-release = "2.3.7" +gh-release = "2.3.12" android-gradle = "7.0.4" dexcount = "3.0.1" From 848bc5ec10a26231aec3c17a707f248e2d0c41c7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 28 Apr 2022 23:45:57 +0600 Subject: [PATCH 2/5] Update gradle.properties --- gradle.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index d66cba7e505..5236d750e64 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,5 +14,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.10.0 -android_code_version=115 +version=0.10.1 +android_code_version=116 From 31fbd3cad7e9af24c232ab73b40b63a5d7700034 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 29 Apr 2022 18:42:59 +0600 Subject: [PATCH 3/5] hmacSha256 and hex --- CHANGELOG.md | 8 ++++++ .../kotlin/dev/inmo/micro_utils/crypto/Hex.kt | 3 +++ .../kotlin/dev/inmo/micro_utils/crypto/Sha.kt | 3 +++ .../kotlin/dev/inmo/micro_utils/crypto/Hex.kt | 15 +++++++++++ .../dev/inmo/micro_utils/crypto/HmacSHA256.kt | 12 +++++++++ .../dev/inmo/micro_utils/crypto/CryptoJs.kt | 6 +++++ .../inmo/micro_utils/crypto/ShaHexActual.kt | 27 +++++++++++++++++++ gradle/libs.versions.toml | 2 +- 8 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Hex.kt create mode 100644 crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Sha.kt create mode 100644 crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Hex.kt create mode 100644 crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/HmacSHA256.kt create mode 100644 crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/ShaHexActual.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index a5a201cb3d5..4ae1533915b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 0.10.1 + +* `Versions`: + * `Ktor`: `2.0.0` -> `2.0.1` +* `Crypto`: + * Add `hmacSha256` + * Add `hex` + ## 0.10.0 * `Versions`: diff --git a/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Hex.kt b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Hex.kt new file mode 100644 index 00000000000..3d20a61b567 --- /dev/null +++ b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Hex.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.crypto + +expect fun SourceString.hex(): String diff --git a/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Sha.kt b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Sha.kt new file mode 100644 index 00000000000..52ea32f236e --- /dev/null +++ b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Sha.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.crypto + +expect fun SourceString.hmacSha256(key: String): String diff --git a/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Hex.kt b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Hex.kt new file mode 100644 index 00000000000..031e001e63f --- /dev/null +++ b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Hex.kt @@ -0,0 +1,15 @@ +package dev.inmo.micro_utils.crypto + +import kotlin.test.* + +class Hex { + @Test + fun testSimpleHmacSHA256Message() { + val text = "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 resultHex = text.hex() + assertEquals( + "4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E73656374657475722061646970697363696E6720656C69742C2073656420646F20656975736D6F642074656D706F7220696E6369646964756E74207574206C61626F726520657420646F6C6F7265206D61676E6120616C697175612E20557420656E696D206164206D696E696D2076656E69616D2C2071756973206E6F737472756420657865726369746174696F6E20756C6C616D636F206C61626F726973206E69736920757420616C697175697020657820656120636F6D6D6F646F20636F6E7365717561742E2044756973206175746520697275726520646F6C6F7220696E20726570726568656E646572697420696E20766F6C7570746174652076656C697420657373652063696C6C756D20646F6C6F726520657520667567696174206E756C6C612070617269617475722E204578636570746575722073696E74206F6363616563617420637570696461746174206E6F6E2070726F6964656E742C2073756E7420696E2063756C706120717569206F666669636961206465736572756E74206D6F6C6C697420616E696D20696420657374206C61626F72756D2E", + resultHex + ) + } +} diff --git a/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/HmacSHA256.kt b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/HmacSHA256.kt new file mode 100644 index 00000000000..9892b5407d3 --- /dev/null +++ b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/HmacSHA256.kt @@ -0,0 +1,12 @@ +package dev.inmo.micro_utils.crypto + +import kotlin.test.* + +class HmacSHA256 { + @Test + fun testSimpleHmacSHA256Message() { + val text = "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 resultSha = text.hmacSha256("Example") + assertEquals("5A481D59329EF862B158EEDC95392EBB22492BA3014661A3379D8201DB992484", resultSha) + } +} 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 index 3f5e90439f8..6845e677572 100644 --- a/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt +++ b/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt @@ -7,3 +7,9 @@ external interface CryptoJs { @JsModule("crypto-js") @JsNonModule external val CryptoJS: CryptoJs + +actual fun SourceString.hmacSha256(key: String) = CryptoJS.asDynamic().HmacSHA256(this, key).unsafeCast() + +actual fun SourceBytes.hex() = CryptoJS.asDynamic().format.Hex(this).unsafeCast() + +actual fun SourceString.hex() = CryptoJS.asDynamic().format.Hex(this).unsafeCast() diff --git a/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/ShaHexActual.kt b/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/ShaHexActual.kt new file mode 100644 index 00000000000..53c8fd845df --- /dev/null +++ b/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/ShaHexActual.kt @@ -0,0 +1,27 @@ +package dev.inmo.micro_utils.crypto + +import javax.crypto.Mac +import javax.crypto.spec.SecretKeySpec + +val HEX_ARRAY = "0123456789ABCDEF".toCharArray() + +fun SourceBytes.hex(): String { + val hexChars = CharArray(size * 2) + for (j in indices) { + val v: Int = this[j].toInt() and 0xFF + hexChars[j * 2] = HEX_ARRAY[v ushr 4] + hexChars[j * 2 + 1] = HEX_ARRAY[v and 0x0F] + } + return String(hexChars) +} + +actual fun SourceString.hmacSha256(key: String): String { + val mac = Mac.getInstance("HmacSHA256") + + val secretKey = SecretKeySpec(key.toByteArray(), "HmacSHA256") + mac.init(secretKey) + + return mac.doFinal(toByteArray()).hex() +} + +actual fun SourceString.hex(): String = toByteArray().hex() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 303f159ff2c..8e1f1fa7456 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -13,7 +13,7 @@ uuid = "0.4.0" ktor = "2.0.1" -gh-release = "2.3.12" +gh-release = "2.3.7" android-gradle = "7.0.4" dexcount = "3.0.1" From 1dafd1352aa2df4a8060889ed3df8952a28f5af5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 29 Apr 2022 18:47:23 +0600 Subject: [PATCH 4/5] hotfix --- .../src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt | 2 -- 1 file changed, 2 deletions(-) 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 index 6845e677572..bfde5aee0bb 100644 --- a/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt +++ b/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt @@ -10,6 +10,4 @@ external val CryptoJS: CryptoJs actual fun SourceString.hmacSha256(key: String) = CryptoJS.asDynamic().HmacSHA256(this, key).unsafeCast() -actual fun SourceBytes.hex() = CryptoJS.asDynamic().format.Hex(this).unsafeCast() - actual fun SourceString.hex() = CryptoJS.asDynamic().format.Hex(this).unsafeCast() From bdb4988569778ad4745f60c40960360ad41890d4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 29 Apr 2022 19:46:15 +0600 Subject: [PATCH 5/5] fixes --- .../kotlin/dev/inmo/micro_utils/crypto/Hex.kt | 14 +++++++++++++- .../kotlin/dev/inmo/micro_utils/crypto/Hex.kt | 2 +- .../dev/inmo/micro_utils/crypto/HmacSHA256.kt | 2 +- .../kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt | 6 +++--- .../dev/inmo/micro_utils/crypto/ShaHexActual.kt | 14 -------------- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Hex.kt b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Hex.kt index 3d20a61b567..a53b5314f5e 100644 --- a/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Hex.kt +++ b/crypto/src/commonMain/kotlin/dev/inmo/micro_utils/crypto/Hex.kt @@ -1,3 +1,15 @@ package dev.inmo.micro_utils.crypto -expect fun SourceString.hex(): String +val HEX_ARRAY = "0123456789abcdef".toCharArray() + +fun SourceBytes.hex(): String { + val hexChars = CharArray(size * 2) + for (j in indices) { + val v: Int = this[j].toInt() and 0xFF + hexChars[j * 2] = HEX_ARRAY[v ushr 4] + hexChars[j * 2 + 1] = HEX_ARRAY[v and 0x0F] + } + return hexChars.concatToString() +} + +fun SourceString.hex(): String = encodeToByteArray().hex() diff --git a/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Hex.kt b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Hex.kt index 031e001e63f..16184b6635a 100644 --- a/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Hex.kt +++ b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/Hex.kt @@ -8,7 +8,7 @@ class Hex { val text = "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 resultHex = text.hex() assertEquals( - "4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E73656374657475722061646970697363696E6720656C69742C2073656420646F20656975736D6F642074656D706F7220696E6369646964756E74207574206C61626F726520657420646F6C6F7265206D61676E6120616C697175612E20557420656E696D206164206D696E696D2076656E69616D2C2071756973206E6F737472756420657865726369746174696F6E20756C6C616D636F206C61626F726973206E69736920757420616C697175697020657820656120636F6D6D6F646F20636F6E7365717561742E2044756973206175746520697275726520646F6C6F7220696E20726570726568656E646572697420696E20766F6C7570746174652076656C697420657373652063696C6C756D20646F6C6F726520657520667567696174206E756C6C612070617269617475722E204578636570746575722073696E74206F6363616563617420637570696461746174206E6F6E2070726F6964656E742C2073756E7420696E2063756C706120717569206F666669636961206465736572756E74206D6F6C6C697420616E696D20696420657374206C61626F72756D2E", + "4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742c2073656420646f20656975736d6f642074656d706f7220696e6369646964756e74207574206c61626f726520657420646f6c6f7265206d61676e6120616c697175612e20557420656e696d206164206d696e696d2076656e69616d2c2071756973206e6f737472756420657865726369746174696f6e20756c6c616d636f206c61626f726973206e69736920757420616c697175697020657820656120636f6d6d6f646f20636f6e7365717561742e2044756973206175746520697275726520646f6c6f7220696e20726570726568656e646572697420696e20766f6c7570746174652076656c697420657373652063696c6c756d20646f6c6f726520657520667567696174206e756c6c612070617269617475722e204578636570746575722073696e74206f6363616563617420637570696461746174206e6f6e2070726f6964656e742c2073756e7420696e2063756c706120717569206f666669636961206465736572756e74206d6f6c6c697420616e696d20696420657374206c61626f72756d2e", resultHex ) } diff --git a/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/HmacSHA256.kt b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/HmacSHA256.kt index 9892b5407d3..60ea7433568 100644 --- a/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/HmacSHA256.kt +++ b/crypto/src/commonTest/kotlin/dev/inmo/micro_utils/crypto/HmacSHA256.kt @@ -7,6 +7,6 @@ class HmacSHA256 { fun testSimpleHmacSHA256Message() { val text = "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 resultSha = text.hmacSha256("Example") - assertEquals("5A481D59329EF862B158EEDC95392EBB22492BA3014661A3379D8201DB992484", resultSha) + assertEquals("5a481d59329ef862b158eedc95392ebb22492ba3014661a3379d8201db992484", resultSha) } } 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 index bfde5aee0bb..f6ac57bb317 100644 --- a/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt +++ b/crypto/src/jsMain/kotlin/dev/inmo/micro_utils/crypto/CryptoJs.kt @@ -8,6 +8,6 @@ external interface CryptoJs { @JsNonModule external val CryptoJS: CryptoJs -actual fun SourceString.hmacSha256(key: String) = CryptoJS.asDynamic().HmacSHA256(this, key).unsafeCast() - -actual fun SourceString.hex() = CryptoJS.asDynamic().format.Hex(this).unsafeCast() +actual fun SourceString.hmacSha256(key: String): String { + return CryptoJS.asDynamic().HmacSHA256(this, key).toString().unsafeCast() +} diff --git a/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/ShaHexActual.kt b/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/ShaHexActual.kt index 53c8fd845df..90c27879ad8 100644 --- a/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/ShaHexActual.kt +++ b/crypto/src/jvmMain/kotlin/dev/inmo/micro_utils/crypto/ShaHexActual.kt @@ -3,18 +3,6 @@ package dev.inmo.micro_utils.crypto import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec -val HEX_ARRAY = "0123456789ABCDEF".toCharArray() - -fun SourceBytes.hex(): String { - val hexChars = CharArray(size * 2) - for (j in indices) { - val v: Int = this[j].toInt() and 0xFF - hexChars[j * 2] = HEX_ARRAY[v ushr 4] - hexChars[j * 2 + 1] = HEX_ARRAY[v and 0x0F] - } - return String(hexChars) -} - actual fun SourceString.hmacSha256(key: String): String { val mac = Mac.getInstance("HmacSHA256") @@ -23,5 +11,3 @@ actual fun SourceString.hmacSha256(key: String): String { return mac.doFinal(toByteArray()).hex() } - -actual fun SourceString.hex(): String = toByteArray().hex()