package com.estate.expert.admin.utils;
|
|
import java.nio.charset.StandardCharsets;
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
|
/**
|
* MD5加密工具类
|
*/
|
public class MD5Utils {
|
|
/**
|
* 对字符串进行MD5加密
|
* @param input 输入字符串
|
* @return MD5加密后的字符串
|
*/
|
public static String encrypt(String input) {
|
try {
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));
|
StringBuilder hexString = new StringBuilder();
|
for (byte hashByte : hashBytes) {
|
String hex = Integer.toHexString(0xff & hashByte);
|
if (hex.length() == 1) hexString.append('0');
|
hexString.append(hex);
|
}
|
return hexString.toString();
|
} catch (NoSuchAlgorithmException e) {
|
throw new RuntimeException("MD5 algorithm not found", e);
|
}
|
}
|
|
/**
|
* 验证密码是否匹配
|
* @param inputPassword 输入的密码
|
* @param storedHash 存储的MD5哈希值
|
* @return 是否匹配
|
*/
|
public static boolean verify(String inputPassword, String storedHash) {
|
String inputHash = encrypt(inputPassword);
|
return inputHash.equals(storedHash);
|
}
|
}
|