# Storing encrypted password in to database

**URL:** <https://www.ruby-forum.com/t/storing-encrypted-password-in-to-database/145360>\
**Category:** Ruby\
**Created:** [August 8, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/storing-encrypted-password-in-to-database/145360 "2008-08-08T12:13:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [August 8, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/storing-encrypted-password-in-to-database/145360/1 "2008-08-08T12:13:59Z")

</div>

Hi,  
I want to store encrypted password in to database…  
If you have any solution plz reply

Thanx in advance

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [August 8, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/storing-encrypted-password-in-to-database/145360/2 "2008-08-08T13:07:44Z")

</div>

You can store the encrypted password along with the salt with which you  
encrypt it.If this password is for the login system then you can have a  
look  
at the plugin salted hash genereator.  
kranthi

On Fri, Aug 8, 2008 at 3:42 PM, Pragash Mr.

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [August 8, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/storing-encrypted-password-in-to-database/145360/3 "2008-08-08T14:29:28Z")

</div>

Pragash Mr. wrote:

> Hi,  
> I want to store encrypted password in to database…  
> If you have any solution plz reply
> 
> Thanx in advance

hash\_pw = Digest::MD5.hexdigest(params[:name] + params[:password])

Using the unique login name and password eliminates duplicates if a  
couple users decide to use the same password.

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [August 8, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/storing-encrypted-password-in-to-database/145360/4 "2008-08-08T14:33:19Z")

</div>

On Fri Aug 8 19:12:04 2008, Pragash Mr. wrote:

> Hi,  
> I want to store encrypted password in to database…  
> If you have any solution plz reply
> 
> Thanx in advance

This[1] is an excellent module for password hashing.

[1] [http://www.zacharyfox.com/blog/ruby-on-rails/password-hashing](http://www.zacharyfox.com/blog/ruby-on-rails/password-hashing)

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [August 8, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/storing-encrypted-password-in-to-database/145360/5 "2008-08-08T15:24:49Z")

</div>

Rick F. wrote:

> Pragash Mr. wrote:
> 
> > Hi,  
> > I want to store encrypted password in to database…  
> > If you have any solution plz reply
> > 
> > Thanx in advance
> 
> hash\_pw = Digest::MD5.hexdigest(params[:name] + params[:password])
> 
> Using the unique login name and password eliminates duplicates if a  
> couple users decide to use the same password.

Like MD5, there’s SHA1 as well. You’ll be fine using either with  
salting.

Digest::SHA1.hexdigest(string)

---

<div class="post-metadata">

**Author:** ![ganana\_srishti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@ganana\_srishti](https://www.ruby-forum.com/u/ganana_srishti)\
**Post date:** [August 8, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/storing-encrypted-password-in-to-database/145360/6 "2008-08-08T17:40:52Z")

</div>

On Aug 8, 8:28 am, Rick F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hash\_pw = Digest::MD5.hexdigest(params[:name] + params[:password])
> 
> Using the unique login name and password eliminates duplicates if a  
> couple users decide to use the same password.

Salt ([Salt (cryptography) - Wikipedia](http://en.wikipedia.org/wiki/Salt_%28cryptography%29)) serves the  
same purpose.

If you use salt then you have to store it in your db as well (or be  
able to derive it from other data in the db entry that will not  
change).

If you use the user name as your salt, then if you allow users to  
change their user names, you have to re-prompt them for their password  
(because you didn’t keep it sitting around in memory since they logged  
in, did you?).

Also, Pragash, the answers you’re finding here may not be what you  
were expecting (based on how you phrased your question). By using a  
digest (or cryptographic hash or one-way function – all the same  
thing), you provide no easy means of re-deriving the password from  
what was stored in the database. You asked about an “encrypted  
password”, which can imply an encryption key that could be used to  
perform a decryption to re-generate the password from the data stored  
in the database. You’re clearly after high security, so using a digest  
+salt is generally the way to go.

Eric

====

Ruby training and Rails training available at [http://LearnRuby.com](http://LearnRuby.com) .
