因为平时用到了,所以记录一下不用第三方类库,用Java原生代码来实现的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| private static String getMd5(String plainText) { MessageDigest md5; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); return ""; } byte[] str = plainText.getBytes(); byte[] md5Bytes = md5.digest(str); StringBuilder cipherText = new StringBuilder(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { cipherText.append("0"); } cipherText.append(Integer.toHexString(val)); } return cipherText.toString(); }
|