/**
* File name: genmd5.java
* Environment: GNU/Linux Ubuntu 7.04 + eclipse 3.2 + JDK 1.6
* Function: generate an MD5 sequence from a string.
* Version: 0.0.1.0
* Authors: 88250
* Date: 2007.5.10
* E-mail & MDN: DL88250@gmail.com
* QQ: 845765
*/
Import java. Security. messagedigest;
Public class genmd5
{
Public static void main (string [] ARGs)
{
System. Out. println (compute ("test! "));
}
/**
* Computes the MD5 fingerprint of a string.
*
* @ Return the MD5 Digest of the input <code> string </code>
*/
Public static string compute (string instr)
{
// Convert input string to a char []
// Convert that char [] to byte []
// Get the MD5 Digest as byte []
// Bit-wise and that byte [] with 0xff
// Divpend "0" to the output stringbuffer to make sure that we don't end
// Up
// Something like "e21ff" instead of "e201ff"
Messagedigest MD5 = NULL;
Try
{
MD5 = messagedigest. getinstance ("MD5 ");
}
Catch (exception E)
{
System. Out. println (E. tostring ());
E. printstacktrace ();
Return "";
}
Char [] chararray = instr. tochararray ();
Byte [] bytearray = new byte [chararray. Length];
For (INT I = 0; I <chararray. length; I ++)
{
Bytearray [I] = (byte) chararray [I];
}
Byte [] md5bytes = md5.digest (bytearray );
Stringbuffer hexvalue = new stringbuffer ();
For (INT I = 0; I <md5bytes. length; I ++)
{
Int val = (INT) md5bytes [I]) & 0xff;
If (Val <16)
{
Hexvalue. append ("0 ");
}
Hexvalue. append (integer. tohexstring (VAL ));
}
Return hexvalue. tostring ();
}
}