Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate MAC address in Go

Tags:

go

I'm looking for an example on how to generate a MAC Address in Go. I have found many examples on creating UUIDs but nothing on MAC addresses.

Can someone help?

Thanks, Ben

like image 214
Shyrka Avatar asked Jan 09 '14 11:01

Shyrka


People also ask

How can I make a fake MAC address?

To spoof the address go to Control Panel>Network Connections. Then right click on the connection you want to spoof and select properties. Now go to the advanced tab and click on Network Address. Then select the black box and type the MAC address you want to have.


1 Answers

Here is how I would do it (playground)

import (
    "crypto/rand"
    "fmt"
)

func main() {
    buf := make([]byte, 6)
    _, err := rand.Read(buf)
    if err != nil {
        fmt.Println("error:", err)
        return
    }
    // Set the local bit
    buf[0] |= 2
    fmt.Printf("Random MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])
}

Note the setting of the local bit which means it won't clash with any globally administered addresses (see wikipedia for more info)

like image 71
Nick Craig-Wood Avatar answered Oct 11 '22 06:10

Nick Craig-Wood