Home > database >  Best way to auto generate global unique identity for IoT devices
Best way to auto generate global unique identity for IoT devices

Time:11-19

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.

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

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.

  • Related