DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Generate Random Password In Ruby
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






Comments
Snippets Manager replied on Tue, 2009/01/06 - 8:48am
Snippets Manager replied on Mon, 2012/05/07 - 2:36pm
ActiveSupport::SecureRandom.base64(6)Snippets Manager replied on Tue, 2009/01/06 - 8:48am
Snippets Manager replied on Mon, 2007/03/26 - 12:53pm
self.password = Base64.encode64(Digest::SHA1.digest("#{rand(1<<64)}/#{Time.now.to_f}/#{Process.pid}/#{login}"))[0..7]But ultimately, if somebody knows about when you created the password and what your username are, there aren't that many possibilities to try. It would be best to include something truly random, which is hard to do cross-platform.