3

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?

BayesianMonk
  • 540
  • 7
  • 23
  • Please refer to [this](https://stackoverflow.com/a/46316162/5779732) and [this](https://stackoverflow.com/a/46316688/5779732) answers. Those discuss rules while generating DICOM UID and multiple algorithms to generate them. – Amit Joshi Mar 10 '20 at 10:26

1 Answers1

2

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/

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
  • 1
    While this is true, it does not help much with DICOM uids - these have another format, and also have to start with a vendor-specific prefix. You could probably use a hash value of MAC address and user name to generate the UID. – MrBean Bremen Mar 10 '20 at 09:53
  • Though, on the other hand, you could transfer that output to decimal numbers, convert it to the proper format, and prepend the prefix. Provided the resulting UID doesn't get to long, it is a good solution. – MrBean Bremen Mar 10 '20 at 10:00
  • 1
    @MrBeanBremen: [Here](https://stackoverflow.com/a/58011693/5779732) is how to transfer/convert it to DICOM UID using C#. Need to translate to python. – Amit Joshi Mar 10 '20 at 10:28
  • 1
    @AmitJoshi, nice! Should be easy to transform this to Python. – MrBean Bremen Mar 10 '20 at 10:37
  • 1
    @MrBeanBremen: this is indeed what I do, I append the generated UID to the vendor specific prefix, eg: `f"1.3.6.1.4.1.32839.1{uuid.uuid3(uuid.NAMESPACE_DNS, urn).int:039}"` – BayesianMonk Mar 10 '20 at 15:59