Skip to content

Commit 8629920

Browse files
authored
fix(rt): Crc32C incorrect results on negative inputs longer than 7 bytes (#768)
1 parent 978cbd0 commit 8629920

File tree

2 files changed

+14
-9
lines changed
  • runtime/hashing/common

2 files changed

+14
-9
lines changed

runtime/hashing/common/src/aws/smithy/kotlin/runtime/hashing/Crc32c.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package aws.smithy.kotlin.runtime.hashing
66

77
import aws.smithy.kotlin.runtime.util.InternalApi
8-
import kotlin.experimental.and
98

109
/**
1110
* Compute the CRC32C hash of the current [ByteArray]
@@ -82,10 +81,10 @@ private class CRC32CImpl {
8281
val c3 = (b[off + 3].toInt() xor localCrc) and 0xff
8382
localCrc = (T[T8_7_start + c0] xor T[T8_6_start + c1] xor T[T8_5_start + c2] xor T[T8_4_start + c3]).toInt()
8483

85-
val c4 = b[off + 4] and 0xff.toByte()
86-
val c5 = b[off + 5] and 0xff.toByte()
87-
val c6 = b[off + 6] and 0xff.toByte()
88-
val c7 = b[off + 7] and 0xff.toByte()
84+
val c4 = b[off + 4].toInt() and 0xff
85+
val c5 = b[off + 5].toInt() and 0xff
86+
val c6 = b[off + 6].toInt() and 0xff
87+
val c7 = b[off + 7].toInt() and 0xff
8988

9089
localCrc = localCrc xor (T[T8_3_start + c4] xor T[T8_2_start + c5] xor T[T8_1_start + c6] xor T[T8_0_start + c7]).toInt()
9190

@@ -103,10 +102,6 @@ private class CRC32CImpl {
103102

104103
fun getValue(): Int = crc.inv()
105104

106-
fun update(b: Int) {
107-
crc = crc ushr 8 xor T[(T8_0_start + ((crc xor b) and 0xff))].toInt()
108-
}
109-
110105
companion object {
111106
private const val T8_0_start: Int = 0 * 256
112107

runtime/hashing/common/test/aws/smithy/kotlin/runtime/hashing/Crc32cTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,14 @@ class Crc32cTest {
8080
val bytes = crc.digest()
8181
assertEquals("7q7efA==", bytes.encodeBase64String())
8282
}
83+
84+
@Test
85+
fun testNegativeByteInput() {
86+
val crc = Crc32c()
87+
val input = ByteArray(1024) { -1 }
88+
crc.update(input)
89+
90+
val bytes = crc.digest()
91+
assertEquals("R+XN5A==", bytes.encodeBase64String())
92+
}
8393
}

0 commit comments

Comments
 (0)