我是靠谱客的博主 刻苦飞机,最近开发中收集的这篇文章主要介绍全局唯一ID生成工具类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

根据网卡、应用启动时间、当前时间,通过DESKeySpec随机加密,随机获取固定长度全局唯一ID。

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Locale;
import java.util.Random;
import static org.apache.commons.codec.binary.Hex.decodeHex;
/**
* 全局唯一id生成工具类
* @author 聂大顺
*/
public class IdUtil {
static String baseNumLetter = "0123456789ABCDEFGHJKLMNOPQRSTUVWXYZ";
private static Logger logger = LogManager.getLogger();
private static final String data =
"a81dfab773f05633e755ce05af7347b130b0ac544b053889e04b1e195864d15bBA";
/** 获取机器名 */
public static final String getHostName() {
String hostName = "";
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
}
return hostName;
}
/** 获取网卡序列号 */
public static final String getDUID() {
String address = "";
String command = "cmd.exe /c ipconfig /all";
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("DUID") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
}
return address;
}
public static String randomSort(String str){
char[] arr=str.toCharArray();
Random r = new Random();//创建随机数对象
for (int i = 0; i < arr.length; i++) {//打乱顺序的原理是第i个数组元素与随机出来的0-4小标的元素进行互换位置
int r1 = r.nextInt(arr.length);//将随机出来的数存入r1中//这里括号里不用具体数值,就是为了代码的多场景性
char temp;//定义一个中间变量
temp = arr[i];//开始互换元素
arr[i] = arr[r1];
arr[r1] = temp;
}
return new String(arr);
}
public static String getUid(String str,int length){
str=str.toUpperCase(Locale.ROOT);
char []ch=str.toCharArray();
char[]root= baseNumLetter.toCharArray();
StringBuilder sb=new StringBuilder();
for(int i=0;i<ch.length;i++){
for(int j=0;j<root.length; j++){
if(ch[i]==root[j]){
sb.append(j);
continue;
}
}
}
String uidStr=sb.toString();
StringBuffer sbt = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sbt.append(uidStr.charAt(random.nextInt(uidStr.length())));
}
String tempUid=sbt.toString();
if(tempUid.startsWith("0")){
random = new Random();
Integer number= random.nextInt();
tempUid.replace("0",number+"");
}
return tempUid;
}
//生成加密解密的随机种子
public static String encrypt(String password) throws NoSuchAlgorithmException, DecoderException,
InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
byte[] key = decodeHex(data);
//从原始密钥数据创建DESKeySpec对象
DESKeySpec desKeySpec = new DESKeySpec(key);
//创建一个密钥工厂,然后把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
//cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
//用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
byte[] bytes = cipher.doFinal(password.getBytes());
return Hex.encodeHexString(bytes);
}
public static String getId() throws NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, DecoderException, InvalidKeyException, InvalidKeySpecException {
long time = ManagementFactory.getRuntimeMXBean().getStartTime()+System.currentTimeMillis();
String uid=encrypt(randomSort(getDUID()+time));
String rootUid= getUid(uid,30);
return rootUid;
}
public static void main(String[] args) throws NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, DecoderException, InvalidKeyException, InvalidKeySpecException {
System.out.println(getId());
}
}

最后

以上就是刻苦飞机为你收集整理的全局唯一ID生成工具类的全部内容,希望文章能够帮你解决全局唯一ID生成工具类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部