class method self.uuid_from_hash

Ruby on Rails 4.2.9

Since v4.2.9

Available in: v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

self.uuid_from_hash(hash_class, uuid_namespace, name)

Generates a v5 non-random UUID (Universally Unique IDentifier).

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

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

Parameters

hash_class req
uuid_namespace req
name req
Source
# 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

Defined in activesupport/lib/active_support/core_ext/digest/uuid.rb line 16 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Digest::UUID

Type at least 2 characters to search.

↑↓ navigate · open · esc close