ruby-changes:37514
From: nobu <ko1@a...>
Date: Sat, 14 Feb 2015 12:02:40 +0900 (JST)
Subject: [ruby-changes:37514] nobu:r49595 (trunk): securerandom.rb: Random::Formatter
nobu 2015-02-14 12:02:32 +0900 (Sat, 14 Feb 2015) New Revision: 49595 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49595 Log: securerandom.rb: Random::Formatter * lib/securerandom.rb (Random::Formatter): extract random number formatting methods into a module. Modified files: trunk/lib/securerandom.rb Index: lib/securerandom.rb =================================================================== --- lib/securerandom.rb (revision 49594) +++ lib/securerandom.rb (revision 49595) @@ -91,7 +91,9 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L91 ret end end +end +module Random::Formatter # SecureRandom.hex generates a random hexadecimal string. # # The argument _n_ specifies the length, in bytes, of the random number to be generated. @@ -107,7 +109,7 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L109 # # If a secure random number generator is not available, # +NotImplementedError+ is raised. - def self.hex(n=nil) + def hex(n=nil) random_bytes(n).unpack("H*")[0] end @@ -128,7 +130,7 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L130 # +NotImplementedError+ is raised. # # See RFC 3548 for the definition of base64. - def self.base64(n=nil) + def base64(n=nil) [random_bytes(n)].pack("m*").delete("\n") end @@ -158,7 +160,7 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L160 # +NotImplementedError+ is raised. # # See RFC 3548 for the definition of URL-safe base64. - def self.urlsafe_base64(n=nil, padding=false) + def urlsafe_base64(n=nil, padding=false) s = [random_bytes(n)].pack("m*") s.delete!("\n") s.tr!("+/", "-_") @@ -182,7 +184,7 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L184 # p SecureRandom.random_number #=> 0.596506046187744 # p SecureRandom.random_number #=> 0.350621695741409 # - def self.random_number(n=0) + def random_number(n=0) if 0 < n if defined? OpenSSL::BN OpenSSL::BN.rand_range(n).to_i @@ -195,7 +197,7 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L197 mask |= mask >> 2 mask |= mask >> 4 begin - rnd = SecureRandom.random_bytes(bin.length) + rnd = random_bytes(bin.length) rnd[0] = (rnd[0].ord & mask).chr end until rnd < bin rnd.unpack("H*")[0].hex @@ -205,7 +207,7 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L207 if defined? OpenSSL::BN i64 = OpenSSL::BN.rand(64, -1).to_i else - i64 = SecureRandom.random_bytes(8).unpack("Q")[0] + i64 = random_bytes(8).unpack("Q")[0] end Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG) end @@ -222,10 +224,12 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L224 # # See RFC 4122 for details of UUID. # - def self.uuid - ary = self.random_bytes(16).unpack("NnnnnN") + def uuid + ary = random_bytes(16).unpack("NnnnnN") ary[2] = (ary[2] & 0x0fff) | 0x4000 ary[3] = (ary[3] & 0x3fff) | 0x8000 "%08x-%04x-%04x-%04x-%04x%08x" % ary end end + +SecureRandom.extend(Random::Formatter) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/