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