Encrypt MD5 Java


SUBMITTED BY: almon

DATE: Dec. 18, 2015, 5:44 p.m.

FORMAT: Java

SIZE: 930 Bytes

HITS: 777

  1. import java.io.UnsupportedEncodingException;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. public class MD5 {
  5. public static String encrypt(String message) throws NoSuchAlgorithmException, UnsupportedEncodingException {
  6. MessageDigest md = MessageDigest.getInstance("MD5");
  7. byte[] hash = md.digest(message.getBytes("UTF-8"));
  8. return convertToHex(hash);
  9. }
  10. public static String convertToHex(byte[] hash){
  11. StringBuilder sb = new StringBuilder(2 * hash.length);
  12. for (byte b : hash) {
  13. sb.append(String.format("%02x", b & 0xff));
  14. }
  15. return sb.toString();
  16. }
  17. public static boolean compareMD5(String digesta, String digestb) throws UnsupportedEncodingException{
  18. return MessageDigest.isEqual(digesta.getBytes("UTF-8"), digestb.getBytes("UTF-8"));
  19. }
  20. }

comments powered by Disqus