diff options
| author | ketsuban <ketsuban@192.168.3.110> | 2026-08-26 15:03:46 +0000 |
|---|---|---|
| committer | ketsuban <ketsuban@192.168.3.110> | 2026-08-26 15:03:46 +0000 |
| commit | cece2bcd155262ec2723093992d6c2d95a14cf52 (patch) | |
| tree | 599ddff2fd3ff76f856dc5d1453f9c066bdaae5d | |
| parent | 87a769291e96ff8ffe91aebccbc99d3ae81ee17f (diff) | |
| download | kukuri-cece2bcd155262ec2723093992d6c2d95a14cf52.tar.gz kukuri-cece2bcd155262ec2723093992d6c2d95a14cf52.tar.bz2 kukuri-cece2bcd155262ec2723093992d6c2d95a14cf52.zip | |
Fix non-JVM HmacService hex formatting (String.format is JVM-only)
| -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 |
