From c26cb089988c81b355b2d48bcbe343601fe214a0 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 23 Nov 2008 15:27:09 -0800 Subject: Lazy-require OpenSSL. Skip entirely if SecureRandom is available. --- activesupport/lib/active_support/secure_random.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/secure_random.rb b/activesupport/lib/active_support/secure_random.rb index 97971e8830..cfbce4d754 100644 --- a/activesupport/lib/active_support/secure_random.rb +++ b/activesupport/lib/active_support/secure_random.rb @@ -1,8 +1,3 @@ -begin - require 'openssl' -rescue LoadError -end - begin require 'securerandom' rescue LoadError @@ -10,7 +5,7 @@ end module ActiveSupport if defined?(::SecureRandom) - # Use Ruby 1.9's SecureRandom library whenever possible. + # Use Ruby's SecureRandom library if available. SecureRandom = ::SecureRandom # :nodoc: else # = Secure random number generator interface. @@ -64,6 +59,13 @@ module ActiveSupport def self.random_bytes(n=nil) n ||= 16 + unless defined? OpenSSL + begin + require 'openssl' + rescue LoadError + end + end + if defined? OpenSSL::Random return OpenSSL::Random.random_bytes(n) end -- cgit v1.2.3