DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Get MD5 Hash In A Few Lines Of Java
// MD5 in Java, short and sweet version.
1 import java.security.*;
2 import java.math.*;
3
4 public class MD5 {
5 public static void main(String args[]) throws Exception{
6 String s="This is a test";
7 MessageDigest m=MessageDigest.getInstance("MD5");
8 m.update(s.getBytes(),0,s.length());
9 System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16));
10 }
11 }






Comments
Cristian Rivera replied on Thu, 2012/07/05 - 12:31pm
Snippets Manager replied on Wed, 2010/02/10 - 9:49pm
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); }Is what I changed it too. The .format there converts it into a 0 padded 32 hex number.Snippets Manager replied on Sat, 2010/08/07 - 6:08pm
Snippets Manager replied on Sun, 2010/09/26 - 9:39am
while (hashtext.length() < 32) { hashtext = "0" + hashtext; }please check out the link here: Java MD5Snippets Manager replied on Fri, 2010/03/26 - 4:26pm
Snippets Manager replied on Mon, 2009/09/21 - 10:08am
Snippets Manager replied on Mon, 2009/02/23 - 7:22am
Snippets Manager replied on Wed, 2009/02/11 - 3:14am
if (s.length() == 31) { s = "0" + s; }where s is the returning value of the md5 hashing. RegardsSnippets Manager replied on Wed, 2009/02/11 - 3:14am