MD5 Hash of a String

Recently I search the forums for a way to get a MD5 Hash of a String. Unfortunantly, there was nothing here to help.

I did, however, find this site on the web:

http://users.rcn.com/danadler/javacom/MD5Hash.txt

The only thing is Dan Adler's code uses the older md5 class from sun. I have modified the code to use the MessageDigest class in the lastest version of java.security. With this class, you can get an all upper case 32 length hex string representation of the MD5 hash of a string...

String myhash = MD5Hash.hash(mystring);

So if you want to use a salt method to encrypt your passwords, or send clear text hashcodes in an insecure environment, this code will let you reliably create these hash code strings.

Dan Adler wrote the code, I have just modified it to add the latest MessageDigest stuff.

Regards,

aka elephantwalker

import java.security.*;

publicclass MD5Hashextends Object{

/**

* Private function to turn md5 result to 32 hex-digit string

*/

privatestatic String asHex (byte hash[]){

StringBuffer buf =new StringBuffer(hash.length * 2);

int i;

for (i = 0; i < hash.length; i++){

if (((int) hash[i] & 0xff) < 0x10)

buf.append("0");

buf.append(Long.toString((int) hash[i] & 0xff, 16));

}

return buf.toString().toUpperCase();

}

/**

* Take a string and return its md5 hash as a hex digit string

*/

publicstatic String hash(String arg)

{

return hash(arg.getBytes());

}

/**

* Non static version for VB

*/

public String doHash(String arg)

{

return hash(arg.getBytes());

}

/**

* Take a byte array and return its md5 hash as a hex digit string

*/

publicstatic String hash(byte barray[])

{

String restring ="";

try{

MessageDigest md = MessageDigest.getInstance("MD5");

md.update(barray);

byte[] result = md.digest();

restring = asHex(result);

}catch (NoSuchAlgorithmException nsa){}

return restring;

}

}

[3762 byte] By [lwfry@bbnow.net] at [2007-9-26 1:24:19]
# 1
Great post! This code is exactly what I needed! Thanks for sharing.Jesse
jessevitrone at 2007-6-29 1:04:15 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 2

hi,

i tried using your code since i'm trying to do the same thing. However, an error stating: 'illegal start of expression' occurs for the following lines:

if (((int) hash<i> & 0xff) < 0x10)

and

buf.append(Long.toString((int) hash<i> & 0xff, 16));

if anyone has any ideas plesae let me know.

thanks

ginaJo at 2007-6-29 1:04:15 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 3
Looks like the indices "open-bracket" and "close-bracket" got translated to "less-than" and "greater-than".Wes
wesjw at 2007-6-29 1:04:15 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 4
Dead thread but I still need to ask.If I store all passwords in database in MD5 Hash mode how do I do if I want the users to change password on the webpage, should the password field just be empty?Andreas
AGustafsson at 2007-6-29 1:04:15 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 5
Since MD5 and most other Hashing algorithms are one-way algorithms, you cannot display the user's previous password. However, you could leave the field empty and ask the user to input the old and new password for it to be changed.Hope this helps.--VP
all_about_java at 2007-6-29 1:04:15 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 6
Hi,can any one say me how to include this into my LoginServlet programthanks in advance
ranjan1976 at 2007-6-29 1:04:15 > top of Java-index,Security,Other Security APIs, Tools, and Issues...