15

In http://www.anyexample.com/programming/java/java_simple_class_to_compute_md5_hash.xml an example is given how to calculate an MD5 hash of String. This results in a 20 digit hex string. According to http://en.wikipedia.org/wiki/MD5 I would expect a 32 digit hex string. I get the same result for example using dac2009 response in How can I generate an MD5 hash?.

Why do I get something which looks like a MD5 hash but isn't? I cannot imagine that all the strings I get I have to pad with 12 leading zeros.

Edit: one code example

public static String MungPass(String pass) throws NoSuchAlgorithmException {
    MessageDigest m = MessageDigest.getInstance("MD5");
    byte[] data = pass.getBytes(); 
    m.update(data,0,data.length);
    BigInteger i = new BigInteger(1,m.digest());
    return String.format("%1$032X", i);
}

Taken from http://snippets.dzone.com/posts/show/3686

Community
  • 1
  • 1
AndyAndroid
  • 4,039
  • 14
  • 44
  • 71
  • Your first link is a 404 – Paul Oct 15 '11 at 06:02
  • 2
    Please post a short example (complete) code that demonstrates the problem you are having. Without that, we can't tell you what you're doing wrong :) The method described in the StackOverflow question you link to is how you generate an MD5 hash which indeed is 32 hex characters – Brian Roach Oct 15 '11 at 06:03
  • Code example added, but any code given in the given links is only showing 20 digits. The coding I pasted produces for the input "java" the MD5 "93F725A07423FE1C889F" which 20 digits hex. – AndyAndroid Oct 15 '11 at 09:49

4 Answers4

47

use org.apache.commons.codec.digest.DigestUtils instead:

DigestUtils.md5Hex(str);

this will give you 32 char string as a result

tsds
  • 8,700
  • 12
  • 62
  • 83
  • 2
    since it is a mobile app I don't want to use any additional packages, rather would stay with "plain java". – AndyAndroid Oct 15 '11 at 09:45
  • 5
    the topic title is misleading, because there is no mention about mobile. for example this answer is usefull and simple for me, because i search a simple java example. this answer is perfect for me :) – sarkiroka Dec 13 '14 at 14:13
  • 1
    @sarkiroka if the title/tags had mentioned "mobile", you may not have tried opening this page either. If the OP doesn't prefer a specific answer, he has his right to do so. No need to take it as an offense. :) – Sufian May 24 '15 at 07:07
4

You can use DatatypeConverter.printHexBinary(digiest) to get the 128 bit hash represented by 32 hexadecimal numbers. Below is the complete code snippet to generate MD5 hash in Java,

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;

public class MD5HashGenerator 
{

public static void main(String args[]) throws NoSuchAlgorithmException
{
    String stringToHash = "MyJavaCode"; 
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    messageDigest.update(stringToHash.getBytes());
    byte[] digiest = messageDigest.digest();
    String hashedOutput = DatatypeConverter.printHexBinary(digiest);
    System.out.println(hashedOutput);
}
}
Prasanna L M
  • 301
  • 3
  • 6
4

You must be missing something. The linked code is just fine. Make sure the issue is nowhere else, related to displaying the result. Possibilities:

  • in a GUI too small
  • in a console with multithreading issues
  • over a network package which is being cut off to soon
  • you cut the length to 20 instead of 0x20, which is 32.
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • I did what is given in the first link --> 20 chars, I looked at the second link --> comment to that post: it ignores trailing blanks. So neither method seems to work fine. did you succeed running these? – AndyAndroid Oct 15 '11 at 09:44
  • okay I changed my return statement to "return i.toString(16);". I am still getting the same result. 20 digits. Did you run the code and get 32 digits? – AndyAndroid Oct 15 '11 at 17:58
  • reinstall is not possible, it runs on an Android phone, but you are right I tried in a plain desktop java install, there it is 32 digits. Maybe a bug in Android. Thanks for all your help. – AndyAndroid Oct 16 '11 at 15:11
  • "is in my opinion bad code. It looks like the autor doesn't know how a computer works"!!! wow! it rather seems like that you don't know how formatting works! don't see anything bad in that code! – mohamnag Sep 11 '18 at 06:50
  • 1
    @mohamnag: You are right. I have no idea why I wrote this... (7 years ago). Maybe the code on the linked page changed. Really no clue, the code is indeed fine. Changed my answer. – Martijn Courteaux Sep 11 '18 at 08:50
  • 1
    took back my down vote as appreciation to your honesty ;-) – mohamnag Sep 11 '18 at 11:22
1

I tried your example above, MungPass("java") and I got a 32 digit string, 93f725a07423fe1c889f448b33d21f46. Since you got the 20 first of those correct when you ran, I'm guessing you are simply missing something in the printout?

Pratik Bhat
  • 7,166
  • 2
  • 35
  • 57
dac2009
  • 3,521
  • 1
  • 22
  • 22