mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-10-05 19:28:25 +00:00
fix(quartz): hash empty input on Apple without crashing
sha256() and sha256Into() in Sha256.apple.kt pinned the input and took addressOf(0), which throws ArrayIndexOutOfBoundsException on Kotlin/Native when the array is empty. So sha256(ByteArray(0)) crashed on iOS/macOS, and test-quartz-ios failed 7 OversizedTransferTest cases that hash "" (red on main too). CommonCrypto's CC_SHA256 accepts a null data pointer for a zero-length input, so pass null when the array is empty. Adds Sha256Test with FIPS 180-2 vectors (empty, "abc", prefix hashing through sha256Into) so every platform's actual is checked, not only through CEP-22. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015VvJ8XpeYqBe8bYT3bJ7UD
This commit is contained in:
@@ -34,7 +34,8 @@ actual fun sha256(data: ByteArray): ByteArray {
|
||||
data.usePinned { inputPinned ->
|
||||
digest.usePinned { digestPinned ->
|
||||
CC_SHA256(
|
||||
inputPinned.addressOf(0),
|
||||
// addressOf(0) throws on an empty array; CommonCrypto accepts null for a 0-byte input.
|
||||
if (data.isEmpty()) null else inputPinned.addressOf(0),
|
||||
data.size.convert(),
|
||||
digestPinned.addressOf(0),
|
||||
)
|
||||
@@ -53,7 +54,8 @@ actual fun sha256Into(
|
||||
data.usePinned { inputPinned ->
|
||||
out.asUByteArray().usePinned { digestPinned ->
|
||||
CC_SHA256(
|
||||
inputPinned.addressOf(0),
|
||||
// addressOf(0) throws on an empty array; CommonCrypto accepts null for a 0-byte input.
|
||||
if (data.isEmpty()) null else inputPinned.addressOf(0),
|
||||
len.convert(),
|
||||
digestPinned.addressOf(0),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.utils.sha256
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
/**
|
||||
* Known-answer vectors (FIPS 180-2) for every platform's sha256 actual. The empty
|
||||
* input case guards the Apple actual, which used to pin `addressOf(0)` on a
|
||||
* zero-length array and throw ArrayIndexOutOfBoundsException.
|
||||
*/
|
||||
class Sha256Test {
|
||||
private val emptyDigest = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
||||
private val abcDigest = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
||||
|
||||
private fun ByteArray.hex() = joinToString("") { (it.toInt() and 0xFF).toString(16).padStart(2, '0') }
|
||||
|
||||
@Test
|
||||
fun hashesEmptyInput() {
|
||||
assertEquals(emptyDigest, sha256(ByteArray(0)).hex())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun hashesAbc() {
|
||||
assertEquals(abcDigest, sha256("abc".encodeToByteArray()).hex())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sha256IntoHashesEmptyInput() {
|
||||
assertEquals(emptyDigest, sha256Into(ByteArray(32), ByteArray(0)).hex())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sha256IntoHashesOnlyThePrefix() {
|
||||
val out = ByteArray(32)
|
||||
assertEquals(abcDigest, sha256Into(out, "abcdef".encodeToByteArray(), 3).hex())
|
||||
assertEquals(emptyDigest, sha256Into(out, "abcdef".encodeToByteArray(), 0).hex())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user