[英]How can I create the hash of a webpage in java?
我需要在Java中使用SHA1或MD5創建網頁html的哈希(從其URL),但是我不知道該怎么做...您能幫我嗎?
拉斐爾·迪·法齊奧(Raffaele Di Fazio):
您可以使用此函數從字符串生成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來GET
頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.