根据指定规则生成唯一纯数字Id
JAVA版
import java.security.MessageDigest;
/**
* @ClassName: Md5Utils
* @Author yang
* @Date: 2021/4/25 9:52 上午
* @Version 1.0
*/
public class Md5Utils {
public static String byteArrayToString2(byte[] b) {
StringBuilder sb = new StringBuilder(40);
for (byte x : b) {
sb.append(Integer.toHexString(x & 0xff));
}
return sb.toString();
}
public static String byteArrayToString(byte[] b) {
StringBuffer sb = new StringBuffer(40);
for (byte x : b) {
sb.append(byteToNumString(x));
}
return sb.toString();
}
public static String encrypt16(byte[] b) {
return byteArrayToString(b).substring(8, 24);
}
private static String byteToNumString(byte b) {
int _b = b;
if (_b < 0) {
_b = 256 + _b;
}
return String.valueOf(_b);
}
public static Long MD5Encode(String origin) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString = encrypt16(md.digest(resultString.getBytes()));
} catch (Exception ex) {
}
return Long.parseLong(resultString);
}
public static String MD5Encode2(String origin) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString = byteArrayToString2(md.digest(resultString.getBytes()));
} catch (Exception ex) {
}
return resultString;
}
public static void main(String[] args) {
System.out.println(Md5Utils.MD5Encode("张三-李四-1"));
System.out.println(Md5Utils.MD5Encode("张三-李四-2"));
}
}
Mysql版
select
cast(conv( substr( md5( CONCAT( '测试', '完成', '0000' )), 15, 16 ), 16, 10 ) AS DECIMAL ( 65 )) AS `20位`,
cast(conv( substr( md5( CONCAT( '测试', '完成', '0000' )), 16, 15 ), 16, 10 ) AS DECIMAL ( 65 )) AS `17位`