mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
MD5 checksumer shouldn't read the whole file at once
This commit is contained in:
parent
49dac2a6b3
commit
37417763d2
1 changed files with 17 additions and 11 deletions
|
@ -7,17 +7,23 @@ import java.security.MessageDigest
|
||||||
|
|
||||||
public class Md5 {
|
public class Md5 {
|
||||||
companion object {
|
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 {
|
fun toMd5(bytes: ByteArray): String =
|
||||||
val result = StringBuilder()
|
MessageDigest.getInstance("MD5").digest(bytes).toHex()
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun ByteArray.toHex() = buildString {
|
||||||
|
forEach {
|
||||||
|
val byte = it.toInt() and 0xff
|
||||||
|
if (byte < 16) append("0")
|
||||||
|
append(Integer.toHexString(byte))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue