Information Security Stack Exchange is a question and answer site for information security professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to store an encrypted string of the password hash in a cookie and use the hash to lookup the user and log them in (if they want to be remembered).

Is this safe? The password is one-way hashed with SHA-512, 1024 iterations, using a timestamp for salt.

I imagine this would be unique because of the time-stamp salt.

share|improve this question
use server sessions. If you ever have to ask if it's safe, it isn't. – zzzzBov Dec 5 '11 at 19:23
Note: Make sure your cookies are "secure" (mitigates MITM attacks) and "httponly" (mitigates XSS attacks). – Brian Oct 2 '12 at 21:30
up vote 18 down vote accepted

No, this is not safe

You should generate a long random number and store it in a cookie. This random number is essentially just another password for this user. So, on the serverside, you only store a properly salted hash of this random number.

You should only give out each number once and it should only be valid for 1 login. So allow for more than one of those hashes for each user. Upon successfully logging in, generate a new random number, give the new number in the cookie you give it to your user and store the (salted) hash of this number on the server side.

(On a side note, MD5 is not deemed good enough anymore for password hashing and a salt should be random; a timestamp is not random enough. Search for password storage here for more information on that.)

share|improve this answer
Thanks for the info. I meant to say I use SHA-512, not MD5 for password encryption. – Mohamad Dec 5 '11 at 14:41
Ideally go for BCrypt Hash. Read: – Jacco Dec 5 '11 at 14:43
and also:, or maybe use the search box in the upper right corner. – Jacco Dec 5 '11 at 14:45
A salt does not have to be random, it does have to be unique to every user! – ewanm89 Dec 5 '11 at 14:56
I don't understand something, perhaps this is another question in itself. Why does it matter if the userId is know? There are many sites where you can view people's ids by navigating to their profile page: *.com/users/123 -- doesn't that depend on the security of the application itself? Why do we need to encrypt a userId/hash when the id is readily available by visiting someone's profile? – Mohamad Dec 5 '11 at 15:07

No. To verify this, you would have to store the hashed value on your server to compare against, otherwise you wouldn't be able to invalidate login sessions; or even worse you would need to store the plaintext password and timestamp used in order to regenerate the value. If you're going to be storing a value between two machines to compare, that value should be random.

While your method increases the work that would need to be done to compromise the system, it creates more complex work than storing a random value and it does expose a brute-forcing weakness -- especially where many browsers log the creation time of a cookie, thus exposing timestamps you were likely to use.

share|improve this answer
No it doesn't all I need is a cookie with the hash in. Easy to generate and even easier to sniff off the wire (unless over SSL). – ewanm89 Dec 5 '11 at 14:55
@ewanm89 Please expand on what you're trying to say. It sounds like you're describing a Firesheep-like attack which any session data can be vulnerable to. – Jeff Ferland Dec 5 '11 at 21:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.