计算密码哈希
使用不同的算法计算相对较小的数据块的哈希值:
final MessageDigest md5 = MessageDigest.getInstance("MD5");
final MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
final MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
final byte[] data = "FOO BAR".getBytes();
System.out.println("MD5 hash: " + DatatypeConverter.printHexBinary(md5.digest(data)));
System.out.println("SHA1 hash: " + DatatypeConverter.printHexBinary(sha1.digest(data)));
System.out.println("SHA256 hash: " + DatatypeConverter.printHexBinary(sha256.digest(data)));
生成此输出:
MD5 hash: E99E768582F6DD5A3BA2D9C849DF736E
SHA1 hash: 0135FAA6323685BA8A8FF8D3F955F0C36949D8FB
SHA256 hash: 8D35C97BCD902B96D1B551741BBE8A7F50BB5A690B4D0225482EAA63DBFB9DED
根据你对 Java 平台的实现,可能还有其他算法可用。