code_junkie

Generate MD5 hash in Java

Nov 13th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import java.security.*;
  2.  
  3. ..
  4.  
  5. byte[] bytesOfMessage = yourString.getBytes("UTF-8");
  6.  
  7. MessageDigest md = MessageDigest.getInstance("MD5");
  8. byte[] thedigest = md.digest(bytesOfMessage);
  9.  
  10. String plaintext = 'your text here';
  11. MessageDigest m = MessageDigest.getInstance("MD5");
  12. m.reset();
  13. m.update(plaintext.getBytes());
  14. byte[] digest = m.digest();
  15. BigInteger bigInt = new BigInteger(1,digest);
  16. String hashtext = bigInt.toString(16);
  17. // Now we need to zero pad it if you actually want the full 32 chars.
  18. while(hashtext.length() < 32 ){
  19. hashtext = "0"+hashtext;
  20. }
  21.  
  22. final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
  23. messageDigest.reset();
  24. messageDigest.update(string.getBytes(Charset.forName("UTF8")));
  25. final byte[] resultByte = messageDigest.digest();
  26. final String result = new String(Hex.encodeHex(resultByte));
  27.  
  28. public String MD5(String md5) {
  29. try {
  30. java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
  31. byte[] array = md.digest(md5.getBytes());
  32. StringBuffer sb = new StringBuffer();
  33. for (int i = 0; i < array.length; ++i) {
  34. sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
  35. }
  36. return sb.toString();
  37. } catch (java.security.NoSuchAlgorithmException e) {
  38. }
  39. return null;
  40. }
  41.  
  42. String hash = MD5.asHex(MD5.getHash(new File(filename)));
  43.  
  44. String Url = "your_url";
  45.  
  46. System.out.println( DigestUtils.md5Hex( Url ) );
Add Comment
Please, Sign In to add comment