【fix sonar】PasswordUtil文件

This commit is contained in:
梁琦涛
2023-03-03 16:58:52 +08:00
parent 81252d50eb
commit 86d1816a3e
@@ -9,6 +9,10 @@ import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
public class PasswordUtil {
private PasswordUtil(){
}
/**
* JAVA6支持以下任意一种算法 PBEWITHMD5ANDDES PBEWITHMD5ANDTRIPLEDES
* PBEWITHSHAANDDESEDE PBEWITHSHA1ANDRC2_40 PBKDF2WITHHMACSHA1
@@ -18,7 +22,7 @@ public class PasswordUtil {
* 定义使用的算法为:PBEWITHMD5andDES算法
*/
public static final String ALGORITHM = "PBEWithMD5AndDES";//加密算法
public static final String Salt = "63293188";//密钥
public static final String SALT = "63293188";//密钥
/**
* 定义迭代次数为1000次
@@ -27,10 +31,10 @@ public class PasswordUtil {
/**
* 获取加密算法中使用的盐值,解密中使用的盐值必须与加密中使用的相同才能完成操作. 盐长度必须为8字节
*
*
* @return byte[] 盐值
* */
public static byte[] getSalt() throws Exception {
public static byte[] getSalt() {
// 实例化安全随机数
SecureRandom random = new SecureRandom();
// 产出盐
@@ -39,12 +43,12 @@ public class PasswordUtil {
public static byte[] getStaticSalt() {
// 产出盐
return Salt.getBytes();
return SALT.getBytes();
}
/**
* 根据PBE密码生成一把密钥
*
*
* @param password
* 生成密钥时所使用的密码
* @return Key PBE算法密钥
@@ -69,7 +73,7 @@ public class PasswordUtil {
/**
* 加密明文字符串
*
*
* @param plaintext
* 待加密的明文字符串
* @param password
@@ -92,6 +96,7 @@ public class PasswordUtil {
encipheredData = cipher.doFinal(plaintext.getBytes("utf-8"));
//update-end-author:sccott date:20180815 for:中文作为用户名时,加密的密码windows和linux会得到不同的结果 gitee/issues/IZUD7
} catch (Exception e) {
e.printStackTrace();
}
return bytesToHexString(encipheredData);
}
@@ -105,13 +110,13 @@ public class PasswordUtil {
* @throws Exception
*/
public static String encrypt(String plaintext) {
return encrypt(plaintext, ALGORITHM, Salt);
return encrypt(plaintext, ALGORITHM, SALT);
}
/**
* 解密密文字符串
*
*
* @param ciphertext
* 待解密的密文字符串
* @param password
@@ -142,7 +147,7 @@ public class PasswordUtil {
/**
* 将字节数组转换为十六进制字符串
*
*
* @param src
* 字节数组
* @return
@@ -165,13 +170,13 @@ public class PasswordUtil {
/**
* 将十六进制字符串转换为字节数组
*
*
* @param hexString
* 十六进制字符串
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
if (hexString == null || "".equals(hexString)) {
return null;
}
hexString = hexString.toUpperCase();
@@ -180,7 +185,7 @@ public class PasswordUtil {
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]) & 0xff);
}
return d;
}
@@ -190,4 +195,4 @@ public class PasswordUtil {
}
}
}