Sha256: 54fd6e0915b3b78bb05220cbc5dbfabc538ffa1011a4860e055394e1a41f5615

Contents?: true

Size: 1.74 KB

Versions: 114

Compression:

Stored size: 1.74 KB

Contents

require 'securerandom'

module Digest
  module UUID
    DNS_NAMESPACE  = "k\xA7\xB8\x10\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" #:nodoc:
    URL_NAMESPACE  = "k\xA7\xB8\x11\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" #:nodoc:
    OID_NAMESPACE  = "k\xA7\xB8\x12\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" #:nodoc:
    X500_NAMESPACE = "k\xA7\xB8\x14\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" #:nodoc:

    # 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: http://www.ietf.org/rfc/rfc4122.txt
    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

    # Convenience method for uuid_from_hash using Digest::MD5.
    def self.uuid_v3(uuid_namespace, name)
      self.uuid_from_hash(Digest::MD5, uuid_namespace, name)
    end

    # Convenience method for uuid_from_hash using Digest::SHA1.
    def self.uuid_v5(uuid_namespace, name)
      self.uuid_from_hash(Digest::SHA1, uuid_namespace, name)
    end

    # Convenience method for SecureRandom.uuid.
    def self.uuid_v4
      SecureRandom.uuid
    end
  end
end

Version data entries

114 entries across 109 versions & 16 rubygems

Version Path
activesupport-5.0.0 lib/active_support/core_ext/digest/uuid.rb
activesupport-5.0.0.rc2 lib/active_support/core_ext/digest/uuid.rb
activesupport-5.0.0.racecar1 lib/active_support/core_ext/digest/uuid.rb
activesupport-5.0.0.rc1 lib/active_support/core_ext/digest/uuid.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/core_ext/digest/uuid.rb
activesupport-5.0.0.beta4 lib/active_support/core_ext/digest/uuid.rb
activesupport-4.2.6 lib/active_support/core_ext/digest/uuid.rb
core_ext-0.0.6 lib/core_ext/digest/uuid.rb
activesupport-4.2.6.rc1 lib/active_support/core_ext/digest/uuid.rb
activesupport-4.2.5.2 lib/active_support/core_ext/digest/uuid.rb
activesupport-5.0.0.beta3 lib/active_support/core_ext/digest/uuid.rb
activesupport-5.0.0.beta2 lib/active_support/core_ext/digest/uuid.rb
activejob-lock-0.0.2 rails/activesupport/lib/active_support/core_ext/digest/uuid.rb
activesupport-5.0.0.beta1.1 lib/active_support/core_ext/digest/uuid.rb
activesupport-4.2.5.1 lib/active_support/core_ext/digest/uuid.rb
core_ext-0.0.5 lib/core_ext/digest/uuid.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activesupport-4.2.4/lib/active_support/core_ext/digest/uuid.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activesupport-4.2.4/lib/active_support/core_ext/digest/uuid.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/core_ext/digest/uuid.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.5/lib/active_support/core_ext/digest/uuid.rb