???Java?DigestUtils????,????????:
-
?????????:DigestUtils????????,?MD5?SHA-1?SHA-256???????????????????????,SHA-256??????MD5?SHA-1?
-
????:??????????????,???????????,?????????,??????????????????????????
-
?????:???????????????,?????????,????????????????????????????????,???????
-
??Java???:Java 8???java.security.MessageDigest?,??????Java??????????????????????,??????????
????:
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.nio.charset.StandardCharsets; import java.util.Arrays; public class DigestUtils { public static void main(String[] args) throws NoSuchAlgorithmException { String data = "https://www.yisu.com/ask/Hello, world!"; String md5Hash = calculateHash(data, "MD5"); String sha256Hash = calculateHash(data, "SHA-256"); System.out.println("MD5: " + md5Hash); System.out.println("SHA-256: " + sha256Hash); } public static String calculateHash(String data, String algorithm) throws NoSuchAlgorithmException { MessageDigest messageDigest = MessageDigest.getInstance(algorithm); byte[] hashBytes = messageDigest.digest(data.getBytes(StandardCharsets.UTF_8)); return bytesToHex(hashBytes); } private static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("x", b)); } return sb.toString(); } }
??:??????????????,?????????????????????,????????????????