Java MD5 String MD5(String str)

Here you can find the source of MD5(String str)

Description

MD

License

Open Source License

Declaration

public static String MD5(String str) 

Method Source Code


//package com.java2s;
import java.security.MessageDigest;

public class Main {

    public static String MD5(String str) {
        try {/*from   w w  w  .  ja  va2s  . c  o  m*/
            if (isBlank(str)) {
                return null;
            }
            MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            md.update(str.getBytes("UTF-8"));
            byte tmp[] = md.digest();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 16; i++) {
                sb.append(String.format("%02x", tmp[i]));
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static boolean isBlank(String str) {
        int length;

        if ((str == null) || ((length = str.length()) == 0)) {
            return true;
        }

        for (int i = 0; i < length; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. md5(String str)
  2. md5(String str)
  3. md5(String str)
  4. md5(String str)
  5. md5(String str)
  6. MD5(String str)
  7. md5(String str)
  8. md5(String str)
  9. md5(String str)