From 89584d2a48e711922ddb2eb69a61e472aabc5429 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 15 Apr 2023 13:37:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/system/util/PasswordUtil.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/plusone-system/plusone-system-common/src/main/java/xyz/zhouxy/plusone/system/util/PasswordUtil.java b/plusone-system/plusone-system-common/src/main/java/xyz/zhouxy/plusone/system/util/PasswordUtil.java index 015a5f6..bf34ac9 100644 --- a/plusone-system/plusone-system-common/src/main/java/xyz/zhouxy/plusone/system/util/PasswordUtil.java +++ b/plusone-system/plusone-system-common/src/main/java/xyz/zhouxy/plusone/system/util/PasswordUtil.java @@ -4,10 +4,10 @@ import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Objects; 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.util.RandomUtil; @@ -17,7 +17,6 @@ import xyz.zhouxy.plusone.util.RandomUtil; * * @author ZhouXY */ -@Slf4j public final class PasswordUtil { private static final char[] SALT_BASE_CHAR_ARRAY = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_-+={}[]|\\:;\"',.<>?/" .toCharArray(); @@ -36,11 +35,12 @@ public final class PasswordUtil { var passwordWithSalt = salt.substring(0, i) + password + salt.substring(1); - String sha512Hex = sha512Hex(passwordWithSalt); - if (sha512Hex == null) { - throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:哈希加密失败!"); + + try { + return sha512Hex(passwordWithSalt); + } catch (Exception e) { + throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "哈希加密失败!", e); } - return sha512Hex; } /** @@ -57,15 +57,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 + 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(); + var sha512Hex = new BigInteger(1, result).toString(16); + return Objects.requireNonNull(sha512Hex); } }