mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
securerandom.rb: separate implementations
* lib/securerandom.rb (SecureRandom.gen_random): separate implementation details and select at the load time. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48334 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0066d95fd6
commit
93ccab82c5
2 changed files with 20 additions and 14 deletions
|
@ -1,3 +1,8 @@
|
|||
Sun Nov 9 12:16:22 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* 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@ruby-lang.org>
|
||||
|
||||
* ext/win32/lib/win32/registry.rb (Win32::Registry::API#Enum{Value,Key):
|
||||
|
|
|
@ -107,8 +107,11 @@ module SecureRandom
|
|||
# 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
|
|||
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
|
|||
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
|
|||
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
|
||||
|
|
Loading…
Add table
Reference in a new issue