简化、重构代码。修复错误。
parent
4e4debaec9
commit
fcdf69026f
|
@ -7,9 +7,7 @@ import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import xyz.zhouxy.plusone.exception.SysException;
|
||||||
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
|
|
||||||
import xyz.zhouxy.plusone.exception.BizException;
|
|
||||||
import xyz.zhouxy.plusone.util.RandomUtil;
|
import xyz.zhouxy.plusone.util.RandomUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,7 +15,6 @@ import xyz.zhouxy.plusone.util.RandomUtil;
|
||||||
*
|
*
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
public final class PasswordUtil {
|
public final class PasswordUtil {
|
||||||
private static final char[] SALT_BASE_CHAR_ARRAY = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_-+={}[]|\\:;\"',.<>?/"
|
private static final char[] SALT_BASE_CHAR_ARRAY = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_-+={}[]|\\:;\"',.<>?/"
|
||||||
.toCharArray();
|
.toCharArray();
|
||||||
|
@ -35,12 +32,12 @@ public final class PasswordUtil {
|
||||||
int i = length > 0 ? length / 2 : 0;
|
int i = length > 0 ? length / 2 : 0;
|
||||||
var passwordWithSalt = salt.substring(0, i)
|
var passwordWithSalt = salt.substring(0, i)
|
||||||
+ password
|
+ password
|
||||||
+ salt.substring(1);
|
+ salt.substring(i);
|
||||||
String sha512Hex = sha512Hex(passwordWithSalt);
|
try {
|
||||||
if (sha512Hex == null) {
|
return sha512Hex(passwordWithSalt);
|
||||||
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:哈希加密失败!");
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new SysException(e);
|
||||||
}
|
}
|
||||||
return sha512Hex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,15 +54,12 @@ public final class PasswordUtil {
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String sha512Hex(String data) {
|
@Nonnull
|
||||||
try {
|
@SuppressWarnings("null")
|
||||||
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
|
private static String sha512Hex(String data) throws NoSuchAlgorithmException {
|
||||||
messageDigest.update(data.getBytes(StandardCharsets.UTF_8));
|
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
|
||||||
byte[] result = messageDigest.digest();
|
messageDigest.update(data.getBytes(StandardCharsets.UTF_8));
|
||||||
return new BigInteger(1, result).toString(16);
|
byte[] result = messageDigest.digest();
|
||||||
} catch (NoSuchAlgorithmException e) {
|
return new BigInteger(1, result).toString(16);
|
||||||
log.error("{}", e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue