You are here: Forums Ask a Rails expert decrypting the password...
Posted in Forums : Ask a Rails expert
Authority 25
Posting Rating 13
Sign in to rate this post
|
Hi im mohd anas
i used Digest/sha2 to make password into “hashed password”. |
Authority 12
Posting Rating 89
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 95
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 25
Posting Rating 0
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 36
Sign in to rate this post
|
thank you Balaji & Clemens Kofler … for giving detailed information |
Ask a Rails expert : Tracking down an issue
Ask a Rails expert : Thread Vs Transaction
Ask a Rails expert : implementing whitelist plugin
Ask a Rails expert : Validation helper
Ask a Rails expert : FILE EDIT/DELETE
Ask a Rails expert : decrypting the password
Ask a Rails expert : Static Objects in Rails
Ask a Rails expert : Newbie - Rake Cant Execute Test
Ask a Rails expert : Call Web service
Ask a Rails expert : RMagick issues