0

I have a application developed in Golang for IoT devices which communicates over MQTT, and this application can also be installed on any device that supports Docker and Golang.

Now I want to auto generate unique identity for my application for each device when I run my application for first time on a device. I was thinking about using permanent MAC or Serial Number, is that good approach and will all device have permanent MAC or Serial Number? If not then what is the better way to achieve this.

2
  • Does this ID has to be the same between restarts, so each time the app starts can identify the device? Commented Nov 19, 2021 at 8:23
  • @lewislbr yes, that's the exact requirement Commented Nov 19, 2021 at 8:57

3 Answers 3

2

Try to get some inspiration from the following code:

package main

import (
    "crypto/sha1"
    "encoding/hex"
    "fmt"
    "time"
)

// SHA1 hashes using sha1 algorithm
func SHA1(text string) string {
    algorithm := sha1.New()
    algorithm.Write([]byte(text))
    return hex.EncodeToString(algorithm.Sum(nil))
}

func main() {
    var macAddress = "00:00:00:00:00:00"
    var deviceType = "deviceType"
    var deviceName = "deviceName"
    var deviceModel = "deviceModel"
    var deviceManufacturer = "deviceManufacturer"
    var deviceVersion = "deviceVersion"
    var deviceSerialNumber = "deviceSerialNumber"
    var timeInMilliseconds = time.Now().UnixNano() / int64(time.Millisecond)
    // convert time to string
    var timeString = fmt.Sprintf("%d", timeInMilliseconds)
    var conc = macAddress + "-" + deviceType + "-" + deviceName + "-" + deviceModel + "-" + deviceManufacturer + "-" + deviceVersion + "-" + deviceSerialNumber + "-" + timeString

    // calculate the uuid using the sha256 algorithm
    // and the concatenated string
    var uuid = SHA1(conc)
    fmt.Println(uuid)

}

  • MAC addresses are primarily assigned by device manufacturers, and are therefore often referred to as the burned-in address, or as an Ethernet hardware address, hardware address, or physical address.
  • A serial number allows a company to identify a product and get additional information about it for replacement or to find compatible parts
Sign up to request clarification or add additional context in comments.

Comments

1

There's absolutely no guarantee that your device will even have have a serial number or MAC address, not to mention a unique one.

Regarding serials, every device manufacturer does its own thing. Those devices that have software-accessible serial numbers usually have them burnt into an EEPROM somewhere which requires special tools to read it. You'd need to know the procedure and run tools for each device you target.

Regarding MACs, if your device has a WiFi or Ethernet interface then the manufacturer probably has allocated a globally unique MAC address for it. However, it would be up to you to find the relevant network interface and read its MAC address. This would be rather cumbersome as you'd have to discover the system for network interfaces, determine if it's a physical interface (vs a virtual one like dial-up, bridge, vpn, etc) and read its MAC. Finally, some devices don't have any physical WiFi or Ethernet interfaces at all - they may come with a GSM module or LoRA or something else entirely.

I'd recommend not relying on MAC or serial. Generate your own GUID on first launch, store it in configuration and use it for subsequent identification.

PS - I'm assuming that you're targeting somewhat larger devices running Linux or other desktop OS. Microcontrollers generally don't support Go, and certainly not Docker.

2 Comments

Thanks for detailed answer @Tarmo, yes I'm targeting larger devices, like raspberry pi, kunbus
now problem is, if I go for GUID and store it in config during first installation then I will have it only untill I uninstall my application, but later if my application is uninstalled from device then I won't be able to have that GUID mapping for that device, in that case I need to find something that will be permanent identification for that device so that whenever I will install my application on same device next time, I should be able to use same ID
1

Read some crypto random data. Convert the random data to a string.

  // N is number of bytes of random data to 
  // to read. I set to N, the same number of 
  // bytes in a UUID.
  const N = 16

  p := make([]byte, N)
  if _, err := rand.Read(p); err != nil {
      // TODO: handle error
  }
  id := fmt.Sprintf("%x", p)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.