添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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, 2011 at 6: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, 2011 at 9:49 since it is a mobile app I don't want to use any additional packages, rather would stay with "plain java". – AndyAndroid Oct 15, 2011 at 9:45 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, 2014 at 14:13 @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, 2015 at 7:07

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.
  • 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, 2011 at 9: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, 2011 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, 2011 at 15:11 @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, 2018 at 8:50

    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);
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.