root/trunk/tango/scrapple/util/uuid/NameUuidGen.d

Revision 56, 3.8 kB (checked in by maxter, 8 months ago)

Removed the time-based generator, sys and ipc packages. Added more API documentation.

Line 
1 /*******************************************************************************
2
3         copyright:      Copyright (c) 2007 Max Samukha. All rights reserved
4
5         license:        BSD style: $(LICENSE)
6
7         authors:         Max Samukha
8
9         With the functions in this module, UUIDs can be created from names
10         that are unique within a name space.
11
12         MD-5 or SHA-1 hashing algorithms can be used to generate a name-based UUID.
13         If backward compatibility is not an issue, SHA-1 should be preferred.
14
15         The functions are thread-safe.
16
17         Examples:
18 ----
19 auto uuid = newUuidSha1(NameSpaceUuids.url, "http://www.dsource.org");
20 ----
21
22
23 *******************************************************************************/
24 module tango.scrapple.util.uuid.NameUuidGen;
25
26 public import tango.scrapple.util.uuid.Uuid;
27 import tango.io.digest.Sha1;
28 import tango.io.digest.Md5;
29
30 private Uuid newUuid(Uuid nsUuid, char[] name, Digest digest, UuidVersion ver)
31 {
32     assert(name.length > 0);
33
34     ubyte[16] nsOctets;
35     nsUuid.toOctets(nsOctets);
36
37     digest.update(nsOctets);
38     digest.update(cast(ubyte[])name);
39
40     Uuid uuid = digest.binaryDigest;
41     uuid.versionTimeHigh = (uuid.versionTimeHigh & 0x0FFF) | (ver << 12);
42     uuid.variantClockSeqHigh = (uuid.variantClockSeqHigh & 0x3F) | 0x80;
43
44     return uuid;
45 }
46
47 /**
48     Generates a _name-based UUID using SHA-1 hashing algorithm (version 5 UUID).
49
50     Params:
51         nsUuid  = namespace UUID.
52         name    = _name from which to generate.
53
54     Returns: The generated UUID.
55 */
56 Uuid newUuidSha1(Uuid nsUuid, char[] name)
57 {
58    scope digest = new Sha1();
59    return newUuid(nsUuid, name, digest, UuidVersion.NameBasedSha1);
60 }
61
62 /**
63     Generates a _name-based UUID using MD-5 hashing algorithm (version 3 UUID).
64
65     Params:
66         nsUuid  = namespace UUID.
67         name    = name from which to generate.
68
69     Returns: The generated UUID.
70 */
71 Uuid newUuidMd5(Uuid nsUuid, char[] name)
72 {
73    scope digest = new Md5();
74    return newUuid(nsUuid, name, digest, UuidVersion.NameBasedMd5);
75 }
76
77 /**
78     A subset of commonly used namespace UUIDs
79 */
80 struct NameSpaceUuids
81 {
82     static const
83     {
84         ///
85         Uuid dns = { 0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, [cast(ubyte)0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8] };
86
87         ///
88         Uuid url = { 0x6ba7b811, 0x9dad, 0x11d1, 0x80,  0xb4, [cast(ubyte)0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8] };
89
90         ///
91         Uuid oid = { 0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, [cast(ubyte)0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8] };
92
93         ///
94         Uuid x500 = { 0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, [cast(ubyte)0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8] };
95     }
96 }
97
98 debug (UuidUnitTest)
99 {
100     import tango.io.Stdout;
101
102     unittest
103     {
104         const char[] TestUuidStr = "24bb3c5e-988b-4833-9d9a-77bf84b3284c";
105         const char[] TestName = "TestUuidStr";
106
107         Uuid nsUuid = TestUuidStr;
108
109         // SHA-1
110         auto uuid = newUuidSha1(nsUuid, TestName);
111         auto uuid2 = newUuidSha1(nsUuid, TestName);
112
113         assert(uuid == uuid2);
114         assert(uuid.ver == UuidVersion.NameBasedSha1);
115         assert(uuid.variant == UuidVariant.Standard);
116
117         // MD5
118         uuid = newUuidMd5(nsUuid, TestName);
119         uuid2 = newUuidMd5(nsUuid, TestName);
120
121         assert(uuid == uuid2);
122         assert(uuid.ver == UuidVersion.NameBasedMd5);
123         assert(uuid.variant == UuidVariant.Standard);
124
125
126         // Namespace uuids
127         assert (NameSpaceUuids.dns  == Uuid("6ba7b810-9dad-11d1-80b4-00c04fd430c8"));
128         assert (NameSpaceUuids.url  == Uuid("6ba7b811-9dad-11d1-80b4-00c04fd430c8"));
129         assert (NameSpaceUuids.oid  == Uuid("6ba7b812-9dad-11d1-80b4-00c04fd430c8"));
130         assert (NameSpaceUuids.x500 == Uuid("6ba7b814-9dad-11d1-80b4-00c04fd430c8"));
131
132     }
133 }
Note: See TracBrowser for help on using the browser.