UUIDTools was designed to be a simple library for generating any of the various types of UUIDs. It conforms to RFC 4122 whenever possible.
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>
Allows users to set the MAC address manually in cases where the MAC address cannot be obtained programatically.
# File lib/uuidtools.rb, line 603 def self.mac_address=(new_mac_address) @@mac_address = new_mac_address end
Creates a UUID using the MD5 hash. (Version 3)
# File lib/uuidtools.rb, line 281 def self.md5_create(namespace, name) return self.create_from_hash(Digest::MD5, namespace, name) end
# File lib/uuidtools.rb, line 116 def initialize(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) unless time_low >= 0 && time_low < 4294967296 raise ArgumentError, "Expected unsigned 32-bit number for time_low, got #{time_low}." end unless time_mid >= 0 && time_mid < 65536 raise ArgumentError, "Expected unsigned 16-bit number for time_mid, got #{time_mid}." end unless time_hi_and_version >= 0 && time_hi_and_version < 65536 raise ArgumentError, "Expected unsigned 16-bit number for time_hi_and_version, " + "got #{time_hi_and_version}." end unless clock_seq_hi_and_reserved >= 0 && clock_seq_hi_and_reserved < 256 raise ArgumentError, "Expected unsigned 8-bit number for clock_seq_hi_and_reserved, " + "got #{clock_seq_hi_and_reserved}." end unless clock_seq_low >= 0 && clock_seq_low < 256 raise ArgumentError, "Expected unsigned 8-bit number for clock_seq_low, " + "got #{clock_seq_low}." end unless nodes.respond_to? :size raise ArgumentError, "Expected nodes to respond to :size." end unless nodes.size == 6 raise ArgumentError, "Expected nodes to have size of 6." end for node in nodes unless node >= 0 && node < 256 raise ArgumentError, "Expected unsigned 8-bit number for each node, " + "got #{node}." end end @time_low = time_low @time_mid = time_mid @time_hi_and_version = time_hi_and_version @clock_seq_hi_and_reserved = clock_seq_hi_and_reserved @clock_seq_low = clock_seq_low @nodes = nodes end
Parses a UUID from a string.
# File lib/uuidtools.rb, line 172 def self.parse(uuid_string) unless uuid_string.kind_of? String raise ArgumentError, "Expected String, got #{uuid_string.class.name} instead." end uuid_components = uuid_string.downcase.scan( Regexp.new("^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-" + "([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$")).first raise ArgumentError, "Invalid UUID format." if uuid_components.nil? time_low = uuid_components[0].to_i(16) time_mid = uuid_components[1].to_i(16) time_hi_and_version = uuid_components[2].to_i(16) clock_seq_hi_and_reserved = uuid_components[3].to_i(16) clock_seq_low = uuid_components[4].to_i(16) nodes = [] for i in 0..5 nodes << uuid_components[5][(i * 2)..(i * 2) + 1].to_i(16) end return self.new(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) end
Parses a UUID from a raw byte string.
# File lib/uuidtools.rb, line 195 def self.parse_raw(raw_string) unless raw_string.kind_of? String raise ArgumentError, "Expected String, got #{raw_string.class.name} instead." end integer = self.convert_byte_string_to_int(raw_string) time_low = (integer >> 96) & 0xFFFFFFFF time_mid = (integer >> 80) & 0xFFFF time_hi_and_version = (integer >> 64) & 0xFFFF clock_seq_hi_and_reserved = (integer >> 56) & 0xFF clock_seq_low = (integer >> 48) & 0xFF nodes = [] for i in 0..5 nodes << ((integer >> (40 - (i * 8))) & 0xFF) end return self.new(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) end
Creates a UUID from a random value.
# File lib/uuidtools.rb, line 216 def self.random_create() new_uuid = self.parse_raw(self.random_128) new_uuid.time_hi_and_version &= 0x0FFF new_uuid.time_hi_and_version |= (4 << 12) new_uuid.clock_seq_hi_and_reserved &= 0x3F new_uuid.clock_seq_hi_and_reserved |= 0x80 return new_uuid end
Creates a UUID using the SHA1 hash. (Version 5)
# File lib/uuidtools.rb, line 286 def self.sha1_create(namespace, name) return self.create_from_hash(Digest::SHA1, namespace, name) end
Creates a UUID from a timestamp.
# File lib/uuidtools.rb, line 226 def self.timestamp_create(timestamp=nil) # We need a lock here to prevent two threads from ever # getting the same timestamp. @@mutex.synchronize do # Always use GMT to generate UUIDs. if timestamp.nil? gmt_timestamp = Time.now.gmtime else gmt_timestamp = timestamp.gmtime end # Convert to 100 nanosecond blocks gmt_timestamp_100_nanoseconds = (gmt_timestamp.tv_sec * 10000000) + (gmt_timestamp.tv_usec * 10) + 0x01B21DD213814000 mac_address = self.get_mac_address if mac_address == nil || mac_address == "" raise StandardError, "MAC address could not be autodetected. " + "Set the MAC address manually." end nodes = mac_address.split(":").collect do |octet| octet.to_i(16) end node_id = 0 for i in 0..5 node_id += (nodes[i] << (40 - (i * 8))) end clock_sequence = @@last_clock_sequence if clock_sequence.nil? clock_sequence = self.convert_byte_string_to_int(self.random_128) end if @@last_node_id != nil && @@last_node_id != node_id # The node id has changed. Change the clock id. clock_sequence = self.convert_byte_string_to_int(self.random_128) elsif @@last_timestamp != nil && gmt_timestamp_100_nanoseconds <= @@last_timestamp clock_sequence = clock_sequence + 1 end @@last_timestamp = gmt_timestamp_100_nanoseconds @@last_node_id = node_id @@last_clock_sequence = clock_sequence time_low = gmt_timestamp_100_nanoseconds & 0xFFFFFFFF time_mid = ((gmt_timestamp_100_nanoseconds >> 32) & 0xFFFF) time_hi_and_version = ((gmt_timestamp_100_nanoseconds >> 48) & 0x0FFF) time_hi_and_version |= (1 << 12) clock_seq_low = clock_sequence & 0xFF; clock_seq_hi_and_reserved = (clock_sequence & 0x3F00) >> 8 clock_seq_hi_and_reserved |= 0x80 return self.new(time_low, time_mid, time_hi_and_version, clock_seq_hi_and_reserved, clock_seq_low, nodes) end end
Compares two UUIDs lexically
# File lib/uuidtools.rb, line 378 def <=>(other_uuid) check = self.time_low <=> other_uuid.time_low return check if check != 0 check = self.time_mid <=> other_uuid.time_mid return check if check != 0 check = self.time_hi_and_version <=> other_uuid.time_hi_and_version return check if check != 0 check = self.clock_seq_hi_and_reserved <=> other_uuid.clock_seq_hi_and_reserved return check if check != 0 check = self.clock_seq_low <=> other_uuid.clock_seq_low return check if check != 0 for i in 0..5 if (self.nodes[i] < other_uuid.nodes[i]) return -1 end if (self.nodes[i] > other_uuid.nodes[i]) return 1 end end return 0 end
Returns an integer hash value.
# File lib/uuidtools.rb, line 443 def hash return self.to_i end
Returns the hex digest of the UUID object.
# File lib/uuidtools.rb, line 407 def hexdigest return self.to_i.to_s(16) end
Returns a representation of the object's state
# File lib/uuidtools.rb, line 402 def inspect return "#<UUID:0x#{self.object_id.to_s(16)} UUID:#{self.to_s}>" end
Returns the IEEE 802 address used to generate this UUID or nil if a MAC address was not used.
# File lib/uuidtools.rb, line 357 def mac_address return nil if self.version != 1 return nil if self.random_node_id? return (self.nodes.collect do |node| sprintf("%2.2x", node) end).join(":") end
Returns true if this UUID is the nil UUID (00000000-0000-0000-0000-000000000000).
# File lib/uuidtools.rb, line 303 def nil_uuid? return false if self.time_low != 0 return false if self.time_mid != 0 return false if self.time_hi_and_version != 0 return false if self.clock_seq_hi_and_reserved != 0 return false if self.clock_seq_low != 0 self.nodes.each do |node| return false if node != 0 end return true 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.
# File lib/uuidtools.rb, line 296 def random_node_id? return false if self.version != 1 return ((self.nodes.first & 0x01) == 1) end
Returns the raw bytes that represent this UUID.
# File lib/uuidtools.rb, line 412 def raw return self.class.convert_int_to_byte_string(self.to_i, 16) end
Returns the timestamp used to generate this UUID
# File lib/uuidtools.rb, line 366 def timestamp return nil if self.version != 1 gmt_timestamp_100_nanoseconds = 0 gmt_timestamp_100_nanoseconds += ((self.time_hi_and_version & 0x0FFF) << 48) gmt_timestamp_100_nanoseconds += (self.time_mid << 32) gmt_timestamp_100_nanoseconds += self.time_low return Time.at( (gmt_timestamp_100_nanoseconds - 0x01B21DD213814000) / 10000000.0) end
Returns an integer representation for this UUID.
# File lib/uuidtools.rb, line 427 def to_i bytes = (time_low << 96) + (time_mid << 80) + (time_hi_and_version << 64) + (clock_seq_hi_and_reserved << 56) + (clock_seq_low << 48) for i in 0..5 bytes += (nodes[i] << (40 - (i * 8))) end return bytes end
Returns a string representation for this UUID.
# File lib/uuidtools.rb, line 417 def to_s result = sprintf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", @time_low, @time_mid, @time_hi_and_version, @clock_seq_hi_and_reserved, @clock_seq_low); for i in 0..5 result << sprintf("%2.2x", @nodes[i]) end return result.downcase end
Returns a URI string for this UUID.
# File lib/uuidtools.rb, line 438 def to_uri return "urn:uuid:#{self.to_s}" end
Returns true if this UUID is valid.
# File lib/uuidtools.rb, line 346 def valid? if [0b000, 0b100, 0b110, 0b111].include?(self.variant) && (1..5).include?(self.version) return true else return false end 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.
# File lib/uuidtools.rb, line 332 def variant variant_raw = (clock_seq_hi_and_reserved >> 5) result = nil if (variant_raw >> 2) == 0 result = 0x000 elsif (variant_raw >> 1) == 2 result = 0x100 else result = variant_raw end return (result >> 6) 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)
# File lib/uuidtools.rb, line 322 def version return (time_hi_and_version >> 12) end