Create a MD5-Hash and Dump as a Hex String

5 votes · 12 comments

It's quite easy to create an md5 hash and dump it as hex-string with the java.security package. This function 'public String md5(String s)' does the job for you :-)

raw ·
copy
· download
public String md5(String s) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); for (int i=0; i<messageDigest.length; i++) hexString.append(Integer.toHexString(0xFF & messageDigest[i])); return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; }
Add a comment

12 Comments

this is not working!

the java.security package produces strange md5 hashes with 30, 31 or 32 characters. it seems to cut out 0's here and there.

better go for this easy implementation: http://www.twmacinta.com/myjava/fast_md5.php#download

Reply · July 13, 2009, 3:55 p.m.

Cool,

so you used a MD5 in a hex string!!! thanks, that was inspiring

Keep up the good work

Reply · Aug. 18, 2009, 2:56 p.m.

Quite inspiring,

hashing this way can prevent hackers to use the data so easily

Keep up the good work

Reply · Aug. 20, 2009, 8:16 p.m.

This MD5 is not working properly. Chris is correct on this one. I am missing 0s aas well.

Reply · Aug. 28, 2009, 3:34 p.m.

I was looking for something like this, but this function skips zeroes. No good at all.

I found something that works well, however:

public static String MD5_Hash(String s) { MessageDigest m = null;

    try {
            m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
    }

    m.update(s.getBytes(),0,s.length());
    String hash = new BigInteger(1, m.digest()).toString(16);
    return hash;

}

From http://www.mail-archive.com/android-beginners@googlegroups.com/msg18680.html

Reply · April 13, 2011, 2:07 a.m.

Try this.. public static final String md5(final String s) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest .getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

Source :http://www.kospol.gr/204/create-md5-hashes-in-android/

Reply · May 5, 2011, 5:50 a.m.

Try this.. It works for me..

public static final String md5(final String s) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest .getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

Source :

http://www.kospol.gr/204/create-md5-hashes-in-android/

Reply · May 5, 2011, 5:51 a.m.

Worked for me ..... thanks ....

Reply · June 20, 2011, 5:37 p.m.

Since: API Level 1

http://developer.android.com/reference/java/security/MessageDigest.html

MessageDigest digester = MessageDigest.getInstance("MD5"); byte[] bytes = new byte[8192]; int byteCount; while ((byteCount = in.read(bytes)) > 0) { digester.update(bytes, 0, byteCount); } byte[] digest = digester.digest();

Reply · May 8, 2011, 9:08 a.m.

Didn't work for me crashed on in.read(bytes) ....

Reply · June 20, 2011, 5:36 p.m.

its working....my code for android.is package com.hari.md5;

import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; //import java.util.ArrayList; //import java.util.Collections;

import android.app.Activity; import android.os.Bundle; import android.widget.TextView;

public class Projectmd5Activity extends Activity { / Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

String s = "000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52methodflickr.auth.getFullTokenmini_token123-456-789";
   String  res = md5(s);
   TextView tv = new TextView(this);
   tv.setText(res);
   setContentView(tv);

}

public String md5(String s) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest();

     // Create Hex String
     StringBuffer hexString = new StringBuffer();
     for (int i=0; i<messageDigest.length; i++)
         hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
    return hexString.toString();

 } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
 }
 return "";

}

}
Reply · July 20, 2011, 10:15 a.m.

As I know, this MD5 is not working properly. Chris is correct on this one. I am missing 0s aas well.

Reply · March 3, 2012, 2:55 a.m.