I have been able to crack passwords, given their salts and their hashes, by using brute force. In the first place, the length of passwords was 3 and the salt length 2: e.g., hash: rrVo/xC.s5/hQ. If the hackers find a match, bingo! They have your password. To solve this problem, however, we need only salt the hash. To salt a hash, we simply come up with a random-looking string of text, concatenate it with the password supplied by the user, then hash both the randomly generated string and password together as one value.
- Build hash tables with each salt for cracking each hash. This makes it hard to crack multiple hashes at a time. But still possible to crack the selected hashes, consider the admin one. Consider the example: We could extract the salt, but as different hash will be having a different salt, it’s impossible to crack all hashes at a stretch.
- Prepend the salt to the password and hash it with a standard password hashing function like Argon2, bcrypt, scrypt, or PBKDF2. Save both the salt and the hash in the user's database record. To Validate a Password. Retrieve the user's salt and hash from the database. Prepend the salt to the given password and hash it using the same hash function.
Active2 months ago
This code is supposed to hash a password with a salt. The salt and hashed password are being saved in the database. The password itself is not.
Given the sensitive nature of the operation, I wanted to make sure everything was kosher.
Mark Amery73.3k3737 gold badges280280 silver badges331331 bronze badges
Chris DutrowChris Dutrow21.7k5151 gold badges160160 silver badges234234 bronze badges
8 Answers
EDIT: This answer is wrong. A single iteration of SHA512 is fast, which makes it inappropriate for use as a password hashing function. Use one of the other answers here instead.
Looks fine by me. However, I'm pretty sure you don't actually need base64. You could just do this:
If it doesn't create difficulties, you can get slightly more efficient storage in your database by storing the salt and hashed password as raw bytes rather than hex strings. To do so, replace
Mark Ameryhex
with bytes
and hexdigest
with digest
.73.3k3737 gold badges280280 silver badges331331 bronze badges
TaymonTaymon17.5k66 gold badges4848 silver badges7777 bronze badges
Based on the other answers to this question, I've implemented a new approach using bcrypt.
Why use bcrypt
If I understand correctly, the argument to use
bcrypt
over SHA512
is that bcrypt
is designed to be slow. bcrypt
also has an option to adjust how slow you want it to be when generating the hashed password for the first time:Slow is desirable because if a malicious party gets their hands on the table containing hashed passwords, then it is much more difficult to brute force them.
Implementation
Notes
I was able to install the library pretty easily in a linux system using:
However, I had more trouble installing it on my windows systems. It appears to need a patch. See this Stack Overflow question: py-bcrypt installing on win 7 64bit python
Mark Amery73.3k3737 gold badges280280 silver badges331331 bronze badges
Chris DutrowChris Dutrow21.7k5151 gold badges160160 silver badges234234 bronze badges
The smart thing is not to write the crypto yourself but to use something like passlib: https://bitbucket.org/ecollins/passlib/wiki/Home
It is easy to mess up writing your crypto code in a secure way. The nasty thing is that with non crypto code you often immediately notice it when it is not working since your program crashes. While with crypto code you often only find out after it is to late and your data has been compromised. Therefor I think it is better to use a package written by someone else who is knowledgable about the subject and which is based on battle tested protocols.
Also passlib has some nice features which make it easy to use and also easy to upgrade to a newer password hashing protocol if an old protocol turns out to be broken.
Also just a single round of sha512 is more vulnerable to dictionary attacks. sha512 is designed to be fast and this is actually a bad thing when trying to store passwords securely. Other people have thought long and hard about all this sort issues so you better take advantage of this.
Ben Hoyt6,71544 gold badges4545 silver badges7878 bronze badges
M.D.M.D.
For this to work in Python 3 you'll need to UTF-8 encode for example:
Otherwise you'll get:
Traceback (most recent call last):
File ', line 1, in
hashed_password = hashlib.sha512(password + salt).hexdigest()
TypeError: Unicode-objects must be encoded before hashing
File ', line 1, in
hashed_password = hashlib.sha512(password + salt).hexdigest()
TypeError: Unicode-objects must be encoded before hashing
![Crack Hash Password With Salt Crack Hash Password With Salt](https://i.stack.imgur.com/jqeUd.png)
11.9k1414 gold badges8484 silver badges113113 bronze badges
WayneWayne
passlib seems to be useful if you need to use hashes stored by an existing system. If you have control of the format, use a modern hash like bcrypt or scrypt. At this time, bcrypt seems to be much easier to use from python.
passlib supports bcrypt, and it recommends installing py-bcrypt as a backend: http://pythonhosted.org/passlib/lib/passlib.hash.bcrypt.html Memorex manual downloads.
You could also use py-bcrypt directly if you don't want to install passlib. The readme has examples of basic use.
see also: How to use scrypt to generate hash for password and salt in Python
Community♦
Terrel ShumwayTerrel Shumway
I don' want to resurrect an old thread, but.. anyone who wants to use a modern up to date secure solution, use argon2.
It won the the password hashing competition. ( https://password-hashing.net/ ) It is easier to use than bcrypt, and it is more secure than bcrypt.
nagylzsnagylzs1,75833 gold badges2323 silver badges3737 bronze badges
As of Python 3.4, the
hashlib
module in the standard library contains key derivation functions which are 'designed for secure password hashing'.Crack Password Hash With Salt
So use one of those, like
hashlib.pbkdf2_hmac
, with a salt generated using os.urandom
:Note that: Best free video editing software windows 10.
- The use of a 16-byte salt and 100000 iterations of PBKDF2 match the minimum numbers recommended in the Python docs. Further increasing the number of iterations will make your hashes slower to compute, and therefore more secure.
os.urandom
always uses a cryptographically secure source of randomnesshmac.compare_digest
, used inis_correct_password
, is basically just the operator for strings but without the ability to short-circuit, which makes it immune to timing attacks. That probably doesn't really provide any extra security value, but it doesn't hurt, either, so I've gone ahead and used it.
For theory on what makes a good password hash and a list of other functions appropriate for hashing passwords with, see https://security.stackexchange.com/q/211/29805.
Password Hash Salt
Mark AmeryMark Amery73.3k3737 gold badges280280 silver badges331331 bronze badges
Crack Hash Password With Salt And Vinegar
Firstly import:-
Then change your code according to this in your method:
Then pass this salt and uname in your database sql query, below login is a table name:
Pranav Totla79211 gold badge77 silver badges2323 bronze badges
Sheetal JhaSheetal Jha