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

ruby-changes:36253

From: nobu <ko1@a...>
Date: Sun, 9 Nov 2014 12:16:32 +0900 (JST)
Subject: [ruby-changes:36253] nobu:r48334 (trunk): securerandom.rb: separate implementations

nobu	2014-11-09 12:16:24 +0900 (Sun, 09 Nov 2014)

  New Revision: 48334

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

  Log:
    securerandom.rb: separate implementations
    
    * lib/securerandom.rb (SecureRandom.gen_random): separate
      implementation details and select at the load time.

  Modified files:
    trunk/ChangeLog
    trunk/lib/securerandom.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 48333)
+++ ChangeLog	(revision 48334)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Nov  9 12:16:22 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/securerandom.rb (SecureRandom.gen_random): separate
+	  implementation details and select at the load time.
+
 Sun Nov  9 12:09:38 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/win32/lib/win32/registry.rb (Win32::Registry::API#Enum{Value,Key):
Index: lib/securerandom.rb
===================================================================
--- lib/securerandom.rb	(revision 48333)
+++ lib/securerandom.rb	(revision 48334)
@@ -107,8 +107,11 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L107
   # NotImplementedError is raised.
   def self.random_bytes(n=nil)
     n = n ? n.to_int : 16
+    gen_random(n)
+  end
 
-    if defined? OpenSSL::Random
+  if defined? OpenSSL::Random
+    def self.gen_random(n)
       @pid = 0 unless defined?(@pid)
       pid = $$
       unless @pid == pid
@@ -119,21 +122,25 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L122
       end
       return OpenSSL::Random.random_bytes(n)
     end
-
-    if defined?(AdvApi32)
+  elsif defined?(AdvApi32)
+    def self.gen_random(n)
       return AdvApi32.gen_random(n)
     end
 
-    if !defined?(@has_urandom) || @has_urandom
+    def self.lastWin32ErrorMessage # :nodoc:
+      # for compatibility
+      return Kernel32.last_error_message
+    end
+  else
+    def self.gen_random(n)
       flags = File::RDONLY
       flags |= File::NONBLOCK if defined? File::NONBLOCK
       flags |= File::NOCTTY if defined? File::NOCTTY
       begin
         File.open("/dev/urandom", flags) {|f|
           unless f.stat.chardev?
-            raise Errno::ENOENT
+            break
           end
-          @has_urandom = true
           ret = f.read(n)
           unless ret.length == n
             raise NotImplementedError, "Unexpected partial read from random device: only #{ret.length} for #{n} bytes"
@@ -141,11 +148,10 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L148
           return ret
         }
       rescue Errno::ENOENT
-        @has_urandom = false
       end
-    end
 
-    raise NotImplementedError, "No random device"
+      raise NotImplementedError, "No random device"
+    end
   end
 
   # SecureRandom.hex generates a random hexadecimal string.
@@ -284,9 +290,4 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L290
     ary[3] = (ary[3] & 0x3fff) | 0x8000
     "%08x-%04x-%04x-%04x-%04x%08x" % ary
   end
-
-  def self.lastWin32ErrorMessage # :nodoc:
-    # for compatibility
-    Kernel32.last_error_message
-  end
 end

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

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