跳至内容 跳至搜索
方法
N
U

类公共方法

nil_uuid()

返回空 UUID。 这是一个特殊的 UUID 形式,其所有 128 位都设置为零。

# File activesupport/lib/active_support/core_ext/digest/uuid.rb, line 58
def self.nil_uuid
  "00000000-0000-0000-0000-000000000000"
end

uuid_from_hash(hash_class, namespace, name)

生成一个 v5 非随机 UUID(通用唯一标识符)。

使用 OpenSSL::Digest::MD5 生成版本 3 UUID;OpenSSL::Digest::SHA1 生成版本 5 UUID。 uuid_from_hash 始终为给定名称和命名空间组合生成相同的 UUID

有关 UUID 的详细信息,请参阅 RFC 4122:www.ietf.org/rfc/rfc4122.txt

# File activesupport/lib/active_support/core_ext/digest/uuid.rb, line 19
def self.uuid_from_hash(hash_class, namespace, name)
  if hash_class == Digest::MD5 || hash_class == OpenSSL::Digest::MD5
    version = 3
  elsif hash_class == Digest::SHA1 || hash_class == OpenSSL::Digest::SHA1
    version = 5
  else
    raise ArgumentError, "Expected OpenSSL::Digest::SHA1 or OpenSSL::Digest::MD5, got #{hash_class.name}."
  end

  uuid_namespace = pack_uuid_namespace(namespace)

  hash = hash_class.new
  hash.update(uuid_namespace)
  hash.update(name)

  ary = hash.digest.unpack("NnnnnN")
  ary[2] = (ary[2] & 0x0FFF) | (version << 12)
  ary[3] = (ary[3] & 0x3FFF) | 0x8000

  "%08x-%04x-%04x-%04x-%04x%08x" % ary
end

uuid_v3(uuid_namespace, name)

使用 OpenSSL::Digest::MD5 的 uuid_from_hash 的便捷方法。

# File activesupport/lib/active_support/core_ext/digest/uuid.rb, line 42
def self.uuid_v3(uuid_namespace, name)
  uuid_from_hash(OpenSSL::Digest::MD5, uuid_namespace, name)
end

uuid_v4()

SecureRandom.uuid 的便捷方法。

# File activesupport/lib/active_support/core_ext/digest/uuid.rb, line 52
def self.uuid_v4
  SecureRandom.uuid
end

uuid_v5(uuid_namespace, name)

使用 OpenSSL::Digest::SHA1 的 uuid_from_hash 的便捷方法。

# File activesupport/lib/active_support/core_ext/digest/uuid.rb, line 47
def self.uuid_v5(uuid_namespace, name)
  uuid_from_hash(OpenSSL::Digest::SHA1, uuid_namespace, name)
end