package com.baidu.speech.utils; import com.umeng.analytics.pro.cb; import java.io.File; import java.io.FileInputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import kotlin.UByte; public class MD5Util { public static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; public static MessageDigest messagedigest; static { try { messagedigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } private static void appendHexPair(byte b, StringBuffer stringBuffer) { char[] cArr = hexDigits; char c = cArr[(b & 240) >> 4]; char c2 = cArr[b & cb.m]; stringBuffer.append(c); stringBuffer.append(c2); } private static String bufferToHex(byte[] bArr) { return bufferToHex(bArr, 0, bArr.length); } private static String bufferToHex(byte[] bArr, int i, int i2) { StringBuffer stringBuffer = new StringBuffer(i2 * 2); int i3 = i2 + i; while (i < i3) { appendHexPair(bArr[i], stringBuffer); i++; } return stringBuffer.toString(); } public static String getFileMD5String(File file) { FileInputStream fileInputStream = new FileInputStream(file); byte[] bArr = new byte[1024]; while (true) { int read = fileInputStream.read(bArr); if (read > 0) { messagedigest.update(bArr, 0, read); } else { fileInputStream.close(); return bufferToHex(messagedigest.digest()); } } } public static String toHexString(byte[] bArr, String str, boolean z) { StringBuilder sb = new StringBuilder(); for (byte b : bArr) { String hexString = Integer.toHexString(b & UByte.MAX_VALUE); if (z) { hexString = hexString.toUpperCase(); } if (hexString.length() == 1) { sb.append("0"); } sb.append(hexString); sb.append(str); } return sb.toString(); } public static String toMd5(byte[] bArr, boolean z) { try { MessageDigest instance = MessageDigest.getInstance("MD5"); instance.reset(); instance.update(bArr); return toHexString(instance.digest(), "", z); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } }