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

Greg has posted 3 posts at DZone. View Full User Profile

Get MD5 Hash In A Few Lines Of Java

03.18.2007
| 146138 views |
  • submit to reddit
        // 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

Hello, I am wondering why you would use MD5, an algorithm thats has already been broken instead of something like GrandCentral which creates a password digest based on the time of day using SHA-512. GrandCentral also includes the SimpleCrypto class which can generate hashes and checksums using MD5, SHA1, SHA-256, SHA-384 and SHA-512. Just a suggestion. http://code.google.com/p/grandcentral/

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

it looks like most people are converging on the same approach for this. I use the method shown in http://www.spiration.co.uk/post/1199/ which just says to do the following (similar to above, but with the exception handled): [code] static String getMd5Digest(String pInput) { try { MessageDigest lDigest = MessageDigest.getInstance("MD5"); lDigest.update(pInput.getBytes()); BigInteger lHashInt = new BigInteger(1, lDigest.digest()); return String.format("%1$032X", lHashInt); } catch(NoSuchAlgorithmException lException) { throw new RuntimeException(lException); } } [/code]

Snippets Manager replied on Sun, 2010/09/26 - 9:39am

your code looks it has a little problem. it just return 31 characters. You may add: while (hashtext.length() < 32) { hashtext = "0" + hashtext; } please check out the link here: Java MD5

Snippets Manager replied on Fri, 2010/03/26 - 4:26pm

anybody knows how i can use such a java code in matlab? thanks

Snippets Manager replied on Mon, 2009/09/21 - 10:08am

To em: Hi, Useful script but it has a little problem - if the returned md5 value has a 00 in front it will be trunked. My workaround was adding the following code: if (s.length() == 30) { s = "00" + s; }

Snippets Manager replied on Mon, 2009/02/23 - 7:22am

Hi, but can i know as what the return value of the code will be?? i mean to ask which datatype??

Snippets Manager replied on Wed, 2009/02/11 - 3:14am

Hi, Useful script but it has a little problem - if the returned md5 value has a 0 in front it will be trunked. For example, for "f78spx", in php I get "0b54f7d8c5926903d31576da3518a167", while java returns "b54f7d8c5926903d31576da3518a167". My workaround was adding the following code: if (s.length() == 31) { s = "0" + s; } where s is the returning value of the md5 hashing. Regards

Snippets Manager replied on Wed, 2009/02/11 - 3:14am

Well, I guess the best solution would be while (s.length() < 32) { s = "0" + s; } But still, analyzing it a little it seems like a childish workaround, maybe anyone has a better ideea... Thanks