Files
smartDriveEE/src/main/java/com/rj/common/PasswordUtil.java

201 lines
2.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.rj.common;
import org.springframework.util.DigestUtils;
/**
* 密码工具类
*
* @author 系统生成
* @since 2025-08-07
*/
public class PasswordUtil {
/**
* 默认盐值
*/
private static final String DEFAULT_SALT = "rj_system_2025";
/**
* MD5加密密码
*
* @param password 原始密码
* @return 加密后的密码
*/
public static String encryptPassword(String password) {
return DigestUtils.md5DigestAsHex(password.getBytes());
}
/**
* MD5加密密码带盐值
*
* @param password 原始密码
* @param salt 盐值
* @return 加密后的密码
*/
public static String encryptPassword(String password, String salt) {
String saltedPassword = password + salt;
return DigestUtils.md5DigestAsHex(saltedPassword.getBytes());
}
/**
* 使用默认盐值加密密码
*
* @param password 原始密码
* @return 加密后的密码
*/
public static String encryptPasswordWithDefaultSalt(String password) {
return encryptPassword(password, DEFAULT_SALT);
}
/**
* 验证密码
*
* @param inputPassword 输入的密码
* @param encryptedPassword 数据库中存储的加密密码
* @return 是否匹配
*/
public static boolean verifyPassword(String inputPassword, String encryptedPassword) {
String inputEncrypted = encryptPassword(inputPassword,DEFAULT_SALT);
return inputEncrypted.equals(encryptedPassword);
}
/**
* 验证密码(带盐值)
*
* @param inputPassword 输入的密码
* @param encryptedPassword 数据库中存储的加密密码
* @param salt 盐值
* @return 是否匹配
*/
public static boolean verifyPassword(String inputPassword, String encryptedPassword, String salt) {
String inputEncrypted = encryptPassword(inputPassword, DEFAULT_SALT);
return inputEncrypted.equals(encryptedPassword);
}
/**
* 使用默认盐值验证密码
*
* @param inputPassword 输入的密码
* @param encryptedPassword 数据库中存储的加密密码
* @return 是否匹配
*/
public static boolean verifyPasswordWithDefaultSalt(String inputPassword, String encryptedPassword) {
return verifyPassword(inputPassword, encryptedPassword, DEFAULT_SALT);
}
}