这篇文章介绍Java中如何使用SHA512加密。
我们主要用到了MessageDigest这个类,它位于java.security.MessageDigest。
下面我们直接上代码,
public static String encryptPasswordWithSHA512(String password) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512"); //创建SHA512类型的加密对象
messageDigest.update(password.getBytes());
byte[] bytes = messageDigest.digest();
StringBuffer strHexString = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xff & bytes[i]);
if (hex.length() == 1) {
strHexString.append('0');
}
strHexString.append(hex);
}
String result = strHexString.toString();
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
本文介绍如何在Java中使用SHA512算法进行密码加密,通过MessageDigest类实现,提供了一段示例代码,展示了从创建加密对象到转换为十六进制字符串的全过程。
635

被折叠的 条评论
为什么被折叠?



