Anyway the md5 example code below takes a session id (this is just a string which I wanted to encrypt - It could just as easily be a document, say a lump of xml or any old bit of text). This session id is pulled into the bytes array, defaultBytes and then the MessageDigest is instantiated as an instance of an md5 encryption.
Java developers who have come over from PERL or PHP often get frustrated with such a longwinded means of running what could simply be a single line of code, however the snippet below could be wrapped into an md5sum class which conducts the encryption and simply returns a string of cipher text.
include java.security.*;
... etc
sessionid="12345";
byte[] defaultBytes = sessionid.getBytes();
try{
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(defaultBytes);
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i=0;i<messageDigest.length;i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
}
String foo = messageDigest.toString();
System.out.println("sessionid "+sessionid+" md5 version is "+hexString.toString());
sessionid=hexString+"";
}catch(NoSuchAlgorithmException nsae){
}
Remember to import the java.security package. The original plain text and cipher text strings are echod to the java console just to demonstrate what has happened.
Hope that's useful,
christo
follow me on twitter: http://www.twitter.com/planet_guru