модуль Digest::UUID
Публичные методы класса
# File activesupport/lib/active_support/core_ext/digest/uuid.rb, line 16
def self.uuid_from_hash(hash_class, uuid_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(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 (универсальный идентификатор уникальных ресурсов) версии 5.
Использование Digest::MD5 генерирует UUID версии 3; Digest::SHA1 генерирует UUID версии 5. ::uuid_from_hash всегда генерирует один и тот же UUID для заданной комбинации имени и пространства имён.
Подробности о UUID см. в RFC 4122: www.ietf.org/rfc/rfc4122.txt
# File activesupport/lib/active_support/core_ext/digest/uuid.rb, line 37 def self.uuid_v3(uuid_namespace, name) self.uuid_from_hash(Digest::MD5, uuid_namespace, name) end
Метод-обёртка для ::uuid_from_hash с использованием Digest::MD5.
# File activesupport/lib/active_support/core_ext/digest/uuid.rb, line 47 def self.uuid_v4 SecureRandom.uuid end
Метод-обёртка для SecureRandom.uuid.
# File activesupport/lib/active_support/core_ext/digest/uuid.rb, line 42 def self.uuid_v5(uuid_namespace, name) self.uuid_from_hash(Digest::SHA1, uuid_namespace, name) end
Метод-обёртка для ::uuid_from_hash с использованием Digest::SHA1.
© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.