Class UUIDTools::UUID
In: lib/uuidtools.rb
Parent: Object
Comparable UUID lib/uuidtools.rb UUIDTools dot/m_1_0.png

uuidtools.rb

 UUIDTools was designed to be a simple library for generating any
 of the various types of UUIDs.  It conforms to RFC 4122 whenever
 possible.

Example

  UUID.md5_create(UUID_DNS_NAMESPACE, "www.widgets.com")
  => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
  UUID.sha1_create(UUID_DNS_NAMESPACE, "www.widgets.com")
  => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
  UUID.timestamp_create
  => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
  UUID.random_create
  => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>

Methods

Included Modules

Comparable

Attributes

clock_seq_hi_and_reserved  [RW] 
clock_seq_low  [RW] 
nodes  [RW] 
time_hi_and_version  [RW] 
time_low  [RW] 
time_mid  [RW] 

Public Class methods

Allows users to set the MAC address manually in cases where the MAC address cannot be obtained programatically.

[Source]

     # File lib/uuidtools.rb, line 612
612:     def self.mac_address=(new_mac_address)
613:       @@mac_address = new_mac_address
614:     end

Creates a UUID using the MD5 hash. (Version 3)

[Source]

     # File lib/uuidtools.rb, line 252
252:     def self.md5_create(namespace, name)
253:       return self.create_from_hash(Digest::MD5, namespace, name)
254:     end

[Source]

     # File lib/uuidtools.rb, line 65
 65:     def initialize(time_low, time_mid, time_hi_and_version,
 66:         clock_seq_hi_and_reserved, clock_seq_low, nodes)
 67:       unless time_low >= 0 && time_low < 4294967296
 68:         raise ArgumentError,
 69:           "Expected unsigned 32-bit number for time_low, got #{time_low}."
 70:       end
 71:       unless time_mid >= 0 && time_mid < 65536
 72:         raise ArgumentError,
 73:           "Expected unsigned 16-bit number for time_mid, got #{time_mid}."
 74:       end
 75:       unless time_hi_and_version >= 0 && time_hi_and_version < 65536
 76:         raise ArgumentError,
 77:           "Expected unsigned 16-bit number for time_hi_and_version, " +
 78:           "got #{time_hi_and_version}."
 79:       end
 80:       unless clock_seq_hi_and_reserved >= 0 && clock_seq_hi_and_reserved < 256
 81:         raise ArgumentError,
 82:           "Expected unsigned 8-bit number for clock_seq_hi_and_reserved, " +
 83:           "got #{clock_seq_hi_and_reserved}."
 84:       end
 85:       unless clock_seq_low >= 0 && clock_seq_low < 256
 86:         raise ArgumentError,
 87:           "Expected unsigned 8-bit number for clock_seq_low, " +
 88:           "got #{clock_seq_low}."
 89:       end
 90:       unless nodes.kind_of?(Enumerable)
 91:         raise TypeError,
 92:           "Expected Enumerable, got #{nodes.class.name}."
 93:       end
 94:       unless nodes.size == 6
 95:         raise ArgumentError,
 96:           "Expected nodes to have size of 6."
 97:       end
 98:       for node in nodes
 99:         unless node >= 0 && node < 256
100:           raise ArgumentError,
101:             "Expected unsigned 8-bit number for each node, " +
102:             "got #{node}."
103:         end
104:       end
105:       @time_low = time_low
106:       @time_mid = time_mid
107:       @time_hi_and_version = time_hi_and_version
108:       @clock_seq_hi_and_reserved = clock_seq_hi_and_reserved
109:       @clock_seq_low = clock_seq_low
110:       @nodes = nodes
111:     end

Parses a UUID from a string.

[Source]

     # File lib/uuidtools.rb, line 121
121:     def self.parse(uuid_string)
122:       unless uuid_string.kind_of? String
123:         raise TypeError,
124:           "Expected String, got #{uuid_string.class.name} instead."
125:       end
126:       uuid_components = uuid_string.downcase.scan(
127:         Regexp.new("^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-" +
128:           "([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$")).first
129:       raise ArgumentError, "Invalid UUID format." if uuid_components.nil?
130:       time_low = uuid_components[0].to_i(16)
131:       time_mid = uuid_components[1].to_i(16)
132:       time_hi_and_version = uuid_components[2].to_i(16)
133:       clock_seq_hi_and_reserved = uuid_components[3].to_i(16)
134:       clock_seq_low = uuid_components[4].to_i(16)
135:       nodes = []
136:       for i in 0..5
137:         nodes << uuid_components[5][(i * 2)..(i * 2) + 1].to_i(16)
138:       end
139:       return self.new(time_low, time_mid, time_hi_and_version,
140:         clock_seq_hi_and_reserved, clock_seq_low, nodes)
141:     end

Parse a UUID from a hexdigest String.

[Source]

     # File lib/uuidtools.rb, line 174
174:     def self.parse_hexdigest(uuid_hexdigest)
175:       unless uuid_hexdigest.kind_of?(String)
176:         raise ArgumentError,
177:           "Expected String, got #{uuid_hexdigest.class.name} instead."
178:       end
179:       return self.parse_int(uuid_hexdigest.to_i(16))
180:     end

Parses a UUID from an Integer.

[Source]

     # File lib/uuidtools.rb, line 165
165:     def self.parse_int(uuid_int)
166:       unless uuid_int.kind_of?(Integer)
167:         raise ArgumentError,
168:           "Expected Integer, got #{uuid_int.class.name} instead."
169:       end
170:       return self.parse_raw(self.convert_int_to_byte_string(uuid_int, 16))
171:     end

Parses a UUID from a raw byte string.

[Source]

     # File lib/uuidtools.rb, line 144
144:     def self.parse_raw(raw_string)
145:       unless raw_string.kind_of? String
146:         raise TypeError,
147:           "Expected String, got #{raw_string.class.name} instead."
148:       end
149:       integer = self.convert_byte_string_to_int(raw_string)
150: 
151:       time_low = (integer >> 96) & 0xFFFFFFFF
152:       time_mid = (integer >> 80) & 0xFFFF
153:       time_hi_and_version = (integer >> 64) & 0xFFFF
154:       clock_seq_hi_and_reserved = (integer >> 56) & 0xFF
155:       clock_seq_low = (integer >> 48) & 0xFF
156:       nodes = []
157:       for i in 0..5
158:         nodes << ((integer >> (40 - (i * 8))) & 0xFF)
159:       end
160:       return self.new(time_low, time_mid, time_hi_and_version,
161:         clock_seq_hi_and_reserved, clock_seq_low, nodes)
162:     end

Creates a UUID from a random value.

[Source]

     # File lib/uuidtools.rb, line 183
183:     def self.random_create()
184:       new_uuid = self.parse_raw(SecureRandom.random_bytes(16))
185:       new_uuid.time_hi_and_version &= 0x0FFF
186:       new_uuid.time_hi_and_version |= (4 << 12)
187:       new_uuid.clock_seq_hi_and_reserved &= 0x3F
188:       new_uuid.clock_seq_hi_and_reserved |= 0x80
189:       return new_uuid
190:     end

Creates a UUID using the SHA1 hash. (Version 5)

[Source]

     # File lib/uuidtools.rb, line 257
257:     def self.sha1_create(namespace, name)
258:       return self.create_from_hash(Digest::SHA1, namespace, name)
259:     end

Creates a UUID from a timestamp.

[Source]

     # File lib/uuidtools.rb, line 193
193:     def self.timestamp_create(timestamp=nil)
194:       # We need a lock here to prevent two threads from ever
195:       # getting the same timestamp.
196:       @@mutex.synchronize do
197:         # Always use GMT to generate UUIDs.
198:         if timestamp.nil?
199:           gmt_timestamp = Time.now.gmtime
200:         else
201:           gmt_timestamp = timestamp.gmtime
202:         end
203:         # Convert to 100 nanosecond blocks
204:         gmt_timestamp_100_nanoseconds = (gmt_timestamp.tv_sec * 10000000) +
205:           (gmt_timestamp.tv_usec * 10) + 0x01B21DD213814000
206:         mac_address = self.mac_address
207:         node_id = 0
208:         if mac_address != nil
209:           nodes = mac_address.split(":").collect do |octet|
210:             octet.to_i(16)
211:           end
212:         else
213:           nodes = SecureRandom.random_bytes(6).unpack("C*")
214:           nodes[0] |= 0b00000001
215:         end
216:         for i in 0..5
217:           node_id += (nodes[i] << (40 - (i * 8)))
218:         end
219:         clock_sequence = @@last_clock_sequence
220:         if clock_sequence.nil?
221:           clock_sequence = self.convert_byte_string_to_int(
222:             SecureRandom.random_bytes(16)
223:           )
224:         end
225:         if @@last_node_id != nil && @@last_node_id != node_id
226:           # The node id has changed.  Change the clock id.
227:           clock_sequence = self.convert_byte_string_to_int(
228:             SecureRandom.random_bytes(16)
229:           )
230:         elsif @@last_timestamp != nil &&
231:             gmt_timestamp_100_nanoseconds <= @@last_timestamp
232:           clock_sequence = clock_sequence + 1
233:         end
234:         @@last_timestamp = gmt_timestamp_100_nanoseconds
235:         @@last_node_id = node_id
236:         @@last_clock_sequence = clock_sequence
237: 
238:         time_low = gmt_timestamp_100_nanoseconds & 0xFFFFFFFF
239:         time_mid = ((gmt_timestamp_100_nanoseconds >> 32) & 0xFFFF)
240:         time_hi_and_version = ((gmt_timestamp_100_nanoseconds >> 48) & 0x0FFF)
241:         time_hi_and_version |= (1 << 12)
242:         clock_seq_low = clock_sequence & 0xFF;
243:         clock_seq_hi_and_reserved = (clock_sequence & 0x3F00) >> 8
244:         clock_seq_hi_and_reserved |= 0x80
245: 
246:         return self.new(time_low, time_mid, time_hi_and_version,
247:           clock_seq_hi_and_reserved, clock_seq_low, nodes)
248:       end
249:     end

Public Instance methods

Compares two UUIDs lexically

[Source]

     # File lib/uuidtools.rb, line 349
349:     def <=>(other_uuid)
350:       check = self.time_low <=> other_uuid.time_low
351:       return check if check != 0
352:       check = self.time_mid <=> other_uuid.time_mid
353:       return check if check != 0
354:       check = self.time_hi_and_version <=> other_uuid.time_hi_and_version
355:       return check if check != 0
356:       check = self.clock_seq_hi_and_reserved <=>
357:         other_uuid.clock_seq_hi_and_reserved
358:       return check if check != 0
359:       check = self.clock_seq_low <=> other_uuid.clock_seq_low
360:       return check if check != 0
361:       for i in 0..5
362:         if (self.nodes[i] < other_uuid.nodes[i])
363:           return -1
364:         end
365:         if (self.nodes[i] > other_uuid.nodes[i])
366:           return 1
367:         end
368:       end
369:       return 0
370:     end

Returns true if this UUID is exactly equal to the other UUID.

[Source]

     # File lib/uuidtools.rb, line 477
477:     def eql?(other)
478:       return self == other
479:     end

Returns an integer hash value.

[Source]

     # File lib/uuidtools.rb, line 424
424:     def hash
425:       return @hash unless @hash.nil?
426:       if self.frozen?
427:         return generate_hash
428:       else
429:         return (@hash = generate_hash)
430:       end
431:     end

Returns the hex digest of the UUID object.

[Source]

     # File lib/uuidtools.rb, line 378
378:     def hexdigest
379:       return @hexdigest unless @hexdigest.nil?
380:       if self.frozen?
381:         return generate_hexdigest
382:       else
383:         return (@hexdigest = generate_hexdigest)
384:       end
385:     end

Returns a representation of the object‘s state

[Source]

     # File lib/uuidtools.rb, line 373
373:     def inspect
374:       return "#<UUID:0x#{self.object_id.to_s(16)} UUID:#{self.to_s}>"
375:     end

Returns the IEEE 802 address used to generate this UUID or nil if a MAC address was not used.

[Source]

     # File lib/uuidtools.rb, line 328
328:     def mac_address
329:       return nil if self.version != 1
330:       return nil if self.random_node_id?
331:       return (self.nodes.collect do |node|
332:         sprintf("%2.2x", node)
333:       end).join(":")
334:     end

Returns true if this UUID is the nil UUID (00000000-0000-0000-0000-000000000000).

[Source]

     # File lib/uuidtools.rb, line 274
274:     def nil_uuid?
275:       return false if self.time_low != 0
276:       return false if self.time_mid != 0
277:       return false if self.time_hi_and_version != 0
278:       return false if self.clock_seq_hi_and_reserved != 0
279:       return false if self.clock_seq_low != 0
280:       self.nodes.each do |node|
281:         return false if node != 0
282:       end
283:       return true
284:     end

This method applies only to version 1 UUIDs. Checks if the node ID was generated from a random number or from an IEEE 802 address (MAC address). Always returns false for UUIDs that aren‘t version 1. This should not be confused with version 4 UUIDs where more than just the node id is random.

[Source]

     # File lib/uuidtools.rb, line 267
267:     def random_node_id?
268:       return false if self.version != 1
269:       return ((self.nodes.first & 0x01) == 1)
270:     end

Returns the raw bytes that represent this UUID.

[Source]

     # File lib/uuidtools.rb, line 388
388:     def raw
389:       return @raw unless @raw.nil?
390:       if self.frozen?
391:         return generate_raw
392:       else
393:         return (@raw = generate_raw)
394:       end
395:     end

Returns the timestamp used to generate this UUID

[Source]

     # File lib/uuidtools.rb, line 337
337:     def timestamp
338:       return nil if self.version != 1
339:       gmt_timestamp_100_nanoseconds = 0
340:       gmt_timestamp_100_nanoseconds +=
341:         ((self.time_hi_and_version  & 0x0FFF) << 48)
342:       gmt_timestamp_100_nanoseconds += (self.time_mid << 32)
343:       gmt_timestamp_100_nanoseconds += self.time_low
344:       return Time.at(
345:         (gmt_timestamp_100_nanoseconds - 0x01B21DD213814000) / 10000000.0)
346:     end

Returns an integer representation for this UUID.

[Source]

     # File lib/uuidtools.rb, line 409
409:     def to_i
410:       return @integer unless @integer.nil?
411:       if self.frozen?
412:         return generate_i
413:       else
414:         return (@integer = generate_i)
415:       end
416:     end

Returns a string representation for this UUID.

[Source]

     # File lib/uuidtools.rb, line 398
398:     def to_s
399:       return @string unless @string.nil?
400:       if self.frozen?
401:         return generate_s
402:       else
403:         return (@string = generate_s)
404:       end
405:     end
to_str()

Alias for to_s

Returns a URI string for this UUID.

[Source]

     # File lib/uuidtools.rb, line 419
419:     def to_uri
420:       return "urn:uuid:#{self.to_s}"
421:     end

Returns true if this UUID is valid.

[Source]

     # File lib/uuidtools.rb, line 317
317:     def valid?
318:       if [0b000, 0b100, 0b110, 0b111].include?(self.variant) &&
319:         (1..5).include?(self.version)
320:         return true
321:       else
322:         return false
323:       end
324:     end

Returns the UUID variant. Possible values: 0b000 - Reserved, NCS backward compatibility. 0b100 - The variant specified in this document. 0b110 - Reserved, Microsoft Corporation backward compatibility. 0b111 - Reserved for future definition.

[Source]

     # File lib/uuidtools.rb, line 303
303:     def variant
304:       variant_raw = (clock_seq_hi_and_reserved >> 5)
305:       result = nil
306:       if (variant_raw >> 2) == 0
307:         result = 0x000
308:       elsif (variant_raw >> 1) == 2
309:         result = 0x100
310:       else
311:         result = variant_raw
312:       end
313:       return (result >> 6)
314:     end

Returns the UUID version type. Possible values: 1 - Time-based with unique or random host identifier 2 - DCE Security version (with POSIX UIDs) 3 - Name-based (MD5 hash) 4 - Random 5 - Name-based (SHA-1 hash)

[Source]

     # File lib/uuidtools.rb, line 293
293:     def version
294:       return (time_hi_and_version >> 12)
295:     end

Protected Instance methods

Generates an integer hash value.

[Source]

     # File lib/uuidtools.rb, line 442
442:     def generate_hash
443:       return self.to_i % 0x3fffffff
444:     end

Generates the hex digest of the UUID object.

[Source]

     # File lib/uuidtools.rb, line 437
437:     def generate_hexdigest
438:       return self.to_i.to_s(16).rjust(32, "0")
439:     end

Generates an integer representation for this UUID.

[Source]

     # File lib/uuidtools.rb, line 447
447:     def generate_i
448:       return (begin
449:         bytes = (time_low << 96) + (time_mid << 80) +
450:           (time_hi_and_version << 64) + (clock_seq_hi_and_reserved << 56) +
451:           (clock_seq_low << 48)
452:         for i in 0..5
453:           bytes += (nodes[i] << (40 - (i * 8)))
454:         end
455:         bytes
456:       end)
457:     end

Generates the raw bytes that represent this UUID.

[Source]

     # File lib/uuidtools.rb, line 470
470:     def generate_raw
471:       return self.class.convert_int_to_byte_string(self.to_i, 16)
472:     end

Generates a string representation for this UUID.

[Source]

     # File lib/uuidtools.rb, line 460
460:     def generate_s
461:       result = sprintf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", @time_low, @time_mid,
462:         @time_hi_and_version, @clock_seq_hi_and_reserved, @clock_seq_low);
463:       for i in 0..5
464:         result << sprintf("%2.2x", @nodes[i])
465:       end
466:       return result.downcase
467:     end

[Validate]