# Salted hash login generator

**URL:** <https://www.ruby-forum.com/t/salted-hash-login-generator/160566>\
**Category:** Rails\
**Created:** [February 17, 2009, 7:20pm UTC](https://www.ruby-forum.com/t/salted-hash-login-generator/160566 "2009-02-17T19:20:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunopires](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@brunopires](https://www.ruby-forum.com/u/brunopires)\
**Post date:** [February 17, 2009, 7:20pm UTC](https://www.ruby-forum.com/t/salted-hash-login-generator/160566/1 "2009-02-17T19:20:54Z")

</div>

Hello all.

Has anyone managed to use SaltedHashLoginGenerator with Rails 2.2.2?

I’ve been following the simple steps on the README file.

When I run script/generate salted\_login User Localization a  
/config/environment/user\_environment.rb file should be created, but  
isn’t.

When I include the line: require ‘environments/user\_environment’ in the  
config/environment.rb file and start the server, it complains that the  
user\_environment.rb file is missing.

Does anyone know how to fix this?

I’m using Aptana IDE with RadRails.

Thanks.

Bruno

---

<div class="post-metadata">

**Author:** ![brunopires](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@brunopires](https://www.ruby-forum.com/u/brunopires)\
**Post date:** [February 18, 2009, 11:35am UTC](https://www.ruby-forum.com/t/salted-hash-login-generator/160566/2 "2009-02-18T11:35:28Z")

</div>

Hi - if you can’t get this to work you could try building your own?

Class User

before\_create :create\_salt

def create\_salt  
unless self.salt  
self.salt = random\_salt  
end  
end

def random\_salt  
nums = (“0”…“9”).to\_a  
random = “”  
1.upto(5) { |i| newrand \<\< numbers[rand(numbers.size - 1)]}  
return random  
end

def hash\_password  
update\_attribute :password, Digest::MD5.hexdigest(“#{self.salt}#  
{self.password.downcase}”)  
end

def password\_matches? password\_to\_match  
password == Digest::MD5.hexdigest(“#{self.salt}#  
{password\_to\_match.downcase}”)  
end  
end

Just call hash\_password after you save the user or update the password  
and call password\_matches? to check that the password provided is the  
correct one.

Hope that helps?

On 17 Feb, 18:20, Bruno P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
