MD5 简介 #
- 一种哈希函数,输入任意长度数据,输出 128 位(16 字节)哈希值。
- 具有单向性,常用于数据完整性校验。
MD5 哈希值 #
- 为了方便表示,将每个字节转换为 2 位十六进制数,所以 MD5 哈希值通常表示为 32 个十六进制字符的字符串。
Java 生成 MD5 #
/*
* Copyright (C) 2012 The CyanogenMod Project
*
* * Licensed under the GNU GPLv2 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl-2.0.txt
*/
package com.cyanogenmod.updater.utils;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
private static final String TAG = "MD5";
public static boolean checkMD5(String md5, File updateFile) {
if (TextUtils.isEmpty(md5) || updateFile == null) {
Log.e(TAG, "MD5 string empty or updateFile null");
return false;
}
String calculatedDigest = calculateMD5(updateFile);
if (calculatedDigest == null) {
Log.e(TAG, "calculatedDigest null");
return false;
}
Log.v(TAG, "Calculated digest: " + calculatedDigest);
Log.v(TAG, "Provided digest: " + md5);
return calculatedDigest.equalsIgnoreCase(md5);
}
public static String calculateMD5(File updateFile) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "Exception while getting digest", e);
return null;
}
InputStream is;
try {
is = new FileInputStream(updateFile);
} catch (FileNotFoundException e) {
Log.e(TAG, "Exception while getting FileInputStream", e);
return null;
}
byte[] buffer = new byte[8192];
int read;
try {
while ((read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
byte[] md5sum = digest.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
String output = bigInt.toString(16);
// Fill to 32 chars
output = String.format("%32s", output).replace(' ', '0');
return output;
} catch (IOException e) {
throw new RuntimeException("Unable to process file for MD5", e);
} finally {
try {
is.close();
} catch (IOException e) {
Log.e(TAG, "Exception on closing MD5 input stream", e);
}
}
}
}
取自 android_packages_apps_CMUpdater
<code>bytesToHex</code> 方法总结 #
1. <code>String.format</code> 方法 #
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
- 优点: 简单易懂,无需额外依赖。
- 缺点: 性能相对较低,因为
String.format
在每次循环中都会创建新的字符串对象。
2. 字符数组法 #
private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray();
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
- 优点: 性能较高,通过预分配字符数组和位运算,避免了字符串创建的开销。
- 缺点: 代码稍微复杂。
3. <code>Hex</code> 类(Apache Commons Codec) #
import org.apache.commons.codec.binary.Hex;
private static String bytesToHex(byte[] bytes) {
return Hex.encodeHexString(bytes);
}
- 优点: 简洁高效,底层实现进行了优化。
- 缺点: 需要引入 Apache Commons Codec 依赖库。
4. <code>DatatypeConverter</code> 类(Java 8+) #
import javax.xml.bind.DatatypeConverter;
private static String bytesToHex(byte[] bytes) {
return DatatypeConverter.printHexBinary(bytes);
}
- 优点: 简单易用。
- 缺点: 在 Android API 26 中被标记为过时,不建议使用。
选择建议 #
- 追求性能且不介意引入依赖: 使用 Apache Commons Codec 的
Hex
类。 - 不想引入依赖或需要兼容旧版本 Android: 使用字符数组法。