diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/Crypto.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/Crypto.kt index 163dffea01..af150b34fa 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/Crypto.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/Crypto.kt @@ -1,7 +1,19 @@ package dev.inmo.tgbotapi.utils +import dev.inmo.micro_utils.crypto.SourceBytes import dev.inmo.micro_utils.crypto.SourceString internal expect fun SourceString.hmacSha256(key: String): String +private val HEX_ARRAY = "0123456789abcdef".toCharArray() -internal expect fun SourceString.hex(): String +internal 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() +} + +internal fun SourceString.hex(): String = encodeToByteArray().hex() diff --git a/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt b/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt index 821bdea167..a8b23eae8a 100644 --- a/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt +++ b/tgbotapi.core/src/jsMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt @@ -3,6 +3,4 @@ package dev.inmo.tgbotapi.utils import dev.inmo.micro_utils.crypto.CryptoJS import dev.inmo.micro_utils.crypto.SourceString -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) = CryptoJS.asDynamic().HmacSHA256(this, key).toString().unsafeCast() diff --git a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt index 7a63c5f7e6..65075f0119 100644 --- a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt +++ b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/CryptoActual.kt @@ -1,22 +1,9 @@ package dev.inmo.tgbotapi.utils -import dev.inmo.micro_utils.crypto.SourceBytes import dev.inmo.micro_utils.crypto.SourceString import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec -val HEX_ARRAY = "0123456789ABCDEF".toCharArray() - -private 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") @@ -25,5 +12,3 @@ actual fun SourceString.hmacSha256(key: String): String { return mac.doFinal(toByteArray()).hex() } - -actual fun SourceString.hex(): String = toByteArray().hex()