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.