Generate a random secret key with OpenSSL. If OpenSSL is not already
loaded, then this method will attempt to load it. LoadError will be raised
if that fails.
# File railties/lib/rails_generator/secret_key_generator.rb, line 67
def generate_secret_with_openssl
require 'openssl'
if !File.exist?("/dev/urandom")
# OpenSSL transparently seeds the random number generator with
# data from /dev/urandom. On platforms where that is not
# available, such as Windows, we have to provide OpenSSL with
# our own seed. Unfortunately there's no way to provide a
# secure seed without OS support, so we'll have to do with
# rand() and Time.now.usec().
OpenSSL::Random.seed(rand(0).to_s + Time.now.usec.to_s)
end
data = OpenSSL::BN.rand(2048, -1, false).to_s
if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000
OpenSSL::Digest::SHA512.new(data).hexdigest
else
generate_secret_with_prng
end
end