Java calculate MD5 hash
Asked Answered
H

4

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

Honorarium answered 15/10, 2011 at 5:57 Comment(3)
Your first link is a 404Zosi
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 charactersSwordbill
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.Honorarium
K
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.
Kulseth answered 15/10, 2011 at 8:16 Comment(6)
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?Honorarium
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?Honorarium
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.Honorarium
"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!Firewarden
@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.Kulseth
took back my down vote as appreciation to your honesty ;-)Firewarden
S
47

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

DigestUtils.md5Hex(str);

this will give you 32 char string as a result

Sandal answered 15/10, 2011 at 6:32 Comment(3)
since it is a mobile app I don't want to use any additional packages, rather would stay with "plain java".Honorarium
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 :)Diphosgene
@Diphosgene 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. :)Corrigendum
K
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.
Kulseth answered 15/10, 2011 at 8:16 Comment(6)
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?Honorarium
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?Honorarium
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.Honorarium
"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!Firewarden
@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.Kulseth
took back my down vote as appreciation to your honesty ;-)Firewarden
T
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);
}
}
Turnabout answered 3/1, 2019 at 11:43 Comment(0)
T
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?

Tower answered 22/1, 2013 at 17:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.