Network Working Group Paul J. Leach, Microsoft INTERNET-DRAFT Rich Salz, Certco Category: Standards Track Expires August 4, 1998 February 4, 1998 UUIDs and GUIDs STATUS OF THIS MEMO This document is an Internet-Draft. Internet-Drafts are working documents of the Internet Engineering Task Force (IETF), its areas, and its working groups. Note that other groups may also distribute working documents as Internet-Drafts. Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as "work in progress". To learn the current status of any Internet-Draft, please check the "1id-abstracts.txt" listing contained in the Internet-Drafts Shadow Directories on ftp.is.co.za (Africa), nic.nordu.net (Europe), munnari.oz.au (Pacific Rim), ds.internic.net (US East Coast), or ftp.isi.edu (US West Coast). Distribution of this document is unlimited. Please send comments to the authors or the CIFS mailing list at . Discussions of the mailing list are archived at "-" "-" "-" "-" time_low = 4* time_mid = 2* time_high_and_version = 2* clock_seq_and_reserved = clock_seq_low = node = 6* hexDigit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F" The following is an example of the string representation of a UUID: f81d4fae-7dec-11d0-a765-00a0c91e6bf6 3.6 Comparing UUIDs for equality Consider each field of the UUID to be an unsigned integer as shown in the table in section 3.1. Then, to compare a pair of UUIDs, arithmetically compare the corresponding fields from each UUID in order of significance and according to their data type. Two UUIDs are equal if and only if all the corresponding fields are equal. Leach, Salz expires Aug 1998 [Page 12] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 Note: as a practical matter, on many systems comparison of two UUIDs for equality can be performed simply by comparing the 128 bits of their in-memory representation considered as a 128 bit unsigned integer. Here, it is presumed that by the time the in-memory representation is obtained the appropriate byte-order canonicalizations have been carried out. 3.7 Comparing UUIDs for relative order Two UUIDs allocated according to the same variant can also be ordered lexicographically. For the UUID variant herein defined, the first of two UUIDs follows the second if the most significant field in which the UUIDs differ is greater for the first UUID. The first of a pair of UUIDs precedes the second if the most significant field in which the UUIDs differ is greater for the second UUID. 3.8 Byte order of UUIDs UUIDs may be transmitted in many different forms, some of which may be dependent on the presentation or application protocol where the UUID may be used. In such cases, the order, sizes and byte orders of the UUIDs fields on the wire will depend on the relevant presentation or application protocol. However, it is strongly RECOMMENDED that the order of the fields conform with ordering set out in section 3.1 above. Furthermore, the payload size of each field in the application or presentation protocol MUST be large enough that no information lost in the process of encoding them for transmission. In the absence of explicit application or presentation protocol specification to the contrary, a UUID is encoded as a 128-bit object, as follows: the fields are encoded as 16 octets, with the sizes and order of the fields defined in section 3.1, and with each field encoded with the Most Significant Byte first (also known as network byte order). 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | time_low | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | time_mid | time_hi_and_version | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |clk_seq_hi_res | clk_seq_low | node (0-1) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | node (2-5) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Leach, Salz expires Aug 1998 [Page 13] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 4. Node IDs when no IEEE 802 network card is available If a system wants to generate UUIDs but has no IEE 802 compliant network card or other source of IEEE 802 addresses, then this section describes how to generate one. The ideal solution is to obtain a 47 bit cryptographic quality random number, and use it as the low 47 bits of the node ID, with the most significant bit of the first octet of the node ID set to 1. This bit is the unicast/multicast bit, which will never be set in IEEE 802 addresses obtained from network cards; hence, there can never be a conflict between UUIDs generated by machines with and without network cards. If a system does not have a primitive to generate cryptographic quality random numbers, then in most systems there are usually a fairly large number of sources of randomness available from which one can be generated. Such sources are system specific, but often include: - the percent of memory in use - the size of main memory in bytes - the amount of free main memory in bytes - the size of the paging or swap file in bytes - free bytes of paging or swap file - the total size of user virtual address space in bytes - the total available user address space bytes - the size of boot disk drive in bytes - the free disk space on boot drive in bytes - the current time - the amount of time since the system booted - the individual sizes of files in various system directories - the creation, last read, and modification times of files in various system directories - the utilization factors of various system resources (heap, etc.) - current mouse cursor position - current caret position - current number of running processes, threads - handles or IDs of the desktop window and the active window - the value of stack pointer of the caller - the process and thread ID of caller - various processor architecture specific performance counters (instructions executed, cache misses, TLB misses) (Note that it precisely the above kinds of sources of randomness that are used to seed cryptographic quality random number generators on systems without special hardware for their construction.) In addition, items such as the computer's name and the name of the operating system, while not strictly speaking random, will help differentiate the results from those obtained by other systems. The exact algorithm to generate a node ID using these data is system specific, because both the data available and the functions to obtain Leach, Salz expires Aug 1998 [Page 14] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 them are often very system specific. However, assuming that one can concatenate all the values from the randomness sources into a buffer, and that a cryptographic hash function such as MD5 [3] is available, then any 6 bytes of the MD5 hash of the buffer, with the multicast bit (the high bit of the first byte) set will be an appropriately random node ID. Other hash functions, such as SHA-1 [4], can also be used. The only requirement is that the result be suitably random _ in the sense that the outputs from a set uniformly distributed inputs are themselves uniformly distributed, and that a single bit change in the input can be expected to cause half of the output bits to change. 5. Obtaining IEEE 802 addresses At the time of writing, the following URL http://standards.ieee.org/db/oui/forms/ contains information on how to obtain an IEEE 802 address block. At the time of writing, the cost is $1250 US. 6. Security Considerations It should not be assumed that UUIDs are hard to guess; they should not be used as capabilities. 7. Acknowledgements This document draws heavily on the OSF DCE specification for UUIDs. Ted Ts'o provided helpful comments, especially on the byte ordering section which we mostly plagiarized from a proposed wording he supplied (all errors in that section are our responsibility, however). 8. References [1] Lisa Zahn, et. al., Network Computing Architecture, Prentice Hall, Englewood Cliffs, NJ, 1990 [2] DCE: Remote Procedure Call, Open Group CAE Specification C309 ISBN 1-85912-041-5 28cm. 674p. pbk. 1,655g. 8/94 [3] R. Rivest, RFC 1321, "The MD5 Message-Digest Algorithm", 04/16/1992. [4] NIST FIPS PUB 180-1, "Secure Hash Standard," National Institute of Standards and Technology, U.S. Department of Commerce, DRAFT, May 31, 1994. Leach, Salz expires Aug 1998 [Page 15] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 9. Authors' addresses Paul J. Leach Microsoft 1 Microsoft Way Redmond, WA, 98052, U.S.A. paulle@microsoft.com Tel. 425 882 8080 Fax. 425 936 7329 Rich Salz 100 Cambridge Park Drive Cambridge MA 02140 salzr@certco.com Tel. 617 499 4075 Fax. 617 576 0019 10. Notice The IETF takes no position regarding the validity or scope of any intellectual property or other rights that might be claimed to pertain to the implementation or use of the technology described in this document or the extent to which any license under such rights might or might not be available; neither does it represent that it has made any effort to identify any such rights. Information on the IETF's procedures with respect to rights in standards-track and standards-related documentation can be found in BCP-11. Copies of claims of rights made available for publication and any assurances of licenses to be made available, or the result of an attempt made to obtain a general license or permission for the use of such proprietary rights by implementors or users of this specification can be obtained from the IETF Secretariat. The IETF invites any interested party to bring to its attention any copyrights, patents or patent applications, or other proprietary rights which may cover technology that may be required to practice this standard. Please address the information to the IETF Executive Director. 11. Full Copyright Statement Copyright (C) The Internet Society 1997. All Rights Reserved. This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way, such as by removing the copyright notice or references to the Internet Society or other Internet organizations, except as needed for the purpose of Leach, Salz expires Aug 1998 [Page 16] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 developing Internet standards in which case the procedures for copyrights defined in the Internet Standards process must be followed, or as required to translate it into languages other than English. The limited permissions granted above are perpetual and will not be revoked by the Internet Society or its successors or assigns. This document and the information contained herein is provided on an "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Appendix A _ UUID Sample Implementation This implementation consists of 5 files: uuid.h, uuid.c, sysdep.h, sysdep.c and utest.c. The uuid.* files are the system independent implementation of the UUID generation algorithms described above, with all the optimizations described above except efficient state sharing across processes included. The code has been tested on Linux (Red Hat 4.0) with GCC (2.7.2), and Windows NT 4.0 with VC++ 5.0. The code assumes 64 bit integer support, which makes it a lot clearer. All the following source files should be considered to have the following copyright notice included: copyrt.h /* ** Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc. ** Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. & ** Digital Equipment Corporation, Maynard, Mass. ** Copyright (c) 1998 Microsoft. ** To anyone who acknowledges that this file is provided "AS IS" ** without any express or implied warranty: permission to use, copy, ** modify, and distribute this file for any purpose is hereby ** granted without fee, provided that the above copyright notices and ** this notice appears in all source code copies, and that none of ** the names of Open Software Foundation, Inc., Hewlett-Packard ** Company, or Digital Equipment Corporation be used in advertising ** or publicity pertaining to distribution of the software without ** specific, written prior permission. Neither Open Software ** Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital Equipment ** Corporation makes any representations about the suitability of ** this software for any purpose. */ uuid.h Leach, Salz expires Aug 1998 [Page 17] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 #include "copyrt.h" #undef uuid_t typedef struct _uuid_t { unsigned32 time_low; unsigned16 time_mid; unsigned16 time_hi_and_version; unsigned8 clock_seq_hi_and_reserved; unsigned8 clock_seq_low; byte node[6]; } uuid_t; /* uuid_create -- generate a UUID */ int uuid_create(uuid_t * uuid); /* uuid_create_from_name -- create a UUID using a "name" from a "name space" */ void uuid_create_from_name( uuid_t * uuid, /* resulting UUID */ uuid_t nsid, /* UUID to serve as context, so identical names from different name spaces generate different UUIDs */ void * name, /* the name from which to generate a UUID */ int namelen /* the length of the name */ ); /* uuid_compare -- Compare two UUID's "lexically" and return -1 u1 is lexically before u2 0 u1 is equal to u2 1 u1 is lexically after u2 Note: lexical ordering is not temporal ordering! */ int uuid_compare(uuid_t *u1, uuid_t *u2); uuid.c #include "copyrt.h" #include #include #include #include #include "sysdep.h" #include "uuid.h" /* various forward declarations */ static int read_state(unsigned16 *clockseq, uuid_time_t *timestamp, uuid_node_t * node); static void write_state(unsigned16 clockseq, uuid_time_t timestamp, uuid_node_t node); static void format_uuid_v1(uuid_t * uuid, unsigned16 clockseq, uuid_time_t timestamp, uuid_node_t node); static void format_uuid_v3(uuid_t * uuid, unsigned char hash[16]); static void get_current_time(uuid_time_t * timestamp); static unsigned16 true_random(void); Leach, Salz expires Aug 1998 [Page 18] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 /* uuid_create -- generator a UUID */ int uuid_create(uuid_t * uuid) { uuid_time_t timestamp, last_time; unsigned16 clockseq; uuid_node_t node; uuid_node_t last_node; int f; /* acquire system wide lock so we're alone */ LOCK; /* get current time */ get_current_time(×tamp); /* get node ID */ get_ieee_node_identifier(&node); /* get saved state from NV storage */ f = read_state(&clockseq, &last_time, &last_node); /* if no NV state, or if clock went backwards, or node ID changed (e.g., net card swap) change clockseq */ if (!f || memcmp(&node, &last_node, sizeof(uuid_node_t))) clockseq = true_random(); else if (timestamp < last_time) clockseq++; /* stuff fields into the UUID */ format_uuid_v1(uuid, clockseq, timestamp, node); /* save the state for next time */ write_state(clockseq, timestamp, node); UNLOCK; return(1); }; /* format_uuid_v1 -- make a UUID from the timestamp, clockseq, and node ID */ void format_uuid_v1(uuid_t * uuid, unsigned16 clock_seq, uuid_time_t timestamp, uuid_node_t node) { /* Construct a version 1 uuid with the information we've gathered * plus a few constants. */ uuid->time_low = (unsigned long)(timestamp & 0xFFFFFFFF); uuid->time_mid = (unsigned short)((timestamp >> 32) & 0xFFFF); uuid->time_hi_and_version = (unsigned short)((timestamp >> 48) & 0x0FFF); uuid->time_hi_and_version |= (1 << 12); uuid->clock_seq_low = clock_seq & 0xFF; uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8; uuid->clock_seq_hi_and_reserved |= 0x80; memcpy(&uuid->node, &node, sizeof uuid->node); }; Leach, Salz expires Aug 1998 [Page 19] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 /* data type for UUID generator persistent state */ typedef struct { uuid_time_t ts; /* saved timestamp */ uuid_node_t node; /* saved node ID */ unsigned16 cs; /* saved clock sequence */ } uuid_state; static uuid_state st; /* read_state -- read UUID generator state from non-volatile store */ int read_state(unsigned16 *clockseq, uuid_time_t *timestamp, uuid_node_t *node) { FILE * fd; static int inited = 0; /* only need to read state once per boot */ if (!inited) { fd = fopen("state", "rb"); if (!fd) return (0); fread(&st, sizeof(uuid_state), 1, fd); fclose(fd); inited = 1; }; *clockseq = st.cs; *timestamp = st.ts; *node = st.node; return(1); }; /* write_state -- save UUID generator state back to non-volatile storage */ void write_state(unsigned16 clockseq, uuid_time_t timestamp, uuid_node_t node) { FILE * fd; static int inited = 0; static uuid_time_t next_save; if (!inited) { next_save = timestamp; inited = 1; }; /* always save state to volatile shared state */ st.cs = clockseq; st.ts = timestamp; st.node = node; if (timestamp >= next_save) { fd = fopen("state", "wb"); fwrite(&st, sizeof(uuid_state), 1, fd); fclose(fd); /* schedule next save for 10 seconds from now */ next_save = timestamp + (10 * 10 * 1000 * 1000); }; }; Leach, Salz expires Aug 1998 [Page 20] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 /* get-current_time -- get time as 60 bit 100ns ticks since whenever. Compensate for the fact that real clock resolution is less than 100ns. */ void get_current_time(uuid_time_t * timestamp) { uuid_time_t time_now; static uuid_time_t time_last; static unsigned16 uuids_this_tick; static int inited = 0; if (!inited) { get_system_time(&time_now); uuids_this_tick = UUIDS_PER_TICK; inited = 1; }; while (1) { get_system_time(&time_now); /* if clock reading changed since last UUID generated... */ if (time_last != time_now) { /* reset count of uuids gen'd with this clock reading */ uuids_this_tick = 0; break; }; if (uuids_this_tick < UUIDS_PER_TICK) { uuids_this_tick++; break; }; /* going too fast for our clock; spin */ }; /* add the count of uuids to low order bits of the clock reading */ *timestamp = time_now + uuids_this_tick; }; /* true_random -- generate a crypto-quality random number. This sample doesn't do that. */ static unsigned16 true_random(void) { static int inited = 0; uuid_time_t time_now; if (!inited) { get_system_time(&time_now); time_now = time_now/UUIDS_PER_TICK; srand((unsigned int)(((time_now >> 32) ^ time_now)&0xffffffff)); inited = 1; }; return (rand()); } Leach, Salz expires Aug 1998 [Page 21] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 /* uuid_create_from_name -- create a UUID using a "name" from a "name space" */ void uuid_create_from_name( uuid_t * uuid, /* resulting UUID */ uuid_t nsid, /* UUID to serve as context, so identical names from different name spaces generate different UUIDs */ void * name, /* the name from which to generate a UUID */ int namelen /* the length of the name */ ) { MD5_CTX c; unsigned char hash[16]; uuid_t net_nsid; /* context UUID in network byte order */ /* put name space ID in network byte order so it hashes the same no matter what endian machine we're on */ net_nsid = nsid; htonl(net_nsid.time_low); htons(net_nsid.time_mid); htons(net_nsid.time_hi_and_version); MD5Init(&c); MD5Update(&c, &net_nsid, sizeof(uuid_t)); MD5Update(&c, name, namelen); MD5Final(hash, &c); /* the hash is in network byte order at this point */ format_uuid_v3(uuid, hash); }; /* format_uuid_v3 -- make a UUID from a (pseudo)random 128 bit number */ void format_uuid_v3(uuid_t * uuid, unsigned char hash[16]) { /* Construct a version 3 uuid with the (pseudo-)random number * plus a few constants. */ memcpy(uuid, hash, sizeof(uuid_t)); /* convert UUID to local byte order */ ntohl(uuid->time_low); ntohs(uuid->time_mid); ntohs(uuid->time_hi_and_version); /* put in the variant and version bits */ uuid->time_hi_and_version &= 0x0FFF; uuid->time_hi_and_version |= (3 << 12); uuid->clock_seq_hi_and_reserved &= 0x3F; uuid->clock_seq_hi_and_reserved |= 0x80; }; /* uuid_compare -- Compare two UUID's "lexically" and return -1 u1 is lexically before u2 0 u1 is equal to u2 1 u1 is lexically after u2 Leach, Salz expires Aug 1998 [Page 22] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 Note: lexical ordering is not temporal ordering! */ int uuid_compare(uuid_t *u1, uuid_t *u2) { int i; #define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1; CHECK(u1->time_low, u2->time_low); CHECK(u1->time_mid, u2->time_mid); CHECK(u1->time_hi_and_version, u2->time_hi_and_version); CHECK(u1->clock_seq_hi_and_reserved, u2->clock_seq_hi_and_reserved); CHECK(u1->clock_seq_low, u2->clock_seq_low) for (i = 0; i < 6; i++) { if (u1->node[i] < u2->node[i]) return -1; if (u1->node[i] > u2->node[i]) return 1; } return 0; }; sysdep.h #include "copyrt.h" /* remove the following define if you aren't running WIN32 */ #define WININC 0 #ifdef WININC #include #else #include #include #include #endif /* change to point to where MD5 .h's live */ /* get MD5 sample implementation from RFC 1321 */ #include "global.h" #include "md5.h" /* set the following to the number of 100ns ticks of the actual resolution of your system's clock */ #define UUIDS_PER_TICK 1024 /* Set the following to a call to acquire a system wide global lock */ #define LOCK #define UNLOCK typedef unsigned long unsigned32; typedef unsigned short unsigned16; typedef unsigned char unsigned8; typedef unsigned char byte; Leach, Salz expires Aug 1998 [Page 23] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 /* Set this to what your compiler uses for 64 bit data type */ #ifdef WININC #define unsigned64_t unsigned __int64 #define I64(C) C #else #define unsigned64_t unsigned long long #define I64(C) C##LL #endif typedef unsigned64_t uuid_time_t; typedef struct { char nodeID[6]; } uuid_node_t; void get_ieee_node_identifier(uuid_node_t *node); void get_system_time(uuid_time_t *uuid_time); void get_random_info(char seed[16]); sysdep.c #include "copyrt.h" #include #include "sysdep.h" /* system dependent call to get IEEE node ID. This sample implementation generates a random node ID */ void get_ieee_node_identifier(uuid_node_t *node) { char seed[16]; FILE * fd; static inited = 0; static uuid_node_t saved_node; if (!inited) { fd = fopen("nodeid", "rb"); if (fd) { fread(&saved_node, sizeof(uuid_node_t), 1, fd); fclose(fd); } else { get_random_info(seed); seed[0] |= 0x80; memcpy(&saved_node, seed, sizeof(uuid_node_t)); fd = fopen("nodeid", "wb"); if (fd) { fwrite(&saved_node, sizeof(uuid_node_t), 1, fd); fclose(fd); }; }; inited = 1; }; Leach, Salz expires Aug 1998 [Page 24] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 *node = saved_node; }; /* system dependent call to get the current system time. Returned as 100ns ticks since Oct 15, 1582, but resolution may be less than 100ns. */ #ifdef _WINDOWS_ void get_system_time(uuid_time_t *uuid_time) { ULARGE_INTEGER time; GetSystemTimeAsFileTime((FILETIME *)&time); /* NT keeps time in FILETIME format which is 100ns ticks since Jan 1, 1601. UUIDs use time in 100ns ticks since Oct 15, 1582. The difference is 17 Days in Oct + 30 (Nov) + 31 (Dec) + 18 years and 5 leap days. */ time.QuadPart += (unsigned __int64) (1000*1000*10) // seconds * (unsigned __int64) (60 * 60 * 24) // days * (unsigned __int64) (17+30+31+365*18+5); // # of days *uuid_time = time.QuadPart; }; void get_random_info(char seed[16]) { MD5_CTX c; typedef struct { MEMORYSTATUS m; SYSTEM_INFO s; FILETIME t; LARGE_INTEGER pc; DWORD tc; DWORD l; char hostname[MAX_COMPUTERNAME_LENGTH + 1]; } randomness; randomness r; MD5Init(&c); /* memory usage stats */ GlobalMemoryStatus(&r.m); /* random system stats */ GetSystemInfo(&r.s); /* 100ns resolution (nominally) time of day */ GetSystemTimeAsFileTime(&r.t); /* high resolution performance counter */ QueryPerformanceCounter(&r.pc); /* milliseconds since last boot */ r.tc = GetTickCount(); r.l = MAX_COMPUTERNAME_LENGTH + 1; Leach, Salz expires Aug 1998 [Page 25] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 GetComputerName(r.hostname, &r.l ); MD5Update(&c, &r, sizeof(randomness)); MD5Final(seed, &c); }; #else void get_system_time(uuid_time_t *uuid_time) { struct timeval tp; gettimeofday(&tp, (struct timezone *)0); /* Offset between UUID formatted times and Unix formatted times. UUID UTC base time is October 15, 1582. Unix base time is January 1, 1970. */ *uuid_time = (tp.tv_sec * 10000000) + (tp.tv_usec * 10) + I64(0x01B21DD213814000); }; void get_random_info(char seed[16]) { MD5_CTX c; typedef struct { struct sysinfo s; struct timeval t; char hostname[257]; } randomness; randomness r; MD5Init(&c); sysinfo(&r.s); gettimeofday(&r.t, (struct timezone *)0); gethostname(r.hostname, 256); MD5Update(&c, &r, sizeof(randomness)); MD5Final(seed, &c); }; #endif utest.c #include "copyrt.h" #include "sysdep.h" #include #include "uuid.h" uuid_t NameSpace_DNS = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */ 0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }; Leach, Salz expires Aug 1998 [Page 26] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 /* puid -- print a UUID */ void puid(uuid_t u); /* Simple driver for UUID generator */ void main(int argc, char **argv) { uuid_t u; int f; uuid_create(&u); printf("uuid_create() -> "); puid(u); f = uuid_compare(&u, &u); printf("uuid_compare(u,u): %d\n", f); /* should be 0 */ f = uuid_compare(&u, &NameSpace_DNS); printf("uuid_compare(u, NameSpace_DNS): %d\n", f); /* s.b. 1 */ f = uuid_compare(&NameSpace_DNS, &u); printf("uuid_compare(NameSpace_DNS, u): %d\n", f); /* s.b. -1 */ uuid_create_from_name(&u, NameSpace_DNS, "www.widgets.com", 15); printf("uuid_create_from_name() -> "); puid(u); }; void puid(uuid_t u) { int i; printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid, u.time_hi_and_version, u.clock_seq_hi_and_reserved, u.clock_seq_low); for (i = 0; i < 6; i++) printf("%2.2x", u.node[i]); printf("\n"); }; Appendix B _ Sample output of utest uuid_create() -> 7d444840-9dc0-11d1-b245-5ffdce74fad2 uuid_compare(u,u): 0 uuid_compare(u, NameSpace_DNS): 1 uuid_compare(NameSpace_DNS, u): -1 uuid_create_from_name() -> e902893a-9d22-3c7e-a7b8-d6e313b71d9f Appendix C _ Some name space IDs This appendix lists the name space IDs for some potentially interesting name spaces, as initialized C structures and in the string representation defined in section 3.5 uuid_t NameSpace_DNS = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */ 0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }; Leach, Salz expires Aug 1998 [Page 27] Internet-Draft UUIDs and GUIDs (DRAFT) 02/04/98 uuid_t NameSpace_URL = { /* 6ba7b811-9dad-11d1-80b4-00c04fd430c8 */ 0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }; uuid_t NameSpace_OID = { /* 6ba7b812-9dad-11d1-80b4-00c04fd430c8 */ 0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }; uuid_t NameSpace_X500 = { /* 6ba7b814-9dad-11d1-80b4-00c04fd430c8 */ 0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 };