From b6301d2dc90e3befa314880339d9daa5af20930f Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 4 Apr 2023 00:48:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E3=80=81=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E3=80=82=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/system/util/PasswordUtil.java | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 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..a924eff 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 @@ -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 ZhouXY */ -@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); } }