Never been to DZone Snippets before?

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 (See related posts)

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 on this post

adb posts on Mar 26, 2007 at 15:03
It makes me pretty nervous to use passwords that weak by default. This has more characters and more randomness:

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.

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts