uuid.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. {
  2. This file is part of the Free Pascal packages.
  3. Copyright (c) 1999-2006 by the Free Pascal development team
  4. Implements a UUID generation algorithm (RFC 4122)
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit uuid;
  12. interface
  13. {$mode objfpc}
  14. {$h+}
  15. uses
  16. SysUtils, DateUtils, md5, sha1;
  17. (******************************************************************************
  18. * types and constants
  19. ******************************************************************************)
  20. type
  21. {$ifdef VER2_0}
  22. uuid_t = packed record
  23. case integer of
  24. 1 : (
  25. Data1 : DWord;
  26. Data2 : word;
  27. Data3 : word;
  28. Data4 : array[0..7] of byte;
  29. );
  30. 2 : (
  31. D1 : DWord;
  32. D2 : word;
  33. D3 : word;
  34. D4 : array[0..7] of byte;
  35. );
  36. 3 : ( { uuid fields according to RFC4122 }
  37. time_low : dword; // The low field of the timestamp
  38. time_mid : word; // The middle field of the timestamp
  39. time_hi_and_version : word; // The high field of the timestamp multiplexed with the version
  40. clock_seq_hi_and_reserved : byte; // The high field of the clock sequence multiplexed with the var
  41. clock_seq_low : byte; // The low field of the clock sequence
  42. node : array[0..5] of byte; // The spatially unique node identifier
  43. );
  44. end;
  45. {$else VER2_0}
  46. uuid_t = TGuid;
  47. {$endif VER2_0}
  48. uuid_time_t = qword;
  49. uuid_node_t = array[0..5] of byte;
  50. unsigned16 = word;
  51. uuid_state = record
  52. ts : uuid_time_t; // saved timestamp
  53. node : uuid_node_t; // saved node ID
  54. cs : unsigned16; // saved clock sequence
  55. end;
  56. const
  57. UUID_VERSION_1 = $1; // The time-based version specified in this document.
  58. UUID_VERSION_2 = $2; // DCE Security version, with embedded POSIX UIDs.
  59. UUID_VERSION_3 = $3; // The name-based version specified in this document that uses MD5 hashing.
  60. UUID_VERSION_4 = $4; // The randomly or pseudo-randomly generated version specified in this document.
  61. UUID_VERSION_5 = $5; // The name-based version specified in this document that uses SHA-1 hashing.
  62. { set the following to the number of 100ns ticks of the actual resolution of your system's clock }
  63. UUIDS_PER_TICK = 1024;
  64. (******************************************************************************
  65. * core uuid functions
  66. ******************************************************************************)
  67. { uuid_initialize -- used to initialize the uuid_create function }
  68. procedure uuid_initialize(const state: uuid_state);
  69. { uuid_create -- generator a UUID }
  70. function uuid_create(out uuid: uuid_t): boolean;
  71. { uuid_finalize -- returns the current state }
  72. procedure uuid_finalize(out state: uuid_state);
  73. { uuid_create_md5_from_name -- create a version 3 (MD5) UUID using a "name" from a "name space" }
  74. procedure uuid_create_md5_from_name(out uuid: uuid_t; const nsid: uuid_t; const name: string);
  75. { uuid_create_sha1_from_name -- create a version 5 (SHA-1) UUID using a "name" from a "name space" }
  76. procedure uuid_create_sha1_from_name(out uuid: uuid_t; const nsid: uuid_t; const name: string);
  77. { uuid_compare -- Compare two UUID's "lexically" }
  78. function uuid_compare(const u1, u2: uuid_t): integer;
  79. (******************************************************************************
  80. * auxilary functions
  81. ******************************************************************************)
  82. { read_state -- read UUID generator state from non-volatile store }
  83. function read_state(out clockseq: unsigned16; out timestamp: uuid_time_t; out node: uuid_node_t): boolean;
  84. { write_state -- save UUID generator state back to non-volatile storage }
  85. procedure write_state(var clockseq: unsigned16; const timestamp: uuid_time_t; const node: uuid_node_t);
  86. { format_uuid_v1 -- make a UUID from the timestamp, clockseq, and node ID }
  87. procedure format_uuid_v1(out uuid: uuid_t; const clockseq: unsigned16; const timestamp: uuid_time_t; const node: uuid_node_t);
  88. { format_uuid_v3or5 -- make a UUID from a (pseudo)random 128-bit number }
  89. procedure format_uuid_v3or5(out uuid: uuid_t; const hash: pointer; const v: integer);
  90. { get_current_time -- get time as 60-bit 100ns ticks since UUID epoch. Compensate for the fact that real clock resolution is less than 100ns. }
  91. procedure get_current_time(out timestamp: uuid_time_t);
  92. (******************************************************************************
  93. * system functions
  94. ******************************************************************************)
  95. { get_system_time -- system dependent call to get the current system time. Returned as 100ns ticks since UUID epoch, but resolution may be less than 100ns. }
  96. procedure get_system_time(out timestamp: uuid_time_t);
  97. { true_random -- generate a crypto-quality random number. }
  98. function true_random: unsigned16;
  99. implementation
  100. { uuid_initialize }
  101. var
  102. current_state : uuid_state;
  103. current_node : uuid_node_t;
  104. procedure uuid_initialize(const state: uuid_state);
  105. begin
  106. Randomize;
  107. current_node[0] := Random($100);
  108. current_node[1] := Random($100);
  109. current_node[2] := Random($100);
  110. current_node[3] := Random($100);
  111. current_node[4] := Random($100);
  112. current_node[5] := Random($100);
  113. current_state := state;
  114. end;
  115. { uuid_finalize }
  116. procedure uuid_finalize(out state: uuid_state);
  117. begin
  118. state := current_state;
  119. end;
  120. { uuid_create }
  121. function uuid_create(out uuid: uuid_t): boolean;
  122. var
  123. timestamp: uuid_time_t;
  124. last_time: uuid_time_t;
  125. clockseq: unsigned16;
  126. last_node: uuid_node_t;
  127. f: boolean;
  128. begin
  129. (* acquire system-wide lock so we're alone *)
  130. // LOCK;
  131. (* get time, node ID, saved state from non-volatile storage *)
  132. get_current_time(timestamp);
  133. f := read_state(clockseq, last_time, last_node);
  134. (* if no NV state, or if clock went backwards, or node ID
  135. changed (e.g., new network card) change clockseq *)
  136. if not f or not CompareMem(@current_node, @last_node, sizeof(uuid_node_t)) then
  137. clockseq := true_random() else
  138. if timestamp < last_time then
  139. clockseq := clockseq + 1;
  140. (* save the state for next time *)
  141. write_state(clockseq, timestamp, current_node);
  142. // UNLOCK;
  143. (* stuff fields into the UUID *)
  144. format_uuid_v1(uuid, clockseq, timestamp, current_node);
  145. Result := true;
  146. end;
  147. { uuid_create_md5_from_name }
  148. procedure uuid_create_md5_from_name(out uuid: uuid_t; const nsid: uuid_t; const name: string);
  149. var
  150. net_nsid: uuid_t;
  151. c: TMDContext;
  152. hash: TMDDigest;
  153. begin
  154. (* put name space ID in network byte order so it hashes the same
  155. no matter what endian machine we're on *)
  156. net_nsid := nsid;
  157. net_nsid.time_low := ntobe(net_nsid.time_low);
  158. net_nsid.time_mid := ntobe(net_nsid.time_mid);
  159. net_nsid.time_hi_and_version := ntobe(net_nsid.time_hi_and_version);
  160. MDInit(c, MD_VERSION_5);
  161. MDUpdate(c, net_nsid, sizeof(net_nsid));
  162. MDUpdate(c, pchar(name)^, Length(name));
  163. MDFinal(c, hash);
  164. (* the hash is in network byte order at this point *)
  165. format_uuid_v3or5(uuid, @hash, UUID_VERSION_3);
  166. end;
  167. { uuid_create_sha1_from_name }
  168. procedure uuid_create_sha1_from_name(out uuid: uuid_t; const nsid: uuid_t; const name: string);
  169. var
  170. net_nsid: uuid_t;
  171. c: TSHA1Context;
  172. hash: TSHA1Digest;
  173. begin
  174. (* put name space ID in network byte order so it hashes the same
  175. no matter what endian machine we're on *)
  176. net_nsid := nsid;
  177. net_nsid.time_low := ntobe(net_nsid.time_low);
  178. net_nsid.time_mid := ntobe(net_nsid.time_mid);
  179. net_nsid.time_hi_and_version := ntobe(net_nsid.time_hi_and_version);
  180. SHA1Init(c);
  181. SHA1Update(c, net_nsid, sizeof(net_nsid));
  182. SHA1Update(c, pchar(name)^, Length(name));
  183. SHA1Final(c, hash);
  184. (* the hash is in network byte order at this point *)
  185. format_uuid_v3or5(uuid, @hash, UUID_VERSION_5);
  186. end;
  187. { uuid_compare }
  188. function uuid_compare(const u1, u2: uuid_t): integer;
  189. begin
  190. Result := pinteger(@u1)[0] - pinteger(@u2)[0];
  191. if Result <> 0 then Exit;
  192. Result := pinteger(@u1)[1] - pinteger(@u2)[1];
  193. if Result <> 0 then Exit;
  194. Result := pinteger(@u1)[2] - pinteger(@u2)[2];
  195. if Result <> 0 then Exit;
  196. Result := pinteger(@u1)[3] - pinteger(@u2)[3];
  197. end;
  198. { read_state }
  199. function read_state(out clockseq: unsigned16; out timestamp: uuid_time_t; out node: uuid_node_t): boolean;
  200. begin
  201. clockseq := current_state.cs;
  202. timestamp := current_state.ts;
  203. node := current_state.node;
  204. Result := true;
  205. end;
  206. { write_state }
  207. procedure write_state(var clockseq: unsigned16; const timestamp: uuid_time_t; const node: uuid_node_t);
  208. begin
  209. (* always save state to volatile shared state *)
  210. current_state.cs := clockseq;
  211. current_state.ts := timestamp;
  212. current_state.node := node;
  213. end;
  214. { format_uuid_v1 }
  215. procedure format_uuid_v1(out uuid: uuid_t; const clockseq: unsigned16; const timestamp: uuid_time_t; const node: uuid_node_t);
  216. begin
  217. uuid.time_low := timestamp and $FFFFFFFF;
  218. uuid.time_mid := (timestamp shr 32) and $FFFF;
  219. uuid.time_hi_and_version := (timestamp shr 48) and $0FFF;
  220. uuid.time_hi_and_version := uuid.time_hi_and_version or (UUID_VERSION_1 shl 12);
  221. uuid.clock_seq_low := clockseq and $FF;
  222. uuid.clock_seq_hi_and_reserved := (clockseq shr 8) and $3F;
  223. uuid.clock_seq_hi_and_reserved := uuid.clock_seq_hi_and_reserved or $80;
  224. uuid.node := node;
  225. end;
  226. { format_uuid_v3or5 }
  227. procedure format_uuid_v3or5(out uuid: uuid_t; const hash: pointer; const v: integer);
  228. begin
  229. (* convert UUID to local byte order *)
  230. move(hash^, uuid, sizeof(uuid));
  231. uuid.time_low := beton(uuid.time_low);
  232. uuid.time_mid := beton(uuid.time_mid);
  233. uuid.time_hi_and_version := beton(uuid.time_hi_and_version);
  234. (* put in the variant and version bits *)
  235. uuid.time_hi_and_version := uuid.time_hi_and_version and $0FFF;
  236. uuid.time_hi_and_version := uuid.time_hi_and_version or (v shl 12);
  237. uuid.clock_seq_hi_and_reserved := $3F;
  238. uuid.clock_seq_hi_and_reserved := uuid.clock_seq_hi_and_reserved or $80;
  239. end;
  240. { get_current_time }
  241. var
  242. time_last: uuid_time_t;
  243. uuids_this_tick: unsigned16 = UUIDS_PER_TICK;
  244. procedure get_current_time(out timestamp: uuid_time_t);
  245. var
  246. time_now: uuid_time_t;
  247. begin
  248. while true do
  249. begin
  250. get_system_time(time_now);
  251. (* if clock reading changed since last UUID generated, *)
  252. if time_last <> time_now then
  253. begin
  254. (* reset count of uuids gen'd with this clock reading *)
  255. uuids_this_tick := 0;
  256. time_last := time_now;
  257. Break;
  258. end;
  259. if uuids_this_tick < UUIDS_PER_TICK then
  260. begin
  261. uuids_this_tick := uuids_this_tick + 1;
  262. Break;
  263. end;
  264. (* going too fast for our clock; spin *)
  265. end;
  266. (* add the count of uuids to low order bits of the clock reading *)
  267. timestamp := time_now + uuids_this_tick;
  268. end;
  269. { get_system_time }
  270. procedure get_system_time(out timestamp: uuid_time_t);
  271. var
  272. Epoch:TDateTime;
  273. begin
  274. Epoch := EncodeDateTime(1582, 10, 15, 0, 0, 0, 0);
  275. timestamp := 10000*MilliSecondsBetween(Epoch, Now);
  276. end;
  277. { true_random }
  278. function true_random: unsigned16;
  279. begin
  280. Result := Random($10000);
  281. end;
  282. end.