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

ruby-changes:10761

From: akr <ko1@a...>
Date: Sun, 15 Feb 2009 16:58:45 +0900 (JST)
Subject: [ruby-changes:10761] Ruby:r22326 (trunk): * lib/securerandom.rb (SecureRandom.urlsafe_base64): new method.

akr	2009-02-15 16:58:31 +0900 (Sun, 15 Feb 2009)

  New Revision: 22326

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

  Log:
    * lib/securerandom.rb (SecureRandom.urlsafe_base64): new method.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 22325)
+++ ChangeLog	(revision 22326)
@@ -1,3 +1,7 @@
+Sun Feb 15 16:57:35 2009  Tanaka Akira  <akr@f...>
+
+	* lib/securerandom.rb (SecureRandom.urlsafe_base64): new method.
+
 Sun Feb 15 14:58:07 2009  Tanaka Akira  <akr@f...>
 
 	* hash.c (hash_i): use Murmurhash.
Index: lib/securerandom.rb
===================================================================
--- lib/securerandom.rb	(revision 22325)
+++ lib/securerandom.rb	(revision 22326)
@@ -46,6 +46,11 @@
   # If n is not specified, 16 is assumed.
   # It may be larger in future.
   #
+  # The result may contain any byte: "\x00" - "\xff".
+  #
+  #   p SecureRandom.random_bytes #=> "\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6"
+  #   p SecureRandom.random_bytes #=> "m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97"
+  #
   # If secure random number generator is not available,
   # NotImplementedError is raised.
   def self.random_bytes(n=nil)
@@ -117,6 +122,11 @@
   # If n is not specified, 16 is assumed.
   # It may be larger in future.
   #
+  # The result may contain 0-9 and a-f.
+  #
+  #   p SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
+  #   p SecureRandom.hex #=> "91dc3bfb4de5b11d029d376634589b61"
+  #
   # If secure random number generator is not available,
   # NotImplementedError is raised.
   def self.hex(n=nil)
@@ -131,21 +141,59 @@
   # If n is not specified, 16 is assumed.
   # It may be larger in future.
   #
+  # The result may contain A-Z, a-z, 0-9, "+", "/" and "=".
+  #
+  #   p SecureRandom.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
+  #   p SecureRandom.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="
+  #
   # If secure random number generator is not available,
   # NotImplementedError is raised.
+  #
+  # See RFC 3548 for base64.
   def self.base64(n=nil)
     [random_bytes(n)].pack("m*").delete("\n")
   end
 
+  # 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.
+  #
+  # If n is not specified, 16 is assumed.
+  # It may be larger in future.
+  #
+  # The result may contain A-Z, a-z, 0-9, "-", "_" and "=".
+  #
+  #   p SecureRandom.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg=="
+  #   p SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg=="
+  #
+  # If secure random number generator is not available,
+  # NotImplementedError is raised.
+  #
+  # See RFC 3548 for URL-safe base64.
+  def self.urlsafe_base64(n=nil)
+    s = [random_bytes(n)].pack("m*")
+    s.delete!("\n")
+    s.tr!("+/", "-_")
+    s
+  end
+
   # SecureRandom.random_number generates a random number.
   #
   # If an positive integer is given as n,
   # SecureRandom.random_number returns an integer:
   # 0 <= SecureRandom.random_number(n) < n.
   #
+  #   p SecureRandom.random_number(100) #=> 15
+  #   p SecureRandom.random_number(100) #=> 88
+  #
   # If 0 is given or an argument is not given,
   # SecureRandom.random_number returns an float:
   # 0.0 <= SecureRandom.random_number() < 1.0.
+  #
+  #   p SecureRandom.random_number #=> 0.596506046187744
+  #   p SecureRandom.random_number #=> 0.350621695741409
+  #
   def self.random_number(n=0)
     if 0 < n
       hex = n.to_s(16)
@@ -168,6 +216,11 @@
   end
 
   # SecureRandom.uuid generates a v4 random UUID.
+  #
+  #   p SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
+  #   p SecureRandom.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
+  #
+  # See RFC 4122 for UUID.
   def self.uuid
     ary = self.random_bytes(16).unpack("NnnnnN")
     ary[2] = (ary[2] & 0x0fff) | 0x4000

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

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