module Digest::UUID

def self.uuid_from_hash(hash_class, namespace, name)

See RFC 4122 for details of UUID at: https://www.ietf.org/rfc/rfc4122.txt

uuid_from_hash always generates the same UUID for a given name and namespace combination.
Using OpenSSL::Digest::MD5 generates version 3 UUIDs; OpenSSL::Digest::SHA1 generates version 5 UUIDs.

Generates a v5 non-random UUID (Universally Unique IDentifier).
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