Turn MD5 MessageDigest into string

4

I'm trying to generate an MD5 hash using the MessageDigest class, but I can not correctly display the hash as a string. The result is a string of unknown characters.

Below is the test code:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author 
 */
public class TesteMD5
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {

       String str = "teste md5";

       try{
          MessageDigest md = MessageDigest.getInstance("MD5");
          md.update(str.getBytes());
          byte[] bytes = md.digest();
          System.out.println("Hash: " + new String(bytes));
       }catch(NoSuchAlgorithmException e){
          e.printStackTrace();
       }
    }
}

The result I'm having is:

Hash: ���9��p>n$�u �
    
asked by anonymous 20.07.2016 / 14:31

4 answers

2

You are returning the string representation of the bytes returned by the MessageDigest method.

The code to represent what you want is this:

    String original = "teste md5";

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(original.getBytes());
        byte[] digest = md.digest();
        StringBuffer sb = new StringBuffer();

        for (byte b : digest)
            sb.append(String.format("%02x", b & 0xff));

        System.out.println("string original: " + original);
        System.out.println("digested(hex): " + sb.toString());
    } catch(NoSuchAlgorithmException e){
        e.printStackTrace();
    }

You can see and run this example here: jdoodle.com/a/Yi

The output will be:

string original: teste md5
digested(hex): d4d1c93999f913703e6e0524b17520ef

Source: link

    
20.07.2016 / 15:00
4

Try this, this is what I use in my algorithm:

      String str = "teste md5";

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(str.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
          System.out.println("Hash: " + sb.toString());
        } catch (NoSuchAlgorithmException e) {
           e.printStackTrace();
        }
    
20.07.2016 / 14:46
3

Try using DataTypeConverter , I also would pass an encoding to keep the hash portable ( getBytes assumes the platform's default encoding):

md.update(str.getBytes("UTF-8"));
byte[] bytes = md.digest();
System.out.println("Hash: " + DatatypeConverter.printHexBinary(bytes));

Font : SOen - Get MD5 String from Message Digest

    
20.07.2016 / 14:46
3

There is also this option:

    String s="teste md5";
    MessageDigest m=MessageDigest.getInstance("MD5");
    m.update(s.getBytes("UTF-8"),0,s.length());
    System.out.println("MD5: " + new BigInteger(1,m.digest()).toString(16));

Source: How can I generate MD5 Hash

    
20.07.2016 / 14:57