You are here: Forums Ask a Rails expert decrypting the password...
Posted in Forums : Ask a Rails expert
Authority 25
Posting Rating 2
Sign in to rate this post
|
Hi im mohd anas
i used Digest/sha2 to make password into “hashed password”. |
Authority 12
Posting Rating 95
Sign in to rate this post
|
Hi Anas I don’t think the passwords encrypted using Digest/sha2 can be retrieved Please refer |
Authority 37
Posting Rating 100
Sign in to rate this post
|
SHA is one-way encryption – you can’t convert it back to the plain string. However, you can always compare the encrypted value with some user input … Most authentication plugins in Rails do it like that, e.g. acts_as_authenticated …
# in the SessionController:
self.current_user = User.authenticate(params[:login], params[:password])
# somewhere in the User model:
def self.authenticate(login, password)
u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login] # need to get the salt
u && u.authenticated?(password) ? u : nil
end
def self.encrypt(password, salt)
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end
def encrypt(password)
self.class.encrypt(password, salt)
end
def authenticated?(password)
crypted_password == encrypt(password)
end
If you need some kind of “forgot password” functionality, the best idea would be to provide a way to reset the user password. You then create a new random string, encrypt it and send it to the user to log in. After that, they can change the password to anything they want. HTH |
Authority 12
Posting Rating 41
Sign in to rate this post
|
It is worth noting the reason for using a one way hash like sha2. You are adding an extra layer of protection in case your database is compromised. So, short of a brute force attack, there is no way to get the original password from the hash. As the previous poster shows, you authenticate the user by computing a hash of the clear password as typed by the user and compare it with the hash in the database. You do not try to reverse the hash and compare it with the clear password. |
Authority 37
Posting Rating 58
Sign in to rate this post
|
thank you Balaji & Clemens Kofler … for giving detailed information |
Ask a Rails expert : How to use mephisto
Ask a Rails expert : How to use mephisto
Ask a Rails expert : will_paginate customization problem
Ask a Rails expert : BackgroundRB still wants 'development' environment...?
Ask a Rails expert : activescaffold, sql exception
Ask a Rails expert : Passing non-english chars in query string
Ask a Rails expert : Rails and 2D barcodes
Ask a Rails expert : apache giving proxy error
Ask a Rails expert : Custom Responses w/ 'extra' information...?
Ask a Rails expert : Log rotation in rails