【fix sonar】PasswordUtil文件

This commit is contained in:
梁琦涛
2023-04-03 17:27:16 +08:00
parent a581579eab
commit 24d705b4f2
4 changed files with 75 additions and 12 deletions
@@ -1,13 +1,18 @@
package com.jero.common.util;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.SecureRandom;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.SecureRandom;
@Component
public class PasswordUtil {
private PasswordUtil(){
@@ -22,8 +27,32 @@ public class PasswordUtil {
/**
* 定义使用的算法为:PBEWITHMD5andDES算法
*/
public static final String ALGORITHM = "PBEWithMD5AndDES";//加密算法
public static final String SALT = "63293188";//密钥
//加密算法
public static String ALGORITHM;
@Value("${jero.password.pbe.algorithm}")
private String algorithmStr;
@PostConstruct
public void setAlgorithm(){
synchronized (PasswordUtil.class){
if(ALGORITHM == null){
ALGORITHM = this.algorithmStr;
}
}
}
public static String SALT;
//密钥
@Value("${jero.password.pbe.salt}")
private String saltStr;
@PostConstruct
public void setSalt(){
synchronized (PasswordUtil.class){
if(SALT == null){
SALT = this.saltStr;
}
}
}
/**
* 定义迭代次数为1000次
@@ -143,6 +172,17 @@ public class PasswordUtil {
}
return new String(passDec);
}
/**
* 自定义加密明文字符串
*
* @param ciphertext
* 待解密的密文字符串
* @return 解密后的明文字符串
* @throws Exception
*/
public static String decrypt(String ciphertext) {
return decrypt(ciphertext, ALGORITHM, SALT);
}
/**
* 将字节数组转换为十六进制字符串
@@ -193,5 +233,4 @@ public class PasswordUtil {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}