@@ -9,29 +9,28 @@ public class Crypt {
9
9
10
10
public static String encrypt (final String name , final String password ) {
11
11
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 ) {
12
16
try {
13
17
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 ));
16
19
} catch (final NoSuchAlgorithmException e ) {
17
- return null ;
20
+ return new byte [ 0 ]; // 返回一个空字节数组,避免返回null
18
21
}
19
22
}
20
23
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 ];
26
29
}
27
- return new String (chars );
30
+ return new String (hexChars );
28
31
}
29
32
30
33
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 ));
36
35
}
37
36
}
0 commit comments