Nsuuid

From AOLserver Wiki
Jump to navigation Jump to search

Universally Unique IDentifiers (UUIDs)

John Havard implemented nsuuid, a C module which uses libuuid1 (part of e2fsprogs). It may be possible to link against OSSP's uuid implementation. http://www.ossp.org/pkg/lib/uuid/

Dossy provides a simple pure-Tcl solution here with RHS's help:

#
# ns_uuid_v4 --
#
# This generates Version 4 "random" UUIDs.
#
# http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-03.txt
#
#    The algorithm is as follows:
#    o  Set the two most significant bits (bits six and seven) of the
#       clock_seq_hi_and_reserved to zero and one, respectively.
#    o  Set the four most significant bits (bits 12 through 15) of the
#       time_hi_and_version field to the four-bit version number from
#       Section 4.1.3.
#    o  Set all the other bits to randomly (or pseudo-randomly) chosen
#       values.
#

proc ns_uuid_v4 {} {
    ns_uuid_gen [[binary format "IIII" \
        [expr {wide(0x0ffffffff*rand())}]] \
        [[expr {wide(0x0ffffffff*rand()) & 0xffff4fff}]] \
        [[expr {wide(0x0ffffffff*rand()) & 0x7fffffff}]] \
        [[expr {wide(0x0ffffffff*rand())}]]]
}

proc ns_uuid_gen {bits} {
    binary scan $bits "H8 H4 H4 H4 H12" a b c d e
    return "$a-$b-$c-$d-$e"
}