Java原生实现md5散列

因为平时用到了,所以记录一下不用第三方类库,用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();
}
文章作者: Qug_
文章链接: https://www.qugcloud.cn/2019/02/08/java-md5/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Qug's Blog