【发布时间】:2010-11-17 07:58:47
【问题描述】:
我需要在 java 中使用 SHA1 或 MD5 创建网页 html 的哈希值(来自其 URL),但我不知道该怎么做...你能帮我吗?
【问题讨论】:
-
网址的哈希值?还是 HTML?
-
对不起,我忘记了...我需要 html 的哈希值。
我需要在 java 中使用 SHA1 或 MD5 创建网页 html 的哈希值(来自其 URL),但我不知道该怎么做...你能帮我吗?
【问题讨论】:
拉斐尔·迪法齐奥:
您可以使用此函数从字符串生成 MD5 作为 HashValue;例如,
String hashValue = MD5Hash("URL or HTML".getBytes());
/**
* MD5 implementation as Hash value
*
* @param a_sDataBytes - a original data as byte[] from String
* @return String as Hex value
* @throws NoSuchAlgorithmException
*/
public static String MD5Hash(byte[] dataBytes) throws NoSuchAlgorithmException {
if( dataBytes == null) return "";
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(dataBytes);
byte[] digest = md.digest();
// convert it to the hexadecimal
BigInteger bi = new BigInteger(digest);
String s = bi.toString(16);
if( s.length() %2 != 0)
{
s = "0"+s;
}
return s;
}
我希望它有所帮助。请让我们知道这个问题的方向是否正确。
老虎。
【讨论】:
DigestUtils.sha(String) 应该为 URI 或网页的 HTML 完成这项工作,但如果这是问题的一部分,则需要从其 URI 获取页面的 HTML。如果是这样,您可能需要考虑使用 Commons HttpClient to GET 页面。
【讨论】: