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,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;
private final BasicHWInfoStrategy strategy;
private Log log;
public HWIdentifier(Log log) {
strategy = new BaseHWInfoImpl();
this.log = log;
}
public HWIdentifier(Log log) {
strategy = new BaseHWInfoImpl();
this.log = log;
}
public String getMAC() {
return strategy.getMAC().orElse("");
}
public String getHardwareHash() {
try {
String hwid = String.format("%1.16s-%1.16s-%1.16s", hashHWIdPart(strategy.getMAC()), hashHWIdPart(strategy.getHarddriveID()),
hashHWIdPart(strategy.getProcessorName()));
public String getHarddriveSerial() {
return strategy.getHarddriveID().orElse("");
}
//Fallback: computing a hash out of homepath+jarFileLocation
if ("null-null-null".equals(hwid)) {
log.debug("Hardware::loaded Hardware definitions fallback");
public String getProcessorName() {
return strategy.getProcessorName().orElse("");
}
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
public String getHardwareHash() {
byte[] hash;
String mac;
String cpuName;
String hdSerial;
hwid = hashHWIdPart(Optional.of(homeDir + clientPath));
}
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);
}
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<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");
}
}