本文介绍了使用MessageDigest的Java安全性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个Java项目,该项目需要实现Java哈希算法(例如MD5或SHA-256).根据Java API的当前实现为:
Hi, I''m developing a Java project that requires the implementation of Java Hashing algorithms such as MD5 or SHA-256. The current implementation according to the Java API is:
MessageDigest md = MessageDigest.getInstance("SHA");
try {
md.update(toChapter1);
MessageDigest tc1 = md.clone();
byte[] toChapter1Digest = tc1.digest();
md.update(toChapter2);
...etc.
} catch (CloneNotSupportedException cnse) {
throw new DigestException("couldn't make digest of partial content");
}
How do I implement the complete solution to store the hashed value in a MySQL database?
推荐答案
String password = "password";
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(password.getBytes());
byte data[] = md.digest();
//convert the byte to hex format
StringBuffer sb = new StringBuffer();
for (int i = 0; i < data.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Digest in Hex: " + sb.toString());
这篇关于使用MessageDigest的Java安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!