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 :-)
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 "";
}
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
Cool,
so you used a MD5 in a hex string!!! thanks, that was inspiring
Keep up the good work
Quite inspiring,
hashing this way can prevent hackers to use the data so easily
Keep up the good work
This MD5 is not working properly. Chris is correct on this one. I am missing 0s aas well.
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;
}
From http://www.mail-archive.com/android-beginners@googlegroups.com/msg18680.html
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();
Source :http://www.kospol.gr/204/create-md5-hashes-in-android/
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();
Source :
http://www.kospol.gr/204/create-md5-hashes-in-android/
Worked for me ..... thanks ....
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();
Didn't work for me crashed on in.read(bytes) ....
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);
}
public String md5(String s) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest();
}
As I know, this MD5 is not working properly. Chris is correct on this one. I am missing 0s aas well.