From 31fbd3cad7e9af24c232ab73b40b63a5d7700034 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 29 Apr 2022 18:42:59 +0600 Subject: [PATCH] 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"