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

ruby-changes:11193

From: akr <ko1@a...>
Date: Fri, 6 Mar 2009 23:36:47 +0900 (JST)
Subject: [ruby-changes:11193] Ruby:r22801 (trunk): * lib/securerandom.rb (SecureRandom.urlsafe_base64): add optional

akr	2009-03-06 23:36:33 +0900 (Fri, 06 Mar 2009)

  New Revision: 22801

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

  Log:
    * lib/securerandom.rb (SecureRandom.urlsafe_base64): add optional
      argument to add padding.

  Modified files:
    trunk/ChangeLog
    trunk/lib/securerandom.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 22800)
+++ ChangeLog	(revision 22801)
@@ -1,3 +1,8 @@
+Fri Mar  6 23:35:59 2009  Tanaka Akira  <akr@f...>
+
+	* lib/securerandom.rb (SecureRandom.urlsafe_base64): add optional
+	  argument to add padding.
+
 Fri Mar  6 19:25:40 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* configure.in (RUBY_LIB_VERSION_STYLE): sets full or minor style
Index: lib/securerandom.rb
===================================================================
--- lib/securerandom.rb	(revision 22800)
+++ lib/securerandom.rb	(revision 22801)
@@ -156,28 +156,35 @@
 
   # SecureRandom.urlsafe_base64 generates a random URL-safe base64 string.
   #
-  # The argument n specifies the length of the random length.
-  # The length of the result string is about 4/3 of n.
+  # The argument _n_ specifies the length of the random length.
+  # The length of the result string is about 4/3 of _n_.
   #
-  # If n is not specified, 16 is assumed.
+  # If _n_ is not specified, 16 is assumed.
   # It may be larger in future.
   #
-  # No padding is generated because "=" may be used as a URL delimiter.
+  # The boolean argument _padding_ specifies the padding.
+  # If it is false or nil, padding is not generated.
+  # Otherwise padding is generated.
+  # By default, padding is not generated because "=" may be used as a URL delimiter.
   #
   # The result may contain A-Z, a-z, 0-9, "-" and "_".
+  # "=" is also used if _padding_ is true.
   #
   #   p SecureRandom.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
   #   p SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
   #
+  #   p SecureRandom.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
+  #   p SecureRandom.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="
+  #
   # If secure random number generator is not available,
   # NotImplementedError is raised.
   #
   # See RFC 3548 for URL-safe base64.
-  def self.urlsafe_base64(n=nil)
+  def self.urlsafe_base64(n=nil, padding=false)
     s = [random_bytes(n)].pack("m*")
     s.delete!("\n")
     s.tr!("+/", "-_")
-    s.delete!("=")
+    s.delete!("=") if !padding
     s
   end
 

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

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