sky-take-out/sky-common/src/main/java/com/sky/common/utils/MD5.java

35 lines
1.0 KiB
Java
Raw Normal View History

2024-03-11 21:08:36 +08:00
package com.sky.common.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public final class MD5 {
public static String encrypt(String strSrc) {
try {
2024-03-16 21:54:38 +08:00
char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'};
2024-03-11 21:08:36 +08:00
byte[] bytes = strSrc.getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(bytes);
bytes = md.digest();
int j = bytes.length;
char[] chars = new char[j * 2];
int k = 0;
2024-03-16 21:54:38 +08:00
for (byte b : bytes) {
2024-03-11 21:08:36 +08:00
chars[k++] = hexChars[b >>> 4 & 0xf];
chars[k++] = hexChars[b & 0xf];
}
return new String(chars);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException("MD5加密出错+" + e);
}
}
public static void main(String[] args) {
System.out.println(MD5.encrypt("111111"));
}
}