Skip to content

Commit

Permalink
Use SecureRandom to generate unique ids, if available.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7966 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Oct 18, 2007
1 parent b98dcde commit a9f790a
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions actionpack/lib/action_controller/cgi_ext/session.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@ class CGI #:nodoc:
# * Expose the CGI instance to session stores. # * Expose the CGI instance to session stores.
# * Don't require 'digest/md5' whenever a new session id is generated. # * Don't require 'digest/md5' whenever a new session id is generated.
class Session #:nodoc: class Session #:nodoc:
# Generate an MD5 hash including the time, a random number, the process id, begin
# and a constant string. This is used to generate session ids but may be require 'securerandom'
# reused elsewhere.
def self.generate_unique_id(constant = 'foobar') # Generate a 32-character unique id using SecureRandom.
md5 = Digest::MD5.new # This is used to generate session ids but may be reused elsewhere.
now = Time.now def self.generate_unique_id(constant = nil)
md5 << now.to_s SecureRandom.hex(16)
md5 << String(now.usec) end
md5 << String(rand(0)) rescue LoadError
md5 << String($$) # Generate an 32-character unique id based on a hash of the current time,
md5 << constant # a random number, the process id, and a constant string. This is used
md5.hexdigest # to generate session ids but may be reused elsewhere.
def self.generate_unique_id(constant = 'foobar')
md5 = Digest::MD5.new
now = Time.now
md5 << now.to_s
md5 << String(now.usec)
md5 << String(rand(0))
md5 << String($$)
md5 << constant
md5.hexdigest
end
end end


# Make the CGI instance available to session stores. # Make the CGI instance available to session stores.
Expand Down

0 comments on commit a9f790a

Please sign in to comment.