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.
Source Code
# File rails_generator/secret_key_generator.rb, line 68 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 return OpenSSL::Digest::SHA512.new(data).hexdigest end
<code/>and<pre/>for code samples.