当前位置:正能量 » 代码编程 » Java » Java根据硬件信息生成唯一机器码

Java根据硬件信息生成唯一机器码

代码如下:

public class MachineCodeUtil {

    public static void main(String[] args) {
        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hal = systemInfo.getHardware();

        String cpuSerial = getCpuSerial(hal.getProcessor());
        System.out.println("CPU序列号:" + cpuSerial);
        String diskSerial = getDiskSerial(systemInfo);
        System.out.println("磁盘序列号:" + diskSerial);
        String motherboardSerial = getMotherboardSerial(hal.getComputerSystem());
        System.out.println("主板序列号:" + motherboardSerial);
        String memorySerial = getMemorySerial(hal.getMemory());
        System.out.println("内存大小:" + memorySerial);
        String combinedInfo = cpuSerial + diskSerial + motherboardSerial + memorySerial;
        String machineCode = SecureUtil.sha256(combinedInfo);
        System.out.println("Unique Machine Code: " + machineCode);
    }

    /**
     * CPU序列号
     * @param processor
     * @return
     */
    private static String getCpuSerial(CentralProcessor processor) {
        return processor.getProcessorIdentifier().getProcessorID();
    }

    /**
     * 操作系统所在磁盘的驱动器UUID
     * @param si
     * @return
     */
    private static String getDiskSerial(SystemInfo si) {
        String systemDrive = System.getenv("SystemDrive") + File.separator;
        StringBuilder serials = new StringBuilder();
        List<OSFileStore> stores = si.getOperatingSystem().getFileSystem().getFileStores();
        for (OSFileStore store : stores) {
            if (systemDrive.equals(store.getMount())) {
                serials.append(store.getUUID());
                break;
            }
        }
        return serials.toString().trim();
    }

    /**
     * 主板序列号
     * @param computerSystem
     * @return
     */
    private static String getMotherboardSerial(ComputerSystem computerSystem) {
        return computerSystem.getBaseboard().getSerialNumber();
    }

    /**
     * 内存总大小
     * @param memory
     * @return
     */
    private static String getMemorySerial(GlobalMemory memory) {
        return String.valueOf(memory.getTotal());
    }

}
未经允许不得转载:正能量 » Java根据硬件信息生成唯一机器码

相关文章

评论 (0)

8 + 2 =