From 37417763d2229662021154d37cbcbcc3a4f72d70 Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Thu, 12 Nov 2015 22:12:08 +0300 Subject: [PATCH] MD5 checksumer shouldn't read the whole file at once --- src/main/kotlin/com/beust/kobalt/maven/Md5.kt | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/maven/Md5.kt b/src/main/kotlin/com/beust/kobalt/maven/Md5.kt index 1bec0939..e6c5eea4 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/Md5.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/Md5.kt @@ -7,17 +7,23 @@ import java.security.MessageDigest public class Md5 { companion object { - fun toMd5(file: File) = toMd5(Files.readAllBytes(Paths.get(file.toURI()))) + fun toMd5(file: File) = + MessageDigest.getInstance("MD5").let { md5 -> + file.forEachBlock { bytes, size -> + md5.update(bytes, 0, size) + } + md5.digest().toHex() + } - fun toMd5(bytes: ByteArray): String { - val result = StringBuilder() - val md5 = MessageDigest.getInstance("MD5").digest(bytes) - md5.forEach { - val byte = it.toInt() and 0xff - if (byte < 16) result.append("0") - result.append(Integer.toHexString(byte)) - } - return result.toString() - } + fun toMd5(bytes: ByteArray): String = + MessageDigest.getInstance("MD5").digest(bytes).toHex() } } + +private fun ByteArray.toHex() = buildString { + forEach { + val byte = it.toInt() and 0xff + if (byte < 16) append("0") + append(Integer.toHexString(byte)) + } +} \ No newline at end of file