View Javadoc
1   /*
2    * Copyright 2019-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
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   * Attempts to create a unique computer identifier. Note that serial numbers won't work on Linux without user
21   * cooperation.
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       * <p>
30       * main.
31       * </p>
32       *
33       * @param args an array of {@link java.lang.String} objects.
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       * Generates a Computer Identifier, which may be part of a strategy to construct a licence key. (The identifier may
47       * not be unique as in one case hashcode could be same for multiple values, and the result may differ based on
48       * whether the program is running with sudo/root permission.) The identifier string is based upon the processor
49       * serial number, vendor, system UUID, processor identifier, and total processor count.
50       *
51       * @return A string containing four hyphen-delimited fields representing the processor; the first 3 are 32-bit
52       *         hexadecimal values and the last one is an integer value.
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  }