java生成32位大写MD5值

版权声明:本文为博主原创文章,未经博主允许不得转载。深圳夸克时代在线技术有限公司 官网:http://www.kksdapp.com https://blog.csdn.net/wahaha13168/article/details/82903883
/**
* 生成32位大写MD5值
*/
private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5',
      '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

public static String getMD5String(String str) {
  try {
      if (str == null || str.trim().length() == 0) {
        return "";
      }
      byte[] bytes = str.getBytes();
      MessageDigest messageDigest = MessageDigest.getInstance("MD5");
      messageDigest.update(bytes);
      bytes = messageDigest.digest();
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < bytes.length; i++) {
        sb.append(HEX_DIGITS[(bytes[i] & 0xf0) >> 4] + ""
              + HEX_DIGITS[bytes[i] & 0xf]);
      }
      return sb.toString();
  } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
  }
  return "";
}

猜你喜欢

转载自blog.csdn.net/wahaha13168/article/details/82903883