Wednesday, 9 March 2016

Implementation of MD5 Algorithm in Java

Implementation of MD5 Algorithm in Java


java Program

  1. import java.math.BigInteger;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. public class JavaMD5Hash {
  5. public static void main(String[] args) {
  6. System.out.println("For null " + md5(""));
  7. System.out.println("For simple text "+ md5("This is my text"));
  8. System.out.println("For simple numbers " + md5("12345"));
  9. }
  10. public static String md5(String input) {
  11. String md5 = null;
  12. if(null == input) return null;
  13. try {
  14. //Create MessageDigest object for MD5
  15. MessageDigest digest = MessageDigest.getInstance("MD5");
  16. //Update input string in message digest
  17. digest.update(input.getBytes(), 0, input.length());
  18. //Converts message digest value in base 16 (hex)
  19. md5 = new BigInteger(1, digest.digest()).toString(16);
  20. }
  21. catch (NoSuchAlgorithmException e) {
  22. e.printStackTrace();
  23. }
  24. return md5;
  25. }
  26. }

No comments:

Post a Comment