68 lines
1.7 KiB
Java
68 lines
1.7 KiB
Java
package com.sgcc.utils;
|
|
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
/**
|
|
* 用户登录相关工具类
|
|
*
|
|
* @author zhaoyuanyuan
|
|
* @version 1.0.0
|
|
* @date 2024/06/26 17:24
|
|
*/
|
|
public class MD5Utils {
|
|
|
|
/**
|
|
* MD5转字符串
|
|
*
|
|
* @param bytes
|
|
* @return {@code String }
|
|
* @author zhaoyuanyuan
|
|
* @date 2024/06/26 17:25
|
|
*/
|
|
public static String toHexString(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();
|
|
}
|
|
|
|
/**
|
|
* 密码加密
|
|
*
|
|
* @param password 密码
|
|
* @return {@code String }
|
|
* @author zhaoyuanyuan
|
|
* @date 2024/06/26 17:25
|
|
*/
|
|
public static String encryptPassword(String password) {
|
|
try {
|
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
md.update(password.getBytes());
|
|
byte[] digest = md.digest();
|
|
return toHexString(digest);
|
|
} catch (NoSuchAlgorithmException e) {
|
|
throw new RuntimeException("MD5 algorithm not found", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 密码验证
|
|
*
|
|
* @param inputPassword 表单输入密码
|
|
* @param storedPassword 数据库存储加密密码
|
|
* @return boolean
|
|
* @author zhaoyuanyuan
|
|
* @date 2024/06/26 17:27
|
|
*/
|
|
public static boolean validatePassword(String inputPassword, String storedPassword) {
|
|
return encryptPassword(inputPassword).equals(storedPassword);
|
|
}
|
|
|
|
}
|