题
我需要在java中使用SHA1或MD5创建网页的html(从其URL)的哈希值,但我不知道该怎么做......你能帮助我吗?
解决方案
的Raffaele迪姆法齐奥:
可以使用该函数来生成MD5作为散列值从字符串;例如,
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 到 GET
这一页。
不隶属于 StackOverflow