?Java?,????String
??hashCode()
???????????????????????(int),??????????????????????:
public class StringToHash { public static void main(String[] args) { String input = "Hello, world!"; int hash = input.hashCode(); System.out.println("Hash code of the string '" + input + "' is: " + hash); } }
??????,????????,??????????????????????,???????????(?????????????),??????,Java?hashCode()
????????????????????
?????????????????,??????Java???MessageDigest
?,???SHA-256?SHA-512???????????????MessageDigest
???????????:
import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class StringToHash { public static void main(String[] args) { String input = "Hello, world!"; String hash = calculateHash(input, "SHA-256"); System.out.println("Hash code of the string '" + input + "' using SHA-256 is: " + hash); } private static String calculateHash(String input, String algorithm) { try { MessageDigest messageDigest = MessageDigest.getInstance(algorithm); byte[] hashBytes = messageDigest.digest(input.getBytes(StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); for (byte b : hashBytes) { sb.append(String.format("x", b)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Failed to calculate hash", e); } } }
??????,?????SHA-256????????????????????calculateHash
?????????????????,??"SHA-512"?