?Java?,MessageDigest
????????????????????,??????MessageDigest
????????????:
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { public static void main(String[] args) { byte[] inputBytes = "Hello, World!".getBytes(); String hashValue = https://www.yisu.com/ask/getHashValue(inputBytes,"SHA-256"); System.out.println("Hash value: " + hashValue); } public static String getHashValue(byte[] inputBytes, String algorithm) { try { MessageDigest messageDigest = MessageDigest.getInstance(algorithm); byte[] hashBytes = messageDigest.digest(inputBytes); return bytesToHex(hashBytes); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Error getting hash value", e); } } public static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("x", b)); } return sb.toString(); } }
??????,????????"Hello, World!"??????????,????getHashValue
????????????????????????????(?"SHA-256")??????????,????MessageDigest.getInstance()
??????MessageDigest
??,????digest()
???????????????,????????????????????
??:??????,???????????????????????????,?????SHA-256???