2024-05-06 12:56:56 +08:00
|
|
|
package hae.utils.string;
|
2023-10-18 00:51:20 +08:00
|
|
|
|
2023-10-12 21:38:27 +08:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
|
|
|
|
|
public class HashCalculator {
|
2024-05-06 12:56:56 +08:00
|
|
|
public static String calculateHash(byte[] bytes){
|
|
|
|
|
MessageDigest digest;
|
|
|
|
|
try {
|
|
|
|
|
digest = MessageDigest.getInstance("MD5");
|
|
|
|
|
byte[] hashBytes = digest.digest(bytes);
|
|
|
|
|
return bytesToHex(hashBytes);
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2023-10-12 21:38:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String bytesToHex(byte[] bytes) {
|
|
|
|
|
StringBuilder hexString = new StringBuilder();
|
|
|
|
|
for (byte b : bytes) {
|
|
|
|
|
String hex = Integer.toHexString(0xff & b);
|
|
|
|
|
if (hex.length() == 1) {
|
|
|
|
|
hexString.append('0');
|
|
|
|
|
}
|
|
|
|
|
hexString.append(hex);
|
|
|
|
|
}
|
|
|
|
|
return hexString.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|