?Java?,??????????hash?:
- ??MessageDigest?:MessageDigest??????????,?MD5?SHA-1?SHA-256????????getInstance("???")????????MessageDigest??,????update()?????????????,????digest()???????????????:
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class HashGenerator { public static void main(String[] args) { String input = "Hello World"; try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] hashBytes = md.digest(input.getBytes()); // ????????????? StringBuilder sb = new StringBuilder(); for (byte b : hashBytes) { sb.append(String.format("x", b)); } String hash = sb.toString(); System.out.println("MD5 hash: " + hash); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } }
- ??HashCode??:Object???hashCode()????????????,????????????????????:
String input = "Hello World"; int hash = input.hashCode(); System.out.println("Hash code: " + hash);
??????,hashCode()??????int??????,??????????????,????Objects??hashCode()???