2024-01-19 03:56:28 -05:00

27 lines
1009 B
Java

package net.server.coordinator.session;
import java.util.regex.Pattern;
public record Hwid(String hwid) {
private static final int HWID_LENGTH = 8;
// First part is a mac address (without dashes), second part is the hwid
private static final Pattern VALID_HOST_STRING_PATTERN = Pattern.compile("[0-9A-F]{12}_[0-9A-F]{8}");
private static boolean isValidHostString(String hostString) {
return VALID_HOST_STRING_PATTERN.matcher(hostString).matches();
}
public static Hwid fromHostString(String hostString) throws IllegalArgumentException {
if (hostString == null || !isValidHostString(hostString)) {
throw new IllegalArgumentException("hostString has invalid format");
}
final String[] split = hostString.split("_");
if (split.length != 2 || split[1].length() != HWID_LENGTH) {
throw new IllegalArgumentException("Hwid validation failed for hwid: " + hostString);
}
return new Hwid(split[1]);
}
}