826220679@qq.com
2025-08-07 4d6cd980c5c69e4d9d150669c89734642297e0cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package mac;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.util.Enumeration;
 
public class MachineCodeGenerator {
 
    public static String getMachineCode() throws Exception {
        // ×éºÏ¶à¸öϵͳ±êʶ·û
        StringBuilder sb = new StringBuilder();
        
        // 1. ²Ù×÷ϵͳÃû³ÆºÍ¼Ü¹¹
        sb.append(System.getProperty("os.name"));
        sb.append(System.getProperty("os.arch"));
        
        // 2. »ñÈ¡MACµØÖ·
        sb.append(getMacAddress());
        
        // 3. »ñÈ¡ÏµÍ³ÌØ¶¨±êʶ·û
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("win")) {
            sb.append(getWindowsIdentifier());
        } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
            sb.append(getLinuxIdentifier());
        }
        
        // 4. Ê¹ÓùþÏ£Ëã·¨Éú³É¹Ì¶¨³¤¶ÈµÄ»úÆ÷Âë
        return generateHash(sb.toString());
    }
 
    private static String getMacAddress() throws Exception {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface network = networkInterfaces.nextElement();
            if (network == null || network.isLoopback() || network.isVirtual() || !network.isUp()) {
                continue;
            }
            
            byte[] mac = network.getHardwareAddress();
            if (mac != null) {
                StringBuilder macAddress = new StringBuilder();
                for (int i = 0; i < mac.length; i++) {
                    macAddress.append(String.format("%02X", mac[i]));
                }
                return macAddress.toString();
            }
        }
        return "NO_MAC";
    }
 
    private static String getWindowsIdentifier() throws Exception {
        // ³¢ÊÔ»ñȡӲÅÌÐòÁкÅ
        try {
            Process process = Runtime.getRuntime().exec(
                new String[]{"wmic", "diskdrive", "get", "serialnumber"});
            process.waitFor();
            
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
            
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.trim().length() > 0 && !line.contains("SerialNumber")) {
                    return line.trim();
                }
            }
        } catch (Exception e) {
            // ±¸Ó÷½°¸£º»ñÈ¡Ö÷°åÐòÁкÅ
            try {
                Process process = Runtime.getRuntime().exec(
                    new String[]{"wmic", "baseboard", "get", "serialnumber"});
                process.waitFor();
                
                BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
                
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.trim().length() > 0 && !line.contains("SerialNumber")) {
                        return line.trim();
                    }
                }
            } catch (Exception ex) {
                // ×îÖÕ±¸ÓãºÊ¹ÓÃÓû§Ãû
                return System.getProperty("user.name");
            }
        }
        return "WIN_ID";
    }
 
    private static String getLinuxIdentifier() throws Exception {
        // ³¢ÊÔ¶ÁÈ¡»úÆ÷IDÎļþ
        File machineIdFile = new File("/etc/machine-id");
        if (machineIdFile.exists() && machineIdFile.canRead()) {
            java.util.Scanner scanner = new java.util.Scanner(machineIdFile);
            try {
                if (scanner.hasNextLine()) {
                    return scanner.nextLine().trim();
                }
            } finally {
                scanner.close();
            }
        }
        
        // ±¸Ó÷½°¸£º¶ÁÈ¡²úÆ·UUID
        File productUuidFile = new File("/sys/class/dmi/id/product_uuid");
        if (productUuidFile.exists() && productUuidFile.canRead()) {
            java.util.Scanner scanner = new java.util.Scanner(productUuidFile);
            try {
                if (scanner.hasNextLine()) {
                    return scanner.nextLine().trim();
                }
            } finally {
                scanner.close();
            }
        }
        
        // ×îÖÕ±¸ÓãºÊ¹ÓÃÖ÷»úÃû
        return java.net.InetAddress.getLocalHost().getHostName();
    }
 
    private static String generateHash(String input) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] hash = md.digest(input.getBytes("UTF-8"));
        
        // ×ª»»ÎªÊ®Áù½øÖÆ×Ö·û´®
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        
        // È¡Ç°16¸ö×Ö·û×÷Ϊ»úÆ÷Âë
        return hexString.toString().substring(0, 16).toUpperCase();
    }
}