Search code examples
pythonpython-3.xuuiddicompydicom

Generate determinisitic UID dependent on the MAC adress and user name


I am writing a Python routine to generate DICOM files. In that case, I need to specify the Instance Creator UID tag, that is used to uniquely identify the device that created the DICOM file. So I would need a function that generates such a UID for a given machine and a specific user that launched the DICOM creation routine.

I guess that the UUID could be created from the MAC adress and the user name. However, UID generated with the uuid module in python are random, so the UID is different each time that the routine is called.

Is there a way to generate deterministic UID, that would depend on the MAC address and user name?


Solution

  • UUID3 and 5 are name-based uuids, so they wont change randomly (by time compontent).

    import uuid
    import getpass
    
    username = getpass.getuser(). # Username
    node = hex(uuid.getnode())  # MAC address
    
    urn = 'urn:node:%s:user:%s' % (node, username)
    
    for i in range(3):
        print (uuid.uuid3(uuid.NAMESPACE_DNS, urn))
    

    Output:

    4b3be55c-c6d9-3252-8b61-b16700fa6528
    4b3be55c-c6d9-3252-8b61-b16700fa6528
    4b3be55c-c6d9-3252-8b61-b16700fa6528
    

    For more information have a at https://pymotw.com/3/uuid/