package com.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
public class MD5Utils {
public static String getMd5(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
// 32位
// return buf.toString();
// 16位
// return buf.toString().substring(0, 16);
return buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public static String getMd5(String plainText,int length) {
return getMd5(plainText).substring(0, length);
}
public static int getRandomCode(){
int max=9999;
int min=1111;
Random random = new Random();
return random.nextInt(max)%(max-min+1) + min;
}
}
原文:https://www.cnblogs.com/linlangtianshang/p/11824773.html