Skip to content

Instantly share code, notes, and snippets.

@spiritedRunning
Created January 14, 2024 16:18
Show Gist options
  • Save spiritedRunning/02e9027298fd5fc1da9d8a8542ff0d6c to your computer and use it in GitHub Desktop.
Save spiritedRunning/02e9027298fd5fc1da9d8a8542ff0d6c to your computer and use it in GitHub Desktop.
mac address
// generate random MAC address
public static String randomMACAddress() {
Random rand = new Random();
byte[] macAddr = new byte[6];
rand.nextBytes(macAddr);
macAddr[0] = (byte) (macAddr[0] & (byte) 254);
StringBuilder sb = new StringBuilder(18);
for (byte b : macAddr) {
if (sb.length() > 0)
sb.append(":");
sb.append(String.format("%02x", b));
}
return sb.toString();
}
// check MAC
public static boolean checkMacAvailable(String macAddress) {
String patternMac = "^[a-f0-9]{2}(:[a-f0-9]{2}){5}$";
return Pattern.compile(patternMac).matcher(macAddress).find();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment