Version 2 of Get UUID on *NIX

Updated 2018-06-02 14:20:00 by dbohdan

Created by CecilWesterhof.

Getting an UUID on a *NIX System

There are some problems with getting the UUID (type 4) on *NIX systems. For one they are not really random.

Because of this I created the following function:

proc getUUIDNix {{secure False}} {
    set fortyeightBits [expr {2 ** 48 - 1}]
    set sixteenBits    [expr {2 ** 16 - 1}]
    set thirtytwoBits  [expr {2 ** 32 - 1}]
    set twelfBits      [expr {2 ** 12 - 1}]

    format %.8x-%.4x-4%.3x-%x%.3x-%.12x \
        [getRandomIntInRangeNix 0 ${thirtytwoBits}] \
        [getRandomIntInRangeNix 0 ${sixteenBits}]   \
        [getRandomIntInRangeNix 0 ${twelfBits}]     \
        [getRandomIntInRangeNix 8 11]               \
        [getRandomIntInRangeNix 0 ${twelfBits}]     \
        [getRandomIntInRangeNix 0 ${fortyeightBits} False True]
}

It uses getRandomIntInRangeNix from Random Integers.


As always: comments, tips and questions are appreciated.

See also

  • UUID for other implementations

Discussion