uuidtools.rb
来自「用ruby on rails写的一个博客程序,还不错..ruby on rail」· RB 代码 · 共 555 行 · 第 1/2 页
RB
555 行
# 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. def random_node_id? return false if self.version != 1 return ((self.nodes.first & 0x01) == 1) end # Returns true if this UUID is the # nil UUID (00000000-0000-0000-0000-000000000000). 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 # 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) def version return (time_hi_and_version >> 12) 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. 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 end # Returns the IEEE 802 address used to generate this UUID or # nil if a MAC address was not used. 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 the timestamp used to generate this UUID 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 # Compares two UUIDs lexically 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 a representation of the object's state def inspect return "#<UUID:0x#{self.object_id.to_s(16)} UUID:#{self.to_s}>" end # Returns the hex digest of the UUID object. def hexdigest return self.to_i.to_s(16) end # Returns the raw bytes that represent this UUID. def raw return UUID.convert_int_to_byte_string(self.to_i, 16) end # Returns a string representation for this UUID. 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 end # Returns an integer representation for this UUID. 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 URI for this UUID. def to_uri return URI.parse(self.to_uri_string) end # Returns a URI string for this UUID. def to_uri_string return "urn:uuid:#{self.to_s}" end def UUID.create_from_hash(hash_class, namespace, name) if hash_class == Digest::MD5 version = 3 elsif hash_class == Digest::SHA1 version = 5 else raise ArgumentError, "Expected Digest::SHA1 or Digest::MD5, got #{hash_class.name}." end hash = hash_class.new hash.update(namespace.raw) hash.update(name) hash_string = hash.to_s[0..31] new_uuid = UUID.parse("#{hash_string[0..7]}-#{hash_string[8..11]}-" + "#{hash_string[12..15]}-#{hash_string[16..19]}-#{hash_string[20..31]}") new_uuid.time_hi_and_version &= 0x0FFF new_uuid.time_hi_and_version |= (version << 12) new_uuid.clock_seq_hi_and_reserved &= 0x3F new_uuid.clock_seq_hi_and_reserved |= 0x80 return new_uuid end # Returns the MAC address of the current computer's network card. # Returns nil if a MAC address could not be found. def UUID.get_mac_address if RUBY_PLATFORM =~ /win/ && !(RUBY_PLATFORM =~ /darwin/) begin ifconfig_output = `ipconfig /all` mac_addresses = ifconfig_output.scan( Regexp.new("(#{(["[0-9A-F]{2}"] * 6).join("-")})")) if mac_addresses.size > 0 return mac_addresses.first.first.downcase.gsub(/-/, ":") end rescue end else begin ifconfig_output = `ifconfig` mac_addresses = ifconfig_output.scan( Regexp.new("ether (#{(["[0-9a-f]{2}"] * 6).join(":")})")) if mac_addresses.size == 0 ifconfig_output = `ifconfig | grep HWaddr | cut -c39-` mac_addresses = ifconfig_output.scan( Regexp.new("(#{(["[0-9a-f]{2}"] * 6).join(":")})")) end if mac_addresses.size == 0 ifconfig_output = `/sbin/ifconfig` mac_addresses = ifconfig_output.scan( Regexp.new("ether (#{(["[0-9a-f]{2}"] * 6).join(":")})")) end if mac_addresses.size == 0 ifconfig_output = `/sbin/ifconfig | grep HWaddr | cut -c39-` mac_addresses = ifconfig_output.scan( Regexp.new("(#{(["[0-9a-f]{2}"] * 6).join(":")})")) end if mac_addresses.size > 0 return mac_addresses.first.first end rescue end end end # Returns 128 bits of highly unpredictable data. # The random number generator isn't perfect, but it's # much, much better than the built-in pseudorandom number generators. def UUID.true_random require 'benchmark' hash = Digest::SHA1.new performance = Benchmark.measure do hash.update(rand.to_s) hash.update(srand.to_s) hash.update(rand.to_s) hash.update(srand.to_s) hash.update(Time.now.to_s) hash.update(rand.to_s) hash.update(self.object_id.to_s) hash.update(rand.to_s) hash.update(hash.object_id.to_s) hash.update(self.methods.inspect) begin random_device = nil if File.exists? "/dev/urandom" random_device = File.open "/dev/urandom", "r" elsif File.exists? "/dev/random" random_device = File.open "/dev/random", "r" end hash.update(random_device.read(20)) if random_device != nil rescue end begin srand(hash.to_s.to_i(16) >> 128) rescue end hash.update(rand.to_s) hash.update(UUID.true_random) if (rand(2) == 0) end hash.update(performance.real.to_s) hash.update(performance.inspect) return UUID.convert_int_to_byte_string(hash.to_s[4..35].to_i(16), 16) end def UUID.convert_int_to_byte_string(integer, size) byte_string = "" for i in 0..(size - 1) byte_string << ((integer >> (((size - 1) - i) * 8)) & 0xFF) end return byte_string end def UUID.convert_byte_string_to_int(byte_string) integer = 0 size = byte_string.size for i in 0..(size - 1) integer += (byte_string[i] << (((size - 1) - i) * 8)) end return integer end class << self protected :create_from_hash protected :get_mac_address protected :true_random protected :convert_int_to_byte_string protected :convert_byte_string_to_int endendUUID_DNS_NAMESPACE = UUID.parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")UUID_URL_NAMESPACE = UUID.parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")UUID_OID_NAMESPACE = UUID.parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")UUID_X500_NAMESPACE = UUID.parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?