package com.ys.bdtp.adm.app.tools

import java.io.File
import java.io.InputStream
import java.security.MessageDigest

/**
 * MD5
 */
fun md5(byteArray: ByteArray): String {
    val digest = MessageDigest.getInstance("md5")
    val bytes = digest.digest(byteArray)
    var md5 = ""
    for (b in bytes) {
        val hex = Integer.toHexString(0xFF and b.toInt())
        if (hex.length == 1) {
            md5 += "0"
        }
        md5 += hex
    }
    return md5
}

/**
 * 文件 md5
 */
fun fileMD5(file: File): String {
    return md5(file.toBytes())
}

/**
 * 字符串 md5
 */
fun String.md5(): String {
    return md5(this.toByteArray())
}

fun streamMD5(stream: InputStream): String {
    return md5(stream.readBytes())
}



