From fbc0b59bd6778625157448fa78004a296ff6ebfc Mon Sep 17 00:00:00 2001 From: knu Date: Sun, 17 Jan 2010 19:23:17 +0000 Subject: * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@26341 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/digest/digest.c | 14 ++++---------- ext/digest/lib/digest.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) (limited to 'ext') diff --git a/ext/digest/digest.c b/ext/digest/digest.c index 88a7206f76..7c07fc3cac 100644 --- a/ext/digest/digest.c +++ b/ext/digest/digest.c @@ -179,10 +179,7 @@ rb_digest_instance_digest(int argc, VALUE *argv, VALUE self) 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 @@ rb_digest_instance_hexdigest(int argc, VALUE *argv, VALUE self) 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 @@ rb_digest_instance_hexdigest(int argc, VALUE *argv, VALUE self) * 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) diff --git a/ext/digest/lib/digest.rb b/ext/digest/lib/digest.rb index 0c4ee3c2cc..30805c0a6d 100644 --- a/ext/digest/lib/digest.rb +++ b/ext/digest/lib/digest.rb @@ -1,4 +1,5 @@ require 'digest.so' +autoload :Base64, 'base64' module Digest def self.const_missing(name) @@ -28,6 +29,13 @@ module Digest 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 @@ module Digest } 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 -- cgit v1.2.3