fun getFileMd5(file: File): String? {
var value: String? = null
var fileInputStream: FileInputStream? = null
try {
fileInputStream = FileInputStream(file)
val byteBuffer = fileInputStream.channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length())
val md5 = MessageDigest.getInstance("MD5")
md5.update(byteBuffer)
val bi = BigInteger(1, md5.digest())
value = bi.toString(16)
} catch (ignore: Exception) {
} finally {
if (null != fileInputStream) {
try {
fileInputStream.close()
} catch (ignore: IOException) {
}
}
}
return value
}
获取MD5:
上面方法产生的MD5值,如果首位是0,则会发生省略现象