From 42d25de9583aab686cab04b572a56597d4f3f8bd Mon Sep 17 00:00:00 2001 From: akr Date: Fri, 6 Mar 2009 14:36:33 +0000 Subject: * lib/securerandom.rb (SecureRandom.urlsafe_base64): add optional argument to add padding. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22801 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/securerandom.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'lib/securerandom.rb') diff --git a/lib/securerandom.rb b/lib/securerandom.rb index 6b16445bb4..1365c66565 100644 --- a/lib/securerandom.rb +++ b/lib/securerandom.rb @@ -156,28 +156,35 @@ module SecureRandom # 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 -- cgit v1.2.3