[前][次][番号順一覧][スレッド一覧]

ruby-changes:68413

From: Nobuyoshi <ko1@a...>
Date: Tue, 12 Oct 2021 22:59:34 +0900 (JST)
Subject: [ruby-changes:68413] 013bac15b0 (master): Fix libraries under digest

https://git.ruby-lang.org/ruby.git/commit/?id=013bac15b0

From 013bac15b01b69b8f556074bc30333dc913b9118 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Tue, 12 Oct 2021 22:32:42 +0900
Subject: Fix libraries under digest

---
 ext/digest/lib/digest.rb    | 124 ++++++++++++++++++++++++++++++++++++++
 ext/digest/sha2/lib/sha2.rb | 142 ++++++++++++++++++++++++++++++++++++++++++++
 lib/digest.rb               | 124 --------------------------------------
 lib/digest/sha2.rb          | 142 --------------------------------------------
 4 files changed, 266 insertions(+), 266 deletions(-)
 create mode 100644 ext/digest/lib/digest.rb
 create mode 100644 ext/digest/sha2/lib/sha2.rb
 delete mode 100644 lib/digest.rb
 delete mode 100644 lib/digest/sha2.rb

diff --git a/ext/digest/lib/digest.rb b/ext/digest/lib/digest.rb
new file mode 100644
index 0000000000..7cb0d2c318
--- /dev/null
+++ b/ext/digest/lib/digest.rb
@@ -0,0 +1,124 @@ https://github.com/ruby/ruby/blob/trunk/ext/digest/lib/digest.rb#L1
+# frozen_string_literal: false
+
+# The gem and bundle commands (except for bundle exec) first load
+# digest via openssl and then load gems, so if this is installed via
+# gem, we are overwriting the default version of digest.  Beware not
+# to break it or cause redefinition warnings.
+#
+# When we introduce incompatible changes and overwriting is not an
+# option, and given that the default version does not have security
+# defects, we may just give up and let those commands just use the
+# default version of digest.
+#
+# return if defined?(Digest) && caller_locations.any? { |l|
+#   %r{/(rubygems/gem_runner|bundler/cli)\.rb}.match?(l.path)
+# }
+
+require 'digest/loader'
+
+module Digest
+  # A mutex for Digest().
+  REQUIRE_MUTEX ||= Thread::Mutex.new
+
+  def self.const_missing(name) # :nodoc:
+    case name
+    when :SHA256, :SHA384, :SHA512
+      lib = 'digest/sha2'
+    else
+      lib = File.join('digest', name.to_s.downcase)
+    end
+
+    begin
+      require lib
+    rescue LoadError
+      raise LoadError, "library not found for class Digest::#{name} -- #{lib}", caller(1)
+    end
+    unless Digest.const_defined?(name)
+      raise NameError, "uninitialized constant Digest::#{name}", caller(1)
+    end
+    Digest.const_get(name)
+  end
+
+  class ::Digest::Class
+    # Creates a digest object and reads a given file, _name_.
+    # Optional arguments are passed to the constructor of the digest
+    # class.
+    #
+    #   p Digest::SHA256.file("X11R6.8.2-src.tar.bz2").hexdigest
+    #   # => "f02e3c85572dc9ad7cb77c2a638e3be24cc1b5bea9fdbb0b0299c9668475c534"
+    def self.file(name, *args)
+      new(*args).file(name)
+    end
+
+    # Returns the base64 encoded hash value of a given _string_.  The
+    # return value is properly padded with '=' and contains no line
+    # feeds.
+    def self.base64digest(str, *args)
+      [digest(str, *args)].pack('m0')
+    end
+  end
+
+  module Instance
+    # Updates the digest with the contents of a given file _name_ and
+    # returns self.
+    def file(name)
+      File.open(name, "rb") {|f|
+        buf = ""
+        while f.read(16384, buf)
+          update buf
+        end
+      }
+      self
+    end
+
+    # If none is given, returns the resulting hash value of the digest
+    # in a base64 encoded form, keeping the digest's state.
+    #
+    # If a +string+ is given, returns the hash value for the given
+    # +string+ in a base64 encoded form, resetting the digest to the
+    # initial state before and after the process.
+    #
+    # In either case, the return value is properly padded with '=' and
+    # contains no line feeds.
+    def base64digest(str = nil)
+      [str ? digest(str) : digest].pack('m0')
+    end
+
+    # Returns the resulting hash value and resets the digest to the
+    # initial state.
+    def base64digest!
+      [digest!].pack('m0')
+    end
+  end
+end
+
+# call-seq:
+#   Digest(name) -> digest_subclass
+#
+# Returns a Digest subclass by +name+ in a thread-safe manner even
+# when on-demand loading is involved.
+#
+#   require 'digest'
+#
+#   Digest("MD5")
+#   # => Digest::MD5
+#
+#   Digest(:SHA256)
+#   # => Digest::SHA256
+#
+#   Digest(:Foo)
+#   # => LoadError: library not found for class Digest::Foo -- digest/foo
+def Digest(name)
+  const = name.to_sym
+  Digest::REQUIRE_MUTEX.synchronize {
+    # Ignore autoload's because it is void when we have #const_missing
+    Digest.const_missing(const)
+  }
+rescue LoadError
+  # Constants do not necessarily rely on digest/*.
+  if Digest.const_defined?(const)
+    Digest.const_get(const)
+  else
+    raise
+  end
+end
diff --git a/ext/digest/sha2/lib/sha2.rb b/ext/digest/sha2/lib/sha2.rb
new file mode 100644
index 0000000000..f17593a206
--- /dev/null
+++ b/ext/digest/sha2/lib/sha2.rb
@@ -0,0 +1,142 @@ https://github.com/ruby/ruby/blob/trunk/ext/digest/sha2/lib/sha2.rb#L1
+# frozen_string_literal: false
+#--
+# sha2.rb - defines Digest::SHA2 class which wraps up the SHA256,
+#           SHA384, and SHA512 classes.
+#++
+# Copyright (c) 2006 Akinori MUSHA <knu@i...>
+#
+# All rights reserved.  You can redistribute and/or modify it under the same
+# terms as Ruby.
+#
+#   $Id$
+
+require 'digest'
+require 'digest/sha2/loader'
+
+module Digest
+  #
+  # A meta digest provider class for SHA256, SHA384 and SHA512.
+  #
+  # FIPS 180-2 describes SHA2 family of digest algorithms. It defines
+  # three algorithms:
+  # * one which works on chunks of 512 bits and returns a 256-bit
+  #   digest (SHA256),
+  # * one which works on chunks of 1024 bits and returns a 384-bit
+  #   digest (SHA384),
+  # * and one which works on chunks of 1024 bits and returns a 512-bit
+  #   digest (SHA512).
+  #
+  # ==Examples
+  #  require 'digest'
+  #
+  #  # Compute a complete digest
+  #  Digest::SHA2.hexdigest 'abc'          # => "ba7816bf8..."
+  #  Digest::SHA2.new(256).hexdigest 'abc' # => "ba7816bf8..."
+  #  Digest::SHA256.hexdigest 'abc'        # => "ba7816bf8..."
+  #
+  #  Digest::SHA2.new(384).hexdigest 'abc' # => "cb00753f4..."
+  #  Digest::SHA384.hexdigest 'abc'        # => "cb00753f4..."
+  #
+  #  Digest::SHA2.new(512).hexdigest 'abc' # => "ddaf35a19..."
+  #  Digest::SHA512.hexdigest 'abc'        # => "ddaf35a19..."
+  #
+  #  # Compute digest by chunks
+  #  sha2 = Digest::SHA2.new               # =>#<Digest::SHA2:256>
+  #  sha2.update "ab"
+  #  sha2 << "c"                           # alias for #update
+  #  sha2.hexdigest                        # => "ba7816bf8..."
+  #
+  #  # Use the same object to compute another digest
+  #  sha2.reset
+  #  sha2 << "message"
+  #  sha2.hexdigest                        # => "ab530a13e..."
+  #
+  class SHA2 < Digest::Class
+    # call-seq:
+    #   Digest::SHA2.new(bitlen = 256) -> digest_obj
+    #
+    # Create a new SHA2 hash object with a given bit length.
+    #
+    # Valid bit lengths are 256, 384 and 512.
+    def initialize(bitlen = 256)
+      case bitlen
+      when 256
+        @sha2 = Digest::SHA256.new
+      when 384
+        @sha2 = Digest::SHA384.new
+      when 512
+        @sha2 = Digest::SHA512.new
+      else
+        raise ArgumentError, "unsupported bit length: %s" % bitlen.inspect
+      end
+      @bitlen = bitlen
+    end
+
+    # call-seq:
+    #   digest_obj.reset -> digest_obj
+    #
+    # Reset the digest to the initial state and return self.
+    def reset
+      @sha2.reset
+      self
+    end
+
+    # call-seq:
+    #   digest_obj.update(string) -> digest_obj
+    #   digest_obj << string -> digest_obj
+    #
+    # Update the digest using a given _string_ and return self.
+    def update(str)
+      @sha2.update(str)
+      self
+    end
+    alias << update
+
+    def finish # :nodoc:
+      @sha2.digest!
+    end
+    private :finish
+
+
+    # call-seq:
+    #   digest_obj.block_length -> Integer
+    #
+    # Return the block length of the digest in bytes.
+    #
+    #   Digest::SHA256.new.block_length * 8
+    #   # => 512
+    #   Digest::SHA384.new.block_length * 8
+    #   # => 1024
+    #   Digest::SHA512.new.block_length * 8
+    #   # => 1024
+    def block_length
+      @sha2.block_length
+    end
+
+    # call-seq:
+    #   digest_obj.digest_length -> Integer
+    #
+    # Return the length of the hash value (the digest) in bytes.
+    #
+    #   Digest::SHA256.new.digest_length * 8
+    #   # => 256
+    #   Digest::SHA384.new.digest_length * 8
+    #   # => 384
+    #   Digest::SHA512.new.digest_length * 8
+    #   # => 512
+    #
+    # For example, digests produced by Digest::SHA256 will always be 32 bytes
+    # (256 bits) in size.
+    def digest_length
+      @sha2.digest_length
+    end
+
+    def initialize_copy(other) # :nodoc:
+      @sha2 = other.instance_eval { @sha2.clone }
+    end
+
+    def inspect # :nodoc:
+      "#<%s:%d %s>" % [self.class.name, @bitlen, hexdigest]
+    end
+  end
+end
diff --git a/lib/digest.rb b/lib/digest.rb
deleted file mode 100644
index 7cb0d2c318..0000000000
--- a/lib/digest.rb
+++ /dev/null
@@ -1,124 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/ext/digest/sha2/lib/sha2.rb#L0
-# frozen_string_literal: false
-
-# The gem and bundle commands (except for bundle exec) first load
-# digest via openssl and then load gems, so if this is installed via
-# gem, we are overwriting the default version of digest.  Beware not
-# to break it or cause redefinition warnings.
-#
-# When we introduce incompatible changes and overwriting is not an
-# option, and given that the default version does not have security
-# defects, we may just give up and let those commands just use the
-# default version of digest.
-#
-# return if defined?(Digest) && caller_locations.any? { |l|
-#   %r{/(rubygems/gem_runner|bundler/cli)\.rb}.match?(l.path)
-# }
-
-require 'digest/loader'
-
-module Digest
-  # A mutex for Digest().
-  REQUIRE_MUTEX ||= Thread::Mutex.new
-
-  def self.const_missing(name) # :nodoc:
-    case name
-    when :SHA256, :SHA384, :SHA512
-      lib = 'digest/sha2'
-    else
-      lib = Fil (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]