根据网卡、应用启动时间、当前时间,通过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; 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