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

ruby-changes:14504

From: knu <ko1@a...>
Date: Mon, 18 Jan 2010 04:23:31 +0900 (JST)
Subject: [ruby-changes:14504] Ruby:r26341 (ruby_1_8): * ext/digest/digest.c (rb_digest_instance_digest)

knu	2010-01-18 04:23:17 +0900 (Mon, 18 Jan 2010)

  New Revision: 26341

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26341

  Log:
    * ext/digest/digest.c (rb_digest_instance_digest)
      (rb_digest_instance_hexdigest): Save a method call of reset()
      for a disposable clone.
    
    * ext/digest/digest.c (rb_digest_instance_hexdigest_bang): Fix
      rdoc.
    
    * ext/digest/lib/digest.rb (Digest::Class.base64digest)
      (Digest::Instance#base64digest{,!}): New methods.

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/NEWS
    branches/ruby_1_8/ext/digest/digest.c
    branches/ruby_1_8/ext/digest/lib/digest.rb
    branches/ruby_1_8/test/digest/test_digest.rb

Index: ruby_1_8/ext/digest/digest.c
===================================================================
--- ruby_1_8/ext/digest/digest.c	(revision 26340)
+++ ruby_1_8/ext/digest/digest.c	(revision 26341)
@@ -179,10 +179,7 @@
         value = rb_funcall(self, id_finish, 0);
         rb_funcall(self, id_reset, 0);
     } else {
-        VALUE clone = rb_obj_clone(self);
-
-        value = rb_funcall(clone, id_finish, 0);
-        rb_funcall(clone, id_reset, 0);
+        value = rb_funcall(rb_obj_clone(self), id_finish, 0);
     }
 
     return value;
@@ -227,10 +224,7 @@
         value = rb_funcall(self, id_finish, 0);
         rb_funcall(self, id_reset, 0);
     } else {
-        VALUE clone = rb_obj_clone(self);
-
-        value = rb_funcall(clone, id_finish, 0);
-        rb_funcall(clone, id_reset, 0);
+        value = rb_funcall(rb_obj_clone(self), id_finish, 0);
     }
 
     return hexencode_str_new(value);
@@ -240,8 +234,8 @@
  * call-seq:
  *     digest_obj.hexdigest! -> string
  *
- * Returns the resulting hash value and resets the digest to the
- * initial state.
+ * Returns the resulting hash value in a hex-encoded form and resets
+ * the digest to the initial state.
  */
 static VALUE
 rb_digest_instance_hexdigest_bang(VALUE self)
Index: ruby_1_8/ext/digest/lib/digest.rb
===================================================================
--- ruby_1_8/ext/digest/lib/digest.rb	(revision 26340)
+++ ruby_1_8/ext/digest/lib/digest.rb	(revision 26341)
@@ -1,4 +1,5 @@
 require 'digest.so'
+autoload :Base64, 'base64'
 
 module Digest
   def self.const_missing(name)
@@ -28,6 +29,13 @@
     def self.file(name)
       new.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)
+      Base64.strict_encode64(digest(str, *args))
+    end
   end
 
   module Instance
@@ -42,6 +50,25 @@
       }
       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)
+      Base64.strict_encode64(str ? digest(str) : digest)
+    end
+
+    # Returns the resulting hash value and resets the digest to the
+    # initial state.
+    def base64digest!
+      Base64.strict_encode64(digest!)
+    end
   end
 end
 
Index: ruby_1_8/NEWS
===================================================================
--- ruby_1_8/NEWS	(revision 26340)
+++ ruby_1_8/NEWS	(revision 26341)
@@ -194,6 +194,12 @@
 
     Added as an alias to #ungetc.
 
+* digest
+    * new methods:
+      * Digest::Class.base64digest
+      * Digest::Instance#base64digest
+      * Digest::Instance#base64digest!
+
 * rss
 
   * 0.2.4 -> 0.2.7.
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 26340)
+++ ruby_1_8/ChangeLog	(revision 26341)
@@ -1,3 +1,15 @@
+Mon Jan 18 04:15:58 2010  Akinori MUSHA  <knu@i...>
+
+	* ext/digest/digest.c (rb_digest_instance_digest)
+	  (rb_digest_instance_hexdigest): Save a method call of reset()
+	  for a disposable clone.
+
+	* ext/digest/digest.c (rb_digest_instance_hexdigest_bang): Fix
+	  rdoc.
+
+	* ext/digest/lib/digest.rb (Digest::Class.base64digest)
+	  (Digest::Instance#base64digest{,!}): New methods.
+
 Mon Jan 18 03:39:05 2010  Akinori MUSHA  <knu@i...>
 
 	* lib/base64.rb (Base64#{strict_encode64,strict_decode64,urlsafe_encode64,
Index: ruby_1_8/test/digest/test_digest.rb
===================================================================
--- ruby_1_8/test/digest/test_digest.rb	(revision 26340)
+++ ruby_1_8/test/digest/test_digest.rb	(revision 26341)
@@ -18,14 +18,22 @@
   Data2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
 
   def test_s_hexdigest
-    self.class::DATA.each do |str, digest|
-      assert_equal(digest, self.class::ALGO.hexdigest(str))
+    self.class::DATA.each do |str, hexdigest|
+      assert_equal(hexdigest, self.class::ALGO.hexdigest(str))
     end
   end
 
+  def test_s_base64digest
+    self.class::DATA.each do |str, hexdigest|
+      digest = [hexdigest].pack("H*")
+      assert_equal([digest].pack("m0").delete("\n"), self.class::ALGO.base64digest(str))
+    end
+  end
+
   def test_s_digest
-    self.class::DATA.each do |str, digest|
-      assert_equal([digest].pack("H*"), self.class::ALGO.digest(str))
+    self.class::DATA.each do |str, hexdigest|
+      digest = [hexdigest].pack("H*")
+      assert_equal(digest, self.class::ALGO.digest(str))
     end
   end
 

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

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