You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.0 KiB
68 lines
2.0 KiB
package com.dxhy.common.util;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
public class MD5 {
|
|
|
|
/**
|
|
* 获得随机数字
|
|
*
|
|
* @param num
|
|
* 位数
|
|
* @return
|
|
*/
|
|
public static String getRandom(int num) {
|
|
double key = Math.random() * 100000;
|
|
StringBuilder result = new StringBuilder(Integer.toString((int)key));
|
|
if (result.length() > num) {
|
|
result = new StringBuilder(result.substring(result.length() - num));
|
|
} else {
|
|
for (int i = num - result.length(); i > 0; i--) {
|
|
result.insert(0, "0");
|
|
}
|
|
}
|
|
return result.toString();
|
|
}
|
|
|
|
/**
|
|
* 获取Md5码
|
|
*
|
|
* @param key
|
|
* @return String
|
|
* @throws NoSuchAlgorithmException
|
|
*/
|
|
public static String getEncode(String key) throws NoSuchAlgorithmException {
|
|
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
|
messageDigest.update(key.getBytes(StandardCharsets.UTF_8));
|
|
byte[] digest = messageDigest.digest();
|
|
int j = digest.length;
|
|
char[] str = new char[j * 2];
|
|
int k = 0;
|
|
for (byte byte0 : digest) {
|
|
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
|
|
str[k++] = hexDigits[byte0 & 0xf];
|
|
}
|
|
|
|
return new String(str);
|
|
}
|
|
|
|
public static String getEncode(byte[] key) throws NoSuchAlgorithmException {
|
|
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
|
messageDigest.update(key);
|
|
byte[] digest = messageDigest.digest();
|
|
int j = digest.length;
|
|
char[] str = new char[j * 2];
|
|
int k = 0;
|
|
for (byte byte0 : digest) {
|
|
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
|
|
str[k++] = hexDigits[byte0 & 0xf];
|
|
}
|
|
|
|
return new String(str);
|
|
}
|
|
|
|
static final char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
|
|
|
}
|
|
|