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

Snippets has posted 5883 posts at DZone. View Full User Profile

[Java] MD5 Hash

04.27.2007
| 8298 views |
  • submit to reddit
        
    private String getMD5Hash(String data, key) {
    	
        try {
        	
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(data.getBytes());
            byte result[] = md5.digest(key.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < result.length; i++) {
                String s = Integer.toHexString(result[i]);
                int length = s.length();
                if (length >= 2) {
                    sb.append(s.substring(length - 2, length));
                } else {
                    sb.append("0");
                    sb.append(s);
                }
            }
            return sb.toString();
            
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:38pm

Better use This