From 93d1dc5630919f53438ddb1710d9b76ecac37151 Mon Sep 17 00:00:00 2001 From: M*C*O Date: Sun, 22 Sep 2024 21:09:29 +0000 Subject: [PATCH] Improve HWIDs to be more human-readable --- .../client/hardware/hwid/HWIdentifier.java | 102 ++++++++---------- 1 file changed, 42 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/sheepit/client/hardware/hwid/HWIdentifier.java b/src/main/java/com/sheepit/client/hardware/hwid/HWIdentifier.java index dbd0481..9c3ec17 100644 --- a/src/main/java/com/sheepit/client/hardware/hwid/HWIdentifier.java +++ b/src/main/java/com/sheepit/client/hardware/hwid/HWIdentifier.java @@ -8,67 +8,49 @@ import java.math.BigInteger; import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Optional; public class HWIdentifier { - private final BasicHWInfoStrategy strategy; - private Log log; - - public HWIdentifier(Log log) { - strategy = new BaseHWInfoImpl(); - this.log = log; - } - - public String getMAC() { - return strategy.getMAC().orElse(""); - } - - public String getHarddriveSerial() { - return strategy.getHarddriveID().orElse(""); - } - - public String getProcessorName() { - return strategy.getProcessorName().orElse(""); - } - - public String getHardwareHash() { - byte[] hash; - String mac; - String cpuName; - String hdSerial; - - try { - MessageDigest digest = MessageDigest.getInstance("md5"); - if ((hdSerial = getHarddriveSerial()).length() > 0) { - hash = digest.digest(hdSerial.getBytes(StandardCharsets.UTF_8)); - log.debug("Hardware::loaded Hardware definitions ver 1.1"); - } - else if ((mac = getMAC()).length() > 0) { - hash = digest.digest(mac.getBytes(StandardCharsets.UTF_8)); - log.debug("Hardware::loaded Hardware definitions ver 1.2"); - } - else { //Fallback: computing a hash out of homepath+jarFileLocation+cpuName - log.debug("Hardware::loaded Hardware definitions ver 1.3"); - cpuName = getProcessorName(); - if (cpuName.isEmpty()) { - log.error("Hardware::failed to load Hardware definitions"); - throw new UnsupportedOperationException("Unable to load CPU information!"); - } - - String homeDir = System.getProperty("user.home"); //get home path - URL clientURL = getClass().getProtectionDomain().getCodeSource().getLocation(); - String clientPath = new File(clientURL.toString()).getParent(); //get jar file location - - hash = digest.digest((homeDir + clientPath + cpuName).getBytes(StandardCharsets.UTF_8)); - } - - BigInteger num = new BigInteger(1, hash); - return num.toString(16); - - } + private final BasicHWInfoStrategy strategy; + private Log log; + + public HWIdentifier(Log log) { + strategy = new BaseHWInfoImpl(); + this.log = log; + } + + public String getHardwareHash() { + try { + String hwid = String.format("%1.16s-%1.16s-%1.16s", hashHWIdPart(strategy.getMAC()), hashHWIdPart(strategy.getHarddriveID()), + hashHWIdPart(strategy.getProcessorName())); + + //Fallback: computing a hash out of homepath+jarFileLocation + if ("null-null-null".equals(hwid)) { + log.debug("Hardware::loaded Hardware definitions fallback"); + + String homeDir = System.getProperty("user.home"); //get home path + URL clientURL = getClass().getProtectionDomain().getCodeSource().getLocation(); + String clientPath = new File(clientURL.toString()).getParent(); //get jar file location + + hwid = hashHWIdPart(Optional.of(homeDir + clientPath)); + } + + return hwid; + } catch (Exception e) { - e.printStackTrace(); - log.error("HWIdentifier::getHardwareHash could not retrieve hash: " + e); - return "unknown"; - } - } + e.printStackTrace(); + log.error("HWIdentifier::getHardwareHash could not retrieve hash: " + e); + return "unknown"; + } + } + + private String hashHWIdPart(Optional part) throws NoSuchAlgorithmException { + MessageDigest digest = MessageDigest.getInstance("md5"); + + return part // + .map(s -> digest.digest(s.getBytes(StandardCharsets.UTF_8))) // + .map(b -> new BigInteger(1, b).toString(16)) // + .orElse("null"); + } }