Skip to content
This repository was archived by the owner on Oct 14, 2026. It is now read-only.

Automatic Password Generation

lightwave edited this page Sep 13, 2010 · 2 revisions

Aaron Blohowiak suggests adding this as a public method in user.rb:

def new_random_password
  self.password= Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--")[0,6]
  self.password_confirmation = self.password
end

This creates an alphanumeric password and satisfies the confirmation validation.

This is not what I would call random. Actually it is quite predictable. This can lead to a security hole, especially if you use this method to allow users to reset their passwords on their own. An attacker would only have to know a valid login to be able to reset the password to a value known to him, since he obviously knows the exact moment in time he submits the request.

The following code should be at least slightly better:

def new_random_password
self.password= Digest::SHA1.hexdigest(“-#{rand.to_s}-#{login}—”)[0,6]
self.password_confirmation = self.password
end

Clone this wiki locally