Improve HWIDs to be more human-readable

This commit is contained in:
M*C*O
2024-09-22 21:09:29 +00:00
committed by Sheepit Renderfarm
parent 0ff9689cd1
commit 93d1dc5630

View File

@@ -8,6 +8,8 @@ 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;
@@ -18,52 +20,23 @@ public class HWIdentifier {
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 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
hash = digest.digest((homeDir + clientPath + cpuName).getBytes(StandardCharsets.UTF_8));
hwid = hashHWIdPart(Optional.of(homeDir + clientPath));
}
BigInteger num = new BigInteger(1, hash);
return num.toString(16);
return hwid;
}
catch (Exception e) {
e.printStackTrace();
@@ -71,4 +44,13 @@ public class HWIdentifier {
return "unknown";
}
}
private String hashHWIdPart(Optional<String> 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");
}
}