1
2
3
4
5 package oshi.demo;
6
7 import java.util.Arrays;
8 import java.util.List;
9 import java.util.Locale;
10
11 import oshi.SystemInfo;
12 import oshi.annotation.SuppressForbidden;
13 import oshi.hardware.CentralProcessor;
14 import oshi.hardware.ComputerSystem;
15 import oshi.hardware.HardwareAbstractionLayer;
16 import oshi.software.os.OperatingSystem;
17 import oshi.util.Constants;
18
19
20
21
22
23 public class ComputerID {
24
25 public static final List<String> NON_UNIQUE_UUIDS = Arrays.asList("03000200-0400-0500-0006-000700080009",
26 "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF", "00000000-0000-0000-0000-000000000000");
27
28
29
30
31
32
33
34
35 @SuppressForbidden(reason = "Using System.out in a demo class")
36 public static void main(String[] args) {
37 String unknownHash = String.format(Locale.ROOT, "%08x", Constants.UNKNOWN.hashCode());
38
39 System.out.println("Here's a unique (?) id for your computer.");
40 System.out.println(getComputerIdentifier());
41 System.out.println("If any field is " + unknownHash
42 + " then I couldn't find a serial number or unique uuid, and running as sudo might change this.");
43 }
44
45
46
47
48
49
50
51
52
53
54 public static String getComputerIdentifier() {
55 SystemInfo systemInfo = new SystemInfo();
56 OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
57 HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
58 CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();
59 ComputerSystem computerSystem = hardwareAbstractionLayer.getComputerSystem();
60
61 String vendor = operatingSystem.getManufacturer();
62 String processorSerialNumber = computerSystem.getSerialNumber();
63 String uuid = computerSystem.getHardwareUUID();
64 if (NON_UNIQUE_UUIDS.contains(uuid.toUpperCase(Locale.ROOT))) {
65 uuid = Constants.UNKNOWN;
66 }
67 String processorIdentifier = centralProcessor.getProcessorIdentifier().getIdentifier();
68 int processors = centralProcessor.getLogicalProcessorCount();
69
70 String delimiter = "-";
71
72 return String.format(Locale.ROOT, "%08x", vendor.hashCode()) + delimiter
73 + String.format(Locale.ROOT, "%08x", processorSerialNumber.hashCode()) + delimiter
74 + String.format(Locale.ROOT, "%08x", uuid.hashCode()) + delimiter
75 + String.format(Locale.ROOT, "%08x", processorIdentifier.hashCode()) + delimiter + processors;
76 }
77 }