3

This question is based on the answer.

I would like to know how you can hash your password by SHA1 and then remove the clear-text password in a MySQL database by Python.

How can you hash your password in a MySQL database by Python?

5
  • 2
    Don't use SHA1 if you don't have to. SHA2-256 or SHA2-512 are much more secure options, and if this is a new application there is no backwards compatibility requirement to use SHA1. Commented Jul 25, 2009 at 22:22
  • 1
    For passwords, SHA1 might even be overkill. A good salt and MD5 is plenty strong enough for most applications. Commented Jul 25, 2009 at 22:24
  • 5
    Why risk it? It's an entirely trivial change to use a better hash algorithm. (You just change hashlib.md5 to hashlib.sha512!) MD5 is dead, and anyone who doesn't know that shouldn't go anywhere near passwords. Commented Jul 25, 2009 at 22:49
  • fixed title - thanks for comments! Commented Jul 26, 2009 at 4:17
  • Listen to kquinn, not Thomas Owens. There's no reason to use low grade options. Commented Jun 27, 2011 at 6:37

4 Answers 4

12

As the documentation says you should use hashlib library not the sha since python 2.5.

It is pretty easy to do make a hash.

hexhash = hashlib.sha512("some text").hexdigest()

This hex number will be easy to store in a database.

Sign up to request clarification or add additional context in comments.

1 Comment

Better to do hexhash = hashlib.sha512("some text" + salt).hexdigest(), where salt is a random string generated for each password and stored in the DB along with the hash. It helps avoid rainbow table attacks.
7

If you're storing passwords in a database, a recommended article to read is Jeff's You're Probably Storing Passwords Incorrectly. This article describes the use of salt and some of the things about storing passwords that are deceptively easy to get wrong.

Comments

4

http://docs.python.org/library/sha.html

The python documentation explains this a lot better than I can.

1 Comment

It's depreciated since Python 2.5, you should now use docs.python.org/library/hashlib.html#module-hashlib (Although, I'm sure this answer was valid when it was posted in 2009).
1

You don't remove the clear-text password when you hash the password. What you do is accept an input from the user, hash the input, and compare the hash of the input to the hash stored in the database. You should never store or send the plain-text password that the user has.

That said, you can use the sha library as scrager said (pre-Python 2.5) and the hashlib library as David Raznick said in newer versions of Python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.