【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; import javax.crypto.spec.PBEParameterSpec;
public class PasswordUtil { public class PasswordUtil {
private PasswordUtil(){
}
/** /**
* JAVA6支持以下任意一种算法 PBEWITHMD5ANDDES PBEWITHMD5ANDTRIPLEDES * JAVA6支持以下任意一种算法 PBEWITHMD5ANDDES PBEWITHMD5ANDTRIPLEDES
* PBEWITHSHAANDDESEDE PBEWITHSHA1ANDRC2_40 PBKDF2WITHHMACSHA1 * PBEWITHSHAANDDESEDE PBEWITHSHA1ANDRC2_40 PBKDF2WITHHMACSHA1
@@ -18,7 +22,7 @@ public class PasswordUtil {
* 定义使用的算法为:PBEWITHMD5andDES算法 * 定义使用的算法为:PBEWITHMD5andDES算法
*/ */
public static final String ALGORITHM = "PBEWithMD5AndDES";//加密算法 public static final String ALGORITHM = "PBEWithMD5AndDES";//加密算法
public static final String Salt = "63293188";//密钥 public static final String SALT = "63293188";//密钥
/** /**
* 定义迭代次数为1000次 * 定义迭代次数为1000次
@@ -30,7 +34,7 @@ public class PasswordUtil {
* *
* @return byte[] 盐值 * @return byte[] 盐值
* */ * */
public static byte[] getSalt() throws Exception { public static byte[] getSalt() {
// 实例化安全随机数 // 实例化安全随机数
SecureRandom random = new SecureRandom(); SecureRandom random = new SecureRandom();
// 产出盐 // 产出盐
@@ -39,7 +43,7 @@ public class PasswordUtil {
public static byte[] getStaticSalt() { public static byte[] getStaticSalt() {
// 产出盐 // 产出盐
return Salt.getBytes(); return SALT.getBytes();
} }
/** /**
@@ -92,6 +96,7 @@ public class PasswordUtil {
encipheredData = cipher.doFinal(plaintext.getBytes("utf-8")); encipheredData = cipher.doFinal(plaintext.getBytes("utf-8"));
//update-end-author:sccott date:20180815 for:中文作为用户名时,加密的密码windows和linux会得到不同的结果 gitee/issues/IZUD7 //update-end-author:sccott date:20180815 for:中文作为用户名时,加密的密码windows和linux会得到不同的结果 gitee/issues/IZUD7
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
} }
return bytesToHexString(encipheredData); return bytesToHexString(encipheredData);
} }
@@ -105,7 +110,7 @@ public class PasswordUtil {
* @throws Exception * @throws Exception
*/ */
public static String encrypt(String plaintext) { public static String encrypt(String plaintext) {
return encrypt(plaintext, ALGORITHM, Salt); return encrypt(plaintext, ALGORITHM, SALT);
} }
@@ -171,7 +176,7 @@ public class PasswordUtil {
* @return * @return
*/ */
public static byte[] hexStringToBytes(String hexString) { public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) { if (hexString == null || "".equals(hexString)) {
return null; return null;
} }
hexString = hexString.toUpperCase(); hexString = hexString.toUpperCase();
@@ -180,7 +185,7 @@ public class PasswordUtil {
byte[] d = new byte[length]; byte[] d = new byte[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
int pos = i * 2; 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; return d;
} }