Shorter implementation of random_ether_addr().

This commit is contained in:
Philipp Marek 2015-10-07 11:09:00 +02:00 committed by Dietmar Maurer
parent e88f9c631c
commit de9a267fec

View File

@ -940,21 +940,13 @@ sub random_ether_addr {
my $rand = Digest::SHA::sha1_hex($$, rand(), $seconds, $microseconds);
my $mac = '';
for (my $i = 0; $i < 6; $i++) {
my $ss = hex(substr($rand, $i*2, 2));
if (!$i) {
$ss &= 0xfe; # clear multicast
$ss |= 2; # set local id
}
$ss = sprintf("%02X", $ss);
# clear multicast, set local id
vec($rand, 0, 8) = (vec($rand, 0, 8) & 0xfe) | 2;
if (!$i) {
$mac .= "$ss";
} else {
$mac .= ":$ss";
}
}
my $mac = sprintf("%02X:" x 6, unpack("C6", $rand));
# remove superfluous ":" at end
chop($mac);
return $mac;
}