Question

I need to create the hash of a the html of a webpage (from its URL) using SHA1 or MD5 in java, but I don't know how to do it... can you help me?

Was it helpful?

Solution

Raffaele Di Fazio:

you can use this function to generate MD5 as HashValue from the String; for example,

   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;
    }

I hope it helps. Please, let us know if it is right direction for that question.

Tiger.

OTHER TIPS

DigestUtils.sha(String) should do the job for the URI or the HTML of the web page, though it's on you to get the HTML of the page from its URI if that's part of the problem. If so, you may want to look into using Commons HttpClient to GET the page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top