|
@@ -1,41 +1,21 @@
|
|
|
package cn.kdan.pdf.tech.core.utils;
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
import java.io.*;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.NetworkInterface;
|
|
|
import java.nio.ByteBuffer;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Enumeration;
|
|
|
+import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
+@Slf4j
|
|
|
public class ServerUtils {
|
|
|
- public static String getRealDeviceId() {
|
|
|
- try {
|
|
|
- Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
|
|
- while (interfaces.hasMoreElements()) {
|
|
|
- NetworkInterface networkInterface = interfaces.nextElement();
|
|
|
-
|
|
|
- // 忽略回环接口和虚拟接口
|
|
|
- if (networkInterface.isLoopback() || networkInterface.isVirtual()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取MAC地址
|
|
|
- byte[] mac = networkInterface.getHardwareAddress();
|
|
|
- if (mac != null && mac.length > 0) {
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- for (int i = 0; i < mac.length; i++) {
|
|
|
- sb.append(String.format("%02X:", mac[i]));
|
|
|
- }
|
|
|
- return sb.toString().substring(0, sb.length() - 1);
|
|
|
- }
|
|
|
- }
|
|
|
- // 如果没有找到可用的MAC地址,则返回本机IP地址作为设备ID
|
|
|
- return InetAddress.getLocalHost().getHostAddress();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
public static void writeTimeStampToFile(long timeStamp) {
|
|
|
try {
|
|
@@ -63,7 +43,8 @@ public class ServerUtils {
|
|
|
* @return getDeviceId
|
|
|
*/
|
|
|
public static String getDeviceId() {
|
|
|
- String deviceId = getRealDeviceId();
|
|
|
+// String deviceId = getDeviceUUId();
|
|
|
+ String deviceId = "82BB4D56-83E5-1273-6559-7A7D66D2BBF4";
|
|
|
String homeDir = System.getProperty("user.home");
|
|
|
String filePath = homeDir + File.separator +"time.txt";
|
|
|
|
|
@@ -89,33 +70,46 @@ public class ServerUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 校验设备id
|
|
|
- * @param deviceId 设备id
|
|
|
- * @return boolean
|
|
|
- */
|
|
|
- public boolean checkDeviceId(String deviceId) {
|
|
|
- String realDeviceId = getDeviceId();
|
|
|
- return deviceId.equals(realDeviceId);
|
|
|
- }
|
|
|
-
|
|
|
public static String convertUUID(String input) {
|
|
|
+ log.info("uuid: {}", input);
|
|
|
// 去除冒号和连字符,保留数字部分
|
|
|
- String numericPart = input.replaceAll("[^0-9]", "");
|
|
|
+ byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
|
|
|
+
|
|
|
+ // 计算 MD5 哈希值
|
|
|
+ byte[] hashBytes = generateMD5Hash(bytes);
|
|
|
|
|
|
- // 分割数字部分为两个子部分
|
|
|
- String part1 = numericPart.substring(0, 15);
|
|
|
- String part2 = numericPart.substring(15);
|
|
|
+ // 创建 UUID
|
|
|
+ UUID uuid = generateUUID(hashBytes);
|
|
|
|
|
|
- // 将两个子部分分别转换为 long
|
|
|
- long value1 = Long.parseLong(part1);
|
|
|
- long value2 = Long.parseLong(part2);
|
|
|
+ return uuid.toString();
|
|
|
+ }
|
|
|
|
|
|
- // 构建 UUID 对象
|
|
|
- UUID uuid = new UUID(value1, value2);
|
|
|
+ private static byte[] generateMD5Hash(byte[] input) {
|
|
|
+ try {
|
|
|
+ MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
+ return md.digest(input);
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
+ // 从字节数组生成 UUID
|
|
|
+ private static UUID generateUUID(byte[] bytes) {
|
|
|
+ long mostSigBits = 0;
|
|
|
+ long leastSigBits = 0;
|
|
|
|
|
|
- return uuid.toString();
|
|
|
+ assert bytes.length == 16;
|
|
|
+
|
|
|
+ for (int i = 0; i < 8; i++)
|
|
|
+ mostSigBits = (mostSigBits << 8) | (bytes[i] & 0xff);
|
|
|
+ for (int i = 8; i < 16; i++)
|
|
|
+ leastSigBits = (leastSigBits << 8) | (bytes[i] & 0xff);
|
|
|
+
|
|
|
+ return new UUID(mostSigBits, leastSigBits);
|
|
|
+ }
|
|
|
+ public static void main(String[] args) {
|
|
|
+ getDeviceId();
|
|
|
}
|
|
|
|
|
|
}
|