package com.zxhy.hfilemanagermaster.knife

import java.io.File
import java.nio.channels.FileChannel
import java.security.MessageDigest

//生成、查看文件的MD5、SHA1、SHA2、SHA3值
//https://blog.csdn.net/COCO56/article/details/106161207

//根据加密类型获取具体的值
fun File.digestMd5(): ByteArray {
    val byteBuffer = inputStream().channel.map(FileChannel.MapMode.READ_ONLY, 0, length())
    val digest = MessageDigest.getInstance("MD5")
    digest.update(byteBuffer)
    return digest.digest()
}

private const val HEXES = "0123456789ABCDEF"

/**
 * ByteArray转Hex码
 */
fun ByteArray.byteArrayToHexString(): String {
    val hex = StringBuilder(2 * this.size)
    for (element in this) {
        val high = element.toInt().and(0xFF).shr(4)
        val low = element.toInt().and(0x0F)
        hex.append(HEXES[high]).append(HEXES[low])
    }
    return hex.toString()
}


