Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

MD5 checksum for an xml

794954Jan 24 2011 — edited Jan 27 2011
Hi,

I am new to java..

I am trying to calculate MD5 checksum for an xml input. Below is the code i am using
package Md5Package;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Checksum
{  
    public static void main (String[] args)
    {
        String md5val = "";
        MessageDigest algorithm = null;
        try
        {
            algorithm = MessageDigest.getInstance("MD5");
        }
        catch (NoSuchAlgorithmException nsae)
        {
            System.out.println("Cannot find digest algorithm");
            System.exit(1);
        }
        
        for (String arg : args)
        {
        	md5val = getHexString( algorithm,arg);
        
         }

    }
    
    public static String getHexString( MessageDigest algorithm,String arg){
        byte[] defaultBytes = arg.getBytes();
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();
        StringBuffer hexString = new StringBuffer();

        for (int i = 0; i < messageDigest.length; i++)
        {
            String hex = Integer.toHexString(0xFF & messageDigest);
if (hex.length() == 1)
{
hexString.append('0');
}
hexString.append(hex);
}
System.out.println("md5val");
return(hexString.toString());
}
}


It actually calculates the MD5 for a string. But i need it for an xml type input. Any suggestions on this? I am new to java sorry if its a basic question. Appreciate your suggestions. Thanks

Edited by: sabre150 on 24-Jan-2011 09:00

Moderator action : added [ code] tags to improve code readability.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 24 2011
Added on Jan 24 2011
7 comments
2,219 views