Java字符串、文件MD5工具类

Java字符串、文件MD5工具类,第1张

Java字符串、文件MD5工具

转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/120874209
本文出自【赵彦军的博客】

文章目录
  • Hex
  • MD5Util
  • 其他实现方式
    • 方式一
    • okio 实现方式

Hex

首先定义 Hex 工具类, 来源于 apache 开源工具类 https://commons.apache.org/proper/commons-codec/download_codec.cgi

public class Hex {

    
    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f'};

    
    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F'};

    public Hex() {
        // use default encoding
    }

    
    public static char[] encodeHex(final byte[] data) {
        return encodeHex(data, true);
    }

    
    public static char[] encodeHex(final byte[] data, final boolean toLowerCase) {
        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    
    protected static char[] encodeHex(final byte[] data, final char[] toDigits) {
        final int l = data.length;
        final char[] out = new char[l << 1];
        encodeHex(data, 0, data.length, toDigits, out, 0);
        return out;
    }

    
    private static void encodeHex(final byte[] data, final int dataOffset, final int dataLen, final char[] toDigits,
                                  final char[] out, final int outOffset) {
        // two characters form the hex value.
        for (int i = dataOffset, j = outOffset; i < dataOffset + dataLen; i++) {
            out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
            out[j++] = toDigits[0x0F & data[i]];
        }
    }
}

MD5Util
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException


object MD5Util {

    
    @Throws(NoSuchAlgorithmException::class, IOException::class)
    fun md5(file: File): String {
        FileInputStream(file).use { input ->
            return md5(input)
        }
    }

    
    @Throws(NoSuchAlgorithmException::class, IOException::class)
    fun md5(message: String): String {
        message.byteInputStream().use { input ->
            return md5(input)
        }
    }

    
    @Throws(NoSuchAlgorithmException::class, IOException::class)
    fun md5(byteArray: ByteArray): String {
        byteArray.inputStream().use { input ->
            return md5(input)
        }
    }

    
    @Throws(NoSuchAlgorithmException::class, IOException::class)
    private fun md5(input: InputStream): String {
        val messageDigest = MessageDigest.getInstance("MD5")
        val buffer = ByteArray(8192)
        var length: Int
        while (input.read(buffer).also { length = it } != -1) {
            messageDigest.update(buffer, 0, length)
        }
        return String(Hex.encodeHex(messageDigest.digest()))
    }
}

使用

//字符串md5
val message = "今天是周三"
val md5 = MD5Util.md5(message)
        
//文件md5
val file = File("")
val md5 = MD5Util.md5(file)
        
//字节数组md5
val byteArray = message.toByteArray()
val md5 = MD5Util.md5(byteArray)
其他实现方式 方式一
fun getFileMD5(file: File): String? {
    val bufferSize = 4 * 1024
    var fileInputStream: FileInputStream? = null
    var digestInputStream: DigestInputStream? = null
    try {
        var messageDigest = MessageDigest.getInstance("MD5")
        fileInputStream = FileInputStream(file)
        digestInputStream = DigestInputStream(fileInputStream, messageDigest)
        val buffer = ByteArray(bufferSize)
        while (digestInputStream.read(buffer) > 0) messageDigest =
            digestInputStream.messageDigest
        val bigInt = BigInteger(1, messageDigest.digest())
        return bigInt.toString(16)
    } catch (e: Exception) {
        return null
    } finally {
        try {
            digestInputStream?.close()
        } catch (e: Exception) {
        }
        try {
            fileInputStream?.close()
        } catch (e: Exception) {
        }
    }
}
okio 实现方式

http://blog.csdn.net/zhaoyanjun6/article/details/119997762

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4014173.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-22
下一篇 2022-10-22

发表评论

登录后才能评论

评论列表(0条)

保存