diff options
| -rw-r--r-- | modules/orgflow-crypto/src/linuxX64Main/kotlin/jp/orgflow/crypto/HmacService.linuxX64.kt | 10 | ||||
| -rw-r--r-- | modules/orgflow-crypto/src/wasmJsMain/kotlin/jp/orgflow/crypto/HmacService.wasm.kt | 10 |
2 files changed, 14 insertions, 6 deletions
diff --git a/modules/orgflow-crypto/src/linuxX64Main/kotlin/jp/orgflow/crypto/HmacService.linuxX64.kt b/modules/orgflow-crypto/src/linuxX64Main/kotlin/jp/orgflow/crypto/HmacService.linuxX64.kt index fa0f0cd..c6b13ef 100644 --- a/modules/orgflow-crypto/src/linuxX64Main/kotlin/jp/orgflow/crypto/HmacService.linuxX64.kt +++ b/modules/orgflow-crypto/src/linuxX64Main/kotlin/jp/orgflow/crypto/HmacService.linuxX64.kt @@ -13,10 +13,14 @@ private fun okioSha256(bytes: ByteArray): ByteArray { return sink.hash.toByteArray() } +private fun bytesToHex(bytes: ByteArray): String = + bytes.joinToString("") { (it.toInt() and 0xff).toString(16).padStart(2, '0') } + // Pure Kotlin/okio HMAC-SHA256 for non-JVM targets actual object HmacService { actual fun hmacSha256Hex(key: ByteArray, data: ByteArray): String { - val k = if (key.size > BLOCK_SIZE) okioSha256(key) + ByteArray(BLOCK_SIZE - 32) else key + ByteArray(BLOCK_SIZE - key.size) + val k = if (key.size > BLOCK_SIZE) okioSha256(key) + ByteArray(BLOCK_SIZE - 32) + else key + ByteArray(BLOCK_SIZE - key.size) val inner = ByteArray(BLOCK_SIZE + data.size) { i -> if (i < BLOCK_SIZE) (k[i].toInt() xor 0x36).toByte() else data[i - BLOCK_SIZE] } @@ -24,6 +28,6 @@ actual object HmacService { val outer = ByteArray(BLOCK_SIZE + innerHash.size) { i -> if (i < BLOCK_SIZE) (k[i].toInt() xor 0x5c).toByte() else innerHash[i - BLOCK_SIZE] } - return okioSha256(outer).joinToString("") { "%02x".format(it) } + return bytesToHex(okioSha256(outer)) } -} +}
\ No newline at end of file diff --git a/modules/orgflow-crypto/src/wasmJsMain/kotlin/jp/orgflow/crypto/HmacService.wasm.kt b/modules/orgflow-crypto/src/wasmJsMain/kotlin/jp/orgflow/crypto/HmacService.wasm.kt index fa0f0cd..c6b13ef 100644 --- a/modules/orgflow-crypto/src/wasmJsMain/kotlin/jp/orgflow/crypto/HmacService.wasm.kt +++ b/modules/orgflow-crypto/src/wasmJsMain/kotlin/jp/orgflow/crypto/HmacService.wasm.kt @@ -13,10 +13,14 @@ private fun okioSha256(bytes: ByteArray): ByteArray { return sink.hash.toByteArray() } +private fun bytesToHex(bytes: ByteArray): String = + bytes.joinToString("") { (it.toInt() and 0xff).toString(16).padStart(2, '0') } + // Pure Kotlin/okio HMAC-SHA256 for non-JVM targets actual object HmacService { actual fun hmacSha256Hex(key: ByteArray, data: ByteArray): String { - val k = if (key.size > BLOCK_SIZE) okioSha256(key) + ByteArray(BLOCK_SIZE - 32) else key + ByteArray(BLOCK_SIZE - key.size) + val k = if (key.size > BLOCK_SIZE) okioSha256(key) + ByteArray(BLOCK_SIZE - 32) + else key + ByteArray(BLOCK_SIZE - key.size) val inner = ByteArray(BLOCK_SIZE + data.size) { i -> if (i < BLOCK_SIZE) (k[i].toInt() xor 0x36).toByte() else data[i - BLOCK_SIZE] } @@ -24,6 +28,6 @@ actual object HmacService { val outer = ByteArray(BLOCK_SIZE + innerHash.size) { i -> if (i < BLOCK_SIZE) (k[i].toInt() xor 0x5c).toByte() else innerHash[i - BLOCK_SIZE] } - return okioSha256(outer).joinToString("") { "%02x".format(it) } + return bytesToHex(okioSha256(outer)) } -} +}
\ No newline at end of file |
