Turn Messagedigest MD5 into a string

Asked

Viewed 1,605 times

5

I am trying to generate an MD5 hash using the class MessageDigest, however, I cannot correctly display the hash as a string on the screen. 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 �

4 answers

4

Try it like 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();
        }

3

Try to use the DataTypeConverter, i would also pass an encoding to keep the hash portable (getBytes assumes the encoding platform standard):

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

Source: Soen - Get MD5 String from Message Digest

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 a MD5 Hash

3


You are returning the string representation of 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 see and run this example here: jdoodle.com/a/Yi

The exit will be:

string original: teste md5
digested(hex): d4d1c93999f913703e6e0524b17520ef

Source: http://www.avajava.com/tutorials/lessons/how-do-i-generate-an-md5-digest-for-a-string.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.