HanteIsHante / file

file note
4 stars 0 forks source link

获取Apk MD5 值,出现首位是0 时丢失现象,造成只有31位字符 #55

Open HanteIsHante opened 6 years ago

HanteIsHante commented 6 years ago

获取MD5:


    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值,如果首位是0,则会发生省略现象

例:0724b7ec028c33f072ed2e6b3fe1a41a, 获取到的是 724b7ec028c33f072ed2e6b3fe1a41a