80 lines
2.1 KiB
Java
80 lines
2.1 KiB
Java
|
|
package com.hxkj.common.utils;
|
||
|
|
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
import java.security.MessageDigest;
|
||
|
|
import java.util.Random;
|
||
|
|
import java.util.UUID;
|
||
|
|
|
||
|
|
public class ToolsUtil {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 制作UUID
|
||
|
|
*
|
||
|
|
* @author fzr
|
||
|
|
* @return String
|
||
|
|
*/
|
||
|
|
public static String makeUUID(){
|
||
|
|
return UUID.randomUUID().toString().replaceAll("-","").substring(0,32);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 制作MD5
|
||
|
|
*
|
||
|
|
* @author fzr
|
||
|
|
* @param data 需加密的数据
|
||
|
|
* @return String
|
||
|
|
*/
|
||
|
|
public static String makeMd5(String data){
|
||
|
|
try {
|
||
|
|
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
||
|
|
byte [] array = md5.digest(data.getBytes(StandardCharsets.UTF_8));
|
||
|
|
StringBuilder sb = new StringBuilder();
|
||
|
|
for (byte item : array) {
|
||
|
|
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
|
||
|
|
}
|
||
|
|
return sb.toString();
|
||
|
|
}catch (Exception e){
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 返回随机字符串
|
||
|
|
*
|
||
|
|
* @author fzr
|
||
|
|
* @param length 要生成的长度
|
||
|
|
* @return String
|
||
|
|
*/
|
||
|
|
public static String randomString(int length) {
|
||
|
|
Random random = new Random();
|
||
|
|
StringBuilder stringBuffer = new StringBuilder();
|
||
|
|
String str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||
|
|
int strLength = str.length();
|
||
|
|
for (int i = 0; i < length; i++) {
|
||
|
|
int index = random.nextInt(strLength);
|
||
|
|
stringBuffer.append(str.charAt(index));
|
||
|
|
}
|
||
|
|
return stringBuffer.toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 返回随机数字字符串
|
||
|
|
*
|
||
|
|
* @author fzr
|
||
|
|
* @param length 要生成的长度
|
||
|
|
* @return String
|
||
|
|
*/
|
||
|
|
public static String randomInt(int length) {
|
||
|
|
Random random = new Random();
|
||
|
|
StringBuilder stringBuffer = new StringBuilder();
|
||
|
|
String str = "0123456789";
|
||
|
|
for (int i = 0; i < length; i++) {
|
||
|
|
int index = random.nextInt(10);
|
||
|
|
stringBuffer.append(str.charAt(index));
|
||
|
|
}
|
||
|
|
return stringBuffer.toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|