uuid.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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;
  17. (******************************************************************************
  18. * types and constants
  19. ******************************************************************************)
  20. type
  21. uuid_t = TGuid;
  22. uuid_time_t = qword;
  23. uuid_node_t = array[0..5] of byte;
  24. unsigned16 = word;
  25. uuid_state = record
  26. ts : uuid_time_t; // saved timestamp
  27. node : uuid_node_t; // saved node ID
  28. cs : unsigned16; // saved clock sequence
  29. end;
  30. const
  31. UUID_VERSION_1 = $1; // The time-based version specified in this document.
  32. UUID_VERSION_2 = $2; // DCE Security version, with embedded POSIX UIDs.
  33. UUID_VERSION_3 = $3; // The name-based version specified in this document that uses MD5 hashing.
  34. UUID_VERSION_4 = $4; // The randomly or pseudo-randomly generated version specified in this document.
  35. UUID_VERSION_5 = $5; // The name-based version specified in this document that uses SHA-1 hashing.
  36. { set the following to the number of 100ns ticks of the actual resolution of your system's clock }
  37. UUIDS_PER_TICK = 1024;
  38. (******************************************************************************
  39. * core uuid functions
  40. ******************************************************************************)
  41. { uuid_initialize -- used to initialize the uuid_create function }
  42. procedure uuid_initialize(const state: uuid_state);
  43. { uuid_create -- generator a UUID }
  44. function uuid_create(var uuid: uuid_t): boolean;
  45. { uuid_finalize -- returns the current state }
  46. procedure uuid_finalize(var state: uuid_state);
  47. { uuid_create_md5_from_name -- create a version 3 (MD5) UUID using a "name" from a "name space" }
  48. procedure uuid_create_md5_from_name(var uuid: uuid_t; const nsid: uuid_t; const name: string);
  49. { uuid_create_sha1_from_name -- create a version 5 (SHA-1) UUID using a "name" from a "name space" }
  50. procedure uuid_create_sha1_from_name(var uuid: uuid_t; const nsid: uuid_t; const name: string);
  51. { uuid_compare -- Compare two UUID's "lexically" }
  52. function uuid_compare(const u1, u2: uuid_t): integer;
  53. (******************************************************************************
  54. * auxilary functions
  55. ******************************************************************************)
  56. { read_state -- read UUID generator state from non-volatile store }
  57. function read_state(var clockseq: unsigned16; var timestamp: uuid_time_t; var node: uuid_node_t): boolean;
  58. { write_state -- save UUID generator state back to non-volatile storage }
  59. procedure write_state(var clockseq: unsigned16; const timestamp: uuid_time_t; const node: uuid_node_t);
  60. { format_uuid_v1 -- make a UUID from the timestamp, clockseq, and node ID }
  61. procedure format_uuid_v1(var uuid: uuid_t; const clockseq: unsigned16; const timestamp: uuid_time_t; const node: uuid_node_t);
  62. { format_uuid_v3or5 -- make a UUID from a (pseudo)random 128-bit number }
  63. procedure format_uuid_v3or5(var uuid: uuid_t; const hash: pointer; const v: integer);
  64. { 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. }
  65. procedure get_current_time(var timestamp: uuid_time_t);
  66. (******************************************************************************
  67. * system functions
  68. ******************************************************************************)
  69. { 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. }
  70. procedure get_system_time(var timestamp: uuid_time_t);
  71. { true_random -- generate a crypto-quality random number. }
  72. function true_random: unsigned16;
  73. implementation
  74. { uuid_initialize }
  75. var
  76. current_state : uuid_state;
  77. current_node : uuid_node_t;
  78. procedure uuid_initialize(const state: uuid_state);
  79. begin
  80. Randomize;
  81. current_node[0] := Random($100);
  82. current_node[1] := Random($100);
  83. current_node[2] := Random($100);
  84. current_node[3] := Random($100);
  85. current_node[4] := Random($100);
  86. current_node[5] := Random($100);
  87. current_state := state;
  88. end;
  89. { uuid_finalize }
  90. procedure uuid_finalize(var state: uuid_state);
  91. begin
  92. state := current_state;
  93. end;
  94. { uuid_create }
  95. function uuid_create(var uuid: TGuid): boolean;
  96. var
  97. timestamp: uuid_time_t;
  98. last_time: uuid_time_t;
  99. clockseq: unsigned16;
  100. last_node: uuid_node_t;
  101. f: boolean;
  102. begin
  103. (* acquire system-wide lock so we're alone *)
  104. // LOCK;
  105. (* get time, node ID, saved state from non-volatile storage *)
  106. get_current_time(timestamp);
  107. f := read_state(clockseq, last_time, last_node);
  108. (* if no NV state, or if clock went backwards, or node ID
  109. changed (e.g., new network card) change clockseq *)
  110. if not f or not CompareMem(@current_node, @last_node, sizeof(uuid_node_t)) then
  111. clockseq := true_random() else
  112. if timestamp < last_time then
  113. clockseq := clockseq + 1;
  114. (* save the state for next time *)
  115. write_state(clockseq, timestamp, current_node);
  116. // UNLOCK;
  117. (* stuff fields into the UUID *)
  118. format_uuid_v1(uuid, clockseq, timestamp, current_node);
  119. Result := true;
  120. end;
  121. { uuid_create_md5_from_name }
  122. procedure uuid_create_md5_from_name(var uuid: uuid_t; const nsid: uuid_t; const name: string);
  123. var
  124. net_nsid: uuid_t;
  125. c: TMDContext;
  126. hash: TMDDigest;
  127. begin
  128. (* put name space ID in network byte order so it hashes the same
  129. no matter what endian machine we're on *)
  130. net_nsid := nsid;
  131. net_nsid.time_low := ntobe(net_nsid.time_low);
  132. net_nsid.time_mid := ntobe(net_nsid.time_mid);
  133. net_nsid.time_hi_and_version := ntobe(net_nsid.time_hi_and_version);
  134. MDInit(c, MD_VERSION_5);
  135. MDUpdate(c, net_nsid, sizeof(net_nsid));
  136. MDUpdate(c, pchar(name)^, Length(name));
  137. MDFinal(c, hash);
  138. (* the hash is in network byte order at this point *)
  139. format_uuid_v3or5(uuid, @hash, UUID_VERSION_3);
  140. end;
  141. { uuid_create_sha1_from_name }
  142. procedure uuid_create_sha1_from_name(var uuid: uuid_t; const nsid: uuid_t; const name: string);
  143. var
  144. net_nsid: uuid_t;
  145. { c: TMDContext;
  146. hash: TMDDigest;}
  147. begin
  148. (* put name space ID in network byte order so it hashes the same
  149. no matter what endian machine we're on *)
  150. net_nsid := nsid;
  151. net_nsid.time_low := ntobe(net_nsid.time_low);
  152. net_nsid.time_mid := ntobe(net_nsid.time_mid);
  153. net_nsid.time_hi_and_version := ntobe(net_nsid.time_hi_and_version);
  154. {SHAInit(c, SHA_VERSION_1);
  155. SHAUpdate(c, net_nsid, sizeof(net_nsid));
  156. SHAUpdate(c, pchar(name)^, Length(name));
  157. SHAFinal(c, hash);}
  158. (* the hash is in network byte order at this point *)
  159. format_uuid_v3or5(uuid, @hash, UUID_VERSION_5);
  160. end;
  161. { uuid_compare }
  162. function uuid_compare(const u1, u2: uuid_t): integer;
  163. begin
  164. Result := pinteger(@u1)[0] - pinteger(@u2)[0];
  165. if Result <> 0 then Exit;
  166. Result := pinteger(@u1)[1] - pinteger(@u2)[1];
  167. if Result <> 0 then Exit;
  168. Result := pinteger(@u1)[2] - pinteger(@u2)[2];
  169. if Result <> 0 then Exit;
  170. Result := pinteger(@u1)[3] - pinteger(@u2)[3];
  171. end;
  172. { read_state }
  173. function read_state(var clockseq: unsigned16; var timestamp: uuid_time_t; var node: uuid_node_t): boolean;
  174. begin
  175. clockseq := current_state.cs;
  176. timestamp := current_state.ts;
  177. node := current_state.node;
  178. Result := true;
  179. end;
  180. { write_state }
  181. procedure write_state(var clockseq: unsigned16; const timestamp: uuid_time_t; const node: uuid_node_t);
  182. begin
  183. (* always save state to volatile shared state *)
  184. current_state.cs := clockseq;
  185. current_state.ts := timestamp;
  186. current_state.node := node;
  187. end;
  188. { format_uuid_v1 }
  189. procedure format_uuid_v1(var uuid: uuid_t; const clockseq: unsigned16; const timestamp: uuid_time_t; const node: uuid_node_t);
  190. begin
  191. uuid.time_low := timestamp and $FFFFFFFF;
  192. uuid.time_mid := (timestamp shr 32) and $FFFF;
  193. uuid.time_hi_and_version := (timestamp shr 48) and $0FFF;
  194. uuid.time_hi_and_version := uuid.time_hi_and_version or (UUID_VERSION_1 shl 12);
  195. uuid.clock_seq_low := clockseq and $FF;
  196. uuid.clock_seq_hi_and_reserved := (clockseq shr 8) and $3F;
  197. uuid.clock_seq_hi_and_reserved := uuid.clock_seq_hi_and_reserved or $80;
  198. uuid.node := node;
  199. end;
  200. { format_uuid_v3or5 }
  201. procedure format_uuid_v3or5(var uuid: uuid_t; const hash: pointer; const v: integer);
  202. begin
  203. (* convert UUID to local byte order *)
  204. move(hash^, uuid, sizeof(uuid));
  205. uuid.time_low := beton(uuid.time_low);
  206. uuid.time_mid := beton(uuid.time_mid);
  207. uuid.time_hi_and_version := beton(uuid.time_hi_and_version);
  208. (* put in the variant and version bits *)
  209. uuid.time_hi_and_version := uuid.time_hi_and_version and $0FFF;
  210. uuid.time_hi_and_version := uuid.time_hi_and_version or (v shl 12);
  211. uuid.clock_seq_hi_and_reserved := $3F;
  212. uuid.clock_seq_hi_and_reserved := uuid.clock_seq_hi_and_reserved or $80;
  213. end;
  214. { get_current_time }
  215. var
  216. time_last: uuid_time_t;
  217. uuids_this_tick: unsigned16 = UUIDS_PER_TICK;
  218. procedure get_current_time(var timestamp: uuid_time_t);
  219. var
  220. time_now: uuid_time_t;
  221. begin
  222. while true do
  223. begin
  224. get_system_time(time_now);
  225. (* if clock reading changed since last UUID generated, *)
  226. if time_last <> time_now then
  227. begin
  228. (* reset count of uuids gen'd with this clock reading *)
  229. uuids_this_tick := 0;
  230. time_last := time_now;
  231. Break;
  232. end;
  233. if uuids_this_tick < UUIDS_PER_TICK then
  234. begin
  235. uuids_this_tick := uuids_this_tick + 1;
  236. Break;
  237. end;
  238. (* going too fast for our clock; spin *)
  239. end;
  240. (* add the count of uuids to low order bits of the clock reading *)
  241. timestamp := time_now + uuids_this_tick;
  242. end;
  243. { get_system_time }
  244. procedure get_system_time(var timestamp: uuid_time_t);
  245. var
  246. Epoch:TDateTime;
  247. begin
  248. Epoch := EncodeDateTime(1582, 10, 15, 0, 0, 0, 0);
  249. timestamp := 10000*MilliSecondsBetween(Epoch, Now);
  250. end;
  251. { true_random }
  252. function true_random: unsigned16;
  253. begin
  254. Result := Random($10000);
  255. end;
  256. end.