Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import java.lang.reflect.Field
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy
import java.math.BigInteger
import java.security.MessageDigest
import java.security.cert.CertificateFactory
import java.util.concurrent.locks.ReentrantLock
Expand Down Expand Up @@ -54,7 +53,7 @@ internal object HashUtils {
} while (numRead != -1)

// Convert byte array to hex string and return result.
return BigInteger(1, md.digest()).toString(16)
return AppEventUtility.bytesToHex(md.digest())
}
}

Expand Down Expand Up @@ -97,7 +96,7 @@ internal object HashUtils {
getTypeMethod.invoke(c) == TYPE_WHOLE_MD5) {
val getValueMethod: Method = c.javaClass.getMethod("getValue")
val checksumValue = getValueMethod.invoke(c) as ByteArray
resultChecksum = BigInteger(1, checksumValue).toString(16)
resultChecksum = AppEventUtility.bytesToHex(checksumValue)
lock.lock()
try {
checksumReady.signalAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ class HashUtilsTest : FacebookPowerMockTestCase() {
assertEquals(expectedChecksum, actualChecksum)
}

@Test
fun `test computeChecksum preserves leading zeros in digest`() {
// MD5("a") = 0cc175b9c0f1b6a831c399e269772661 (one leading zero nibble) and
// MD5("jk8ssl") = 0000000018e6137ac2caab16074784a6 (eight leading zero nibbles).
// A digest with leading zeros must still render as a full 32-character hex string.
val singleZero = File.createTempFile("tempFile.txt", null)
singleZero.writeText("a")
singleZero.deleteOnExit()
assertEquals("0cc175b9c0f1b6a831c399e269772661", HashUtils.computeChecksum(singleZero.path))

val manyZeros = File.createTempFile("tempFile.txt", null)
manyZeros.writeText("jk8ssl")
manyZeros.deleteOnExit()
assertEquals("0000000018e6137ac2caab16074784a6", HashUtils.computeChecksum(manyZeros.path))
}

@Test
fun `test computeChecksum with empty file`() {
val tempFile = File.createTempFile("tempFile.txt", null)
Expand Down