Creating MD5 hashed passwords in Java

I found a much-needed Java programming tip in a LiveJournal XML-RPC client programming discussion this afternoon: How to create an MD5-hashed password from a string.

Radio UserLand only presents hashed passwords when publishing via XML-RPC, storing the plain text version in the object database. The following class method can be used to create a hash from a string:

public static String hashPassword(String password) {
String hashword = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
BigInteger hash = new BigInteger(1, md5.digest());
hashword = hash.toString(16);
} catch (NoSuchAlgorithmException nsae) {
// ignore
return hashword;

MessageDigest and NoSuchAlgorithm are from the package. At this point, I only have tested it with JStorageSystem, an open-source Java implementation of the Radio Community Server that I am working on. Programmer emptor.


The proposed method does not work correctly,
if the md5 digest contains a MSB which is
less 0x10. In this case the hash string is
only 31 characters long instead of 32.

Greetings from Bremen,

Daniel Krügler

That's a lot of code for a single hash!

For comparison, the Python version:

import md5
hashword = md5.md5(password).hexdigest()

I'd like to have an information please:

I'm working on the draft "Computational Puzzle against SPAM", which "defines a mechanism for a proxy or UAS to request that a UAC compute the solution to a puzzle.
The puzzle is based on finding a value called the pre-image that, when hashed with SHA1, results in a specific value referred to as the image."

I need to hash a String, to compare it with a value, and to increment my String to try his next value.
Could you tell me how it would be possible to increment a String please???

Thank you very much, best regards,

>>The proposed method does not work correctly

Very true! I ignored this comment and it caused headaches ;) Some passowrds work but others don't.

With this method:
"test" ->

When it should be:
(note the '0')

Adding something like:
if(hashword.length() == 31){
hashword = "0"+hashword;
makes it work.

Note that Jakarta Commons has a utility for accomplishing this for MD5 and SHA:

hi , this is good, but i want to get string valuu from Hex, is it possible?
mail me at

Isn't it possible to have more than one leading zero? How about:

public String toHex (String hashword) {
String h = hashword;
while (h.length < 32) h = "0" + h; return h; }

pls tell me how to decrypt to get the source value.

you should create a cycle from 0 to infinite and check what number generates this md5, you could find several numbers XD

How to MD5 to String again or decode ?

Thanks, it worked properly for me.

Thank you! It looks that The method does not work correctly, the expected MD5 value shoud be 32 characters. You should add the following line:
while (hashtext.length() < 32) { hashtext = "0" + hashtext; } check out: Java MD5 example

Hopefully see you again

Add a Comment

All comments are moderated before publication. These HTML tags are permitted: <p>, <b>, <i>, <a>, and <blockquote>. This site is protected by reCAPTCHA (for which the Google Privacy Policy and Terms of Service apply).