try {
MessageDigest digest = MessageDigest.getInstance("SHA-512");
byte[] output = digest.digest(password);
digest.update(salt);
digest.update(output);
return new BigInteger(1, digest.digest());
} catch (NoSuchAlgorithmException e) {
throw new UnsupportedOperationException(e);
}
Run Code Online (Sandbox Code Playgroud)
但我得到了Exception in thread "main" java.security.NoSuchAlgorithmException: SHA_512 MessageDigest not available错误
以下是可用于通过SHA-512获取哈希字符串的示例方法:
private static String getHashCodeFromString(String algorithm, String str) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(str.getBytes());
byte byteData[] = md.digest();
//convert the byte to hex format method 1
StringBuffer hashCodeBuffer = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
hashCodeBuffer.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
return hashCodeBuffer.toString();
}
Run Code Online (Sandbox Code Playgroud)
使用SHA-512的算法.转到以下链接,了解您可以通过方法传递的其他可能的算法名称.https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest
| 归档时间: |
|
| 查看次数: |
21149 次 |
| 最近记录: |