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

Mickael has posted 15 posts at DZone. View Full User Profile

Generate Random Password In Ruby

  • submit to reddit
        Aaron Blohowiak suggests adding this as a public method in user.rb:

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


Snippets Manager replied on Tue, 2009/01/06 - 7:48am

As I know, this is about Ruby 1.9 Many production servers still based on Ruby 1.8.6

Snippets Manager replied on Mon, 2012/05/07 - 1:36pm

Maybe this is more secure? ActiveSupport::SecureRandom.base64(6)

Snippets Manager replied on Tue, 2009/01/06 - 7:48am

I've wrote an article about generate random password before user has been saved Please take a look:

Snippets Manager replied on Mon, 2007/03/26 - 11:53am

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)}/#{}/#{}/#{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.