登录密码加密,反向解密
This commit is contained in:
88
src/main/java/com/rj/common/PasswordUtil.java
Normal file
88
src/main/java/com/rj/common/PasswordUtil.java
Normal file
@@ -0,0 +1,88 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user