我需要在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;
}
希望对您有所帮助。请让我们知道该问题的正确方向。
虎。