Skip to content

Commit

Permalink
feat: 优化加密方法及异常处理
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed Jul 14, 2024
1 parent 2d25517 commit ee39685
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/main/java/cc/baka9/catseedlogin/util/CommunicationAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,23 @@

public class CommunicationAuth {

private static MessageDigest messageDigest;
private static final MessageDigest messageDigest;

static {
try {
messageDigest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
// 适当地处理异常
e.printStackTrace();
throw new RuntimeException("SHA-256 algorithm not found", e);
}
}

public static String encryption(String... args) {
String paramString = String.join("", args);
byte[] arrayOfByte = messageDigest.digest(paramString.getBytes());
StringBuilder stringBuilder = new StringBuilder();
StringBuilder stringBuilder = new StringBuilder(arrayOfByte.length * 2);
for (byte value : arrayOfByte) {
int unsignedByte = value & 0xff;
if (unsignedByte < 16) stringBuilder.append("0");
stringBuilder.append(Integer.toHexString(unsignedByte));
stringBuilder.append(String.format("%02x", value & 0xff));
}
return stringBuilder.toString().toLowerCase();
return stringBuilder.toString();
}

}
}

0 comments on commit ee39685

Please sign in to comment.