Sha256: 3c1e0d069ae41f5e4b5b8d11b1f4bb804ce980562fed8211611d4c624a3ac666

Contents?: true

Size: 1.78 KB

Versions: 20

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: false
#--
# = Ruby-space predefined Digest subclasses
#
# = Info
# 'OpenSSL for Ruby 2' project
# Copyright (C) 2002  Michal Rokos <m.rokos@sh.cvut.cz>
# All rights reserved.
#
# = Licence
# This program is licensed under the same licence as Ruby.
# (See the file 'LICENCE'.)
#++

module OpenSSL
  class Digest

    alg = %w(MD2 MD4 MD5 MDC2 RIPEMD160 SHA1)
    if OPENSSL_VERSION_NUMBER < 0x10100000
      alg += %w(DSS DSS1 SHA)
    end
    if OPENSSL_VERSION_NUMBER > 0x00908000
      alg += %w(SHA224 SHA256 SHA384 SHA512)
    end

    # Return the +data+ hash computed with +name+ Digest. +name+ is either the
    # long name or short name of a supported digest algorithm.
    #
    # === Examples
    #
    #   OpenSSL::Digest.digest("SHA256", "abc")
    #
    # which is equivalent to:
    #
    #   OpenSSL::Digest::SHA256.digest("abc")

    def self.digest(name, data)
      super(data, name)
    end

    alg.each{|name|
      klass = Class.new(self) {
        define_method(:initialize, ->(data = nil) {super(name, data)})
      }
      singleton = (class << klass; self; end)
      singleton.class_eval{
        define_method(:digest){|data| new.digest(data) }
        define_method(:hexdigest){|data| new.hexdigest(data) }
      }
      const_set(name, klass)
    }

    # Deprecated.
    #
    # This class is only provided for backwards compatibility.
    # Use OpenSSL::Digest instead.
    class Digest < Digest; end # :nodoc:
    deprecate_constant :Digest

  end # Digest

  # Returns a Digest subclass by +name+.
  #
  #   require 'openssl'
  #
  #   OpenSSL::Digest("MD5")
  #   # => OpenSSL::Digest::MD5
  #
  #   Digest("Foo")
  #   # => NameError: wrong constant name Foo

  def Digest(name)
    OpenSSL::Digest.const_get(name)
  end

  module_function :Digest

end # OpenSSL

Version data entries

20 entries across 20 versions & 4 rubygems

Version Path
rhodes-7.6.0 lib/extensions/openssl/openssl/digest.rb
rhodes-7.5.1 lib/extensions/openssl/openssl/digest.rb
rhodes-7.4.1 lib/extensions/openssl/openssl/digest.rb
rubysl-openssl-2.13 lib/openssl/digest.rb
rubysl-openssl-2.11 lib/openssl/digest.rb
rhodes-7.1.17 lib/extensions/openssl/openssl/digest.rb
rhodes-6.2.0 lib/extensions/openssl/openssl/digest.rb
openssl-2.0.9 lib/openssl/digest.rb
openssl-2.0.8 lib/openssl/digest.rb
openssl-2.0.7 lib/openssl/digest.rb
openssl-2.0.6 lib/openssl/digest.rb
openssl-2.0.5 lib/openssl/digest.rb
openssl-2.0.4 lib/openssl/digest.rb
openssl-2.0.3 lib/openssl/digest.rb
ruby-compiler-0.1.1 vendor/ruby/ext/openssl/lib/openssl/digest.rb
openssl-2.0.2 lib/openssl/digest.rb
openssl-2.0.1 lib/openssl/digest.rb
openssl-2.0.0 lib/openssl/digest.rb
openssl-2.0.0.beta.2 lib/openssl/digest.rb
openssl-2.0.0.beta.1 lib/openssl/digest.rb