Skip to content

Commit 540e593

Browse files
committed
优化Crypt类加密逻辑及异常处理
1 parent c62ebec commit 540e593

File tree

1 file changed

+13
-14
lines changed
  • src/main/java/cc/baka9/catseedlogin/util

1 file changed

+13
-14
lines changed

src/main/java/cc/baka9/catseedlogin/util/Crypt.java

+13-14
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,28 @@ public class Crypt {
99

1010
public static String encrypt(final String name, final String password) {
1111
String text = "ÜÄaeut//&/=I " + password + "7421€547" + name + "__+IÄIH§%NK " + password;
12+
return byteArrayToHexString(hash(text));
13+
}
14+
15+
private static byte[] hash(String text) {
1216
try {
1317
final MessageDigest md = MessageDigest.getInstance("SHA-512");
14-
md.update(text.getBytes(StandardCharsets.UTF_8), 0, text.length());
15-
return byteArrayToHexString(md.digest());
18+
return md.digest(text.getBytes(StandardCharsets.UTF_8));
1619
} catch (final NoSuchAlgorithmException e) {
17-
return null;
20+
return new byte[0]; // 返回一个空字节数组,避免返回null
1821
}
1922
}
2023

21-
public static String byteArrayToHexString(byte[] args) {
22-
char[] chars = new char[args.length * 2];
23-
for (int i = 0; i < args.length; i++) {
24-
chars[(i * 2)] = CRYPTCHARS[(args[i] >> 4 & 0xF)];
25-
chars[(i * 2 + 1)] = CRYPTCHARS[(args[i] & 0xF)];
24+
public static String byteArrayToHexString(byte[] bytes) {
25+
char[] hexChars = new char[bytes.length * 2];
26+
for (int i = 0; i < bytes.length; i++) {
27+
hexChars[i * 2] = CRYPTCHARS[(bytes[i] >> 4) & 0xF];
28+
hexChars[i * 2 + 1] = CRYPTCHARS[bytes[i] & 0xF];
2629
}
27-
return new String(chars);
30+
return new String(hexChars);
2831
}
2932

3033
public boolean match(final String name, final String password, final String encrypted) {
31-
try {
32-
return encrypted.equals(encrypt(name, password));
33-
} catch (final Exception e) {
34-
return false;
35-
}
34+
return encrypted.equals(encrypt(name, password));
3635
}
3736
}

0 commit comments

Comments
 (0)