Skip to content

Commit 8fc034a

Browse files
committed
Fix hashing of empty inputs
1 parent 943ee32 commit 8fc034a

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/hashing/CrcNative.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ internal class Crc(val checksumFn: AwsChecksumsCrcFunction) : HashFunction {
2828
private var crc = 0U
2929

3030
override fun update(input: ByteArray, offset: Int, length: Int) {
31+
if (input.isEmpty() || length == 0) { return }
32+
3133
val offsetInput = input.usePinned {
3234
it.addressOf(offset)
3335
}

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/hashing/Md5Native.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class Md5 : HashFunction {
3131
private var md5 = checkNotNull(aws_md5_new(Allocator.Default)) { "aws_md5_new" }
3232

3333
override fun update(input: ByteArray, offset: Int, length: Int) {
34+
if (input.isEmpty() || length == 0) { return }
35+
3436
val inputCursor = input.usePinned {
3537
aws_byte_cursor_from_array(it.addressOf(offset), length.convert())
3638
}

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/hashing/ShaNative.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ internal class Sha(val initializeFn: InitializeHashFn) : HashFunction {
5555

5656
// aws_hash_update
5757
override fun update(input: ByteArray, offset: Int, length: Int) {
58+
if (input.isEmpty() || length == 0) { return }
59+
5860
val inputCursor = input.usePinned {
5961
aws_byte_cursor_from_array(it.addressOf(offset), length.convert())
6062
}

aws-crt-kotlin/native/test/aws/sdk/kotlin/crt/util/hashing/HashFunctionNativeTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,21 @@ class HashFunctionTest {
5050
assertEquals(expected, hash.digest().encodeToHex())
5151
}
5252
}
53+
54+
@Test
55+
fun testEmptyUpdate() {
56+
// algorithm -> hash("")
57+
val tests = listOf(
58+
(Sha1() to "da39a3ee5e6b4b0d3255bfef95601890afd80709"),
59+
(Sha256() to "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
60+
(Crc32() to "00000000"),
61+
(Crc32c() to "00000000"),
62+
)
63+
64+
tests.forEach { (hash, expected) ->
65+
val data = "".encodeToByteArray()
66+
hash.update(data, 0, 0)
67+
assertEquals(expected, hash.digest().encodeToHex())
68+
}
69+
}
5370
}

0 commit comments

Comments
 (0)