com.fastchar.utils.FastMD5Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastchar Show documentation
Show all versions of fastchar Show documentation
FastChar is Web+ORM Framework in Java.
package com.fastchar.utils;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
/**
* MD5加密
*
*/
public class FastMD5Utils {
public static String MD5To16(Object data) {
return MD5(data).substring(8, 24);
}
public static String MD5(Object data, int md5Length) {
return MD5(data).substring(0, Math.min(md5Length, 32));
}
public static String MD5(Object data) {
String s = String.valueOf(data);
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};
try {
if (s != null && !"".equals(s.trim())) {
byte[] strTemp = s.getBytes(StandardCharsets.UTF_8);
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} else {
return "";
}
} catch (Exception ignored) {
}
return data.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy