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();
|
}
|
}
|