Files
HaE/src/main/java/hae/utils/string/HashCalculator.java
2024-05-12 19:02:38 +08:00

29 lines
796 B
Java

package hae.utils.string;
import java.security.MessageDigest;
public class HashCalculator {
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 "";
}
}
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();
}
}