Why do you need to Salt and Hash passwords? | Culttt (2024)

Jan 21, 2013

Table of contents:

  1. What is the problem?
  2. Hashing passwords
  3. Salting
  4. Types of hashing algorithm
  5. Example code
  6. Conclusion

Storing user passwords is a critical component for any web application. When you store a user’s password, you must ensure that you have it secured in such a way that if your data is compromised, you don’t expose your user’s password.

There have been many high profile cases of websites and web applications that have had their user details compromised. This is made even worse when the developers of the website have not stored the user’s password in a secure way.

If you are developing a website or web application that needs to store user data, it is incredibly important that you take the correct precautions should your data become exposed.

This is a guide to storing passwords securely and an example using PHP.

What is the problem?

When developing websites or web applications it is important to always have the outlook that everyone is out to get you. Generally you should assume that every point of your code is vulnerable to attack. You might be thinking, “there’s no way that anyone could get access to my database!”, but this is not the right attitude to have.

When you store a password in a database, you never want to store it in plain text. Storing a password in plain text would mean that anyone who looked through the database would be able to just read the user’s passwords.

Similarly, there are many ways to secure a password from prying eyes, but not all of them should be used.

If you do not take the right precautions when storing passwords, you could expose your user passwords to an attacker. Many people use the same email and password combination all over the Internet. If you expose their password, they could be left vulnerable on other websites or web applications as well.

It is your responsibility to take the right precautions!

Hashing passwords

“Hashing” passwords is the common approach to storing passwords securely. A “Hash” is a one-way function that generates a representation of the password. So when a user signs up for an account and they choose a password, the password is stored as the generated hash, rather than the actual characters that the user typed in. When you run a password through a hashing function, it will always produce the same output. When the user tries to log in with their email and password, the entered password is hashed again and then compared to what is stored in the database. If the two hashes are the same, the user has entered the correct password.

Hashes are impossible to convert back into plain text, but you don’t need to convert them back in order to break them. Once you know that a certain string converts to a certain hash, you know that any instance of that hash represents that string.

Hashing a password is good because it is quick and it is easy to store. Instead of storing the user’s password as plain text, which is open for anyone to read, it is stored as a hash which is impossible for a human to read.

Unfortunately, hashing a password is not nearly enough. It does not take very much computational power to generate a table of hashes of combinations of letters, numbers and symbols. Once you have this store of hashes, you can then compare the hash you want to crack and see if it matches. Once you find a match, you know the password.

Salting

In order to make it more difficult to expose a hash, you also need to salt it. Salting is where you add an extra bit of data to the password before you hash it. So for example, you would append every password with a string before hashing it. This would mean the string prior to hashing would be longer and therefore harder to find a match.

When the user comes to log back into your system, you simply take their entered password, append the salt and then hash it to see if it matches the hash you have stored.

Why is Salting important?

Salting is important because it adds a whole new level of required computational power in order to expose the hash. By adding a salt, you effectively render any lookup table useless. Hashing a password is not 100% secure as hashing alone is not that difficult to break. When you add a salt to the hash, you make it much more difficult to crack.

What should I use as a Salt?

Choosing the right strategy for salting is very important. Salting works best when the salt is completely unique for that user and for that instance of setting the password.

Many insecure systems do not use completely random salts. For example, I’ve seen systems that either use just the email address as a salt, or ever worse, they use the exact same string as a salt for every user. This is effectively pointless because once the salt is known for every user, it does not take much to generate a hash look up table. You also want the salt to be completely unique too. People use the same emails or usernames across many different websites. By using the email or username as the salt, you are opening the opportunity for a pre-made lookup table to be used.

Instead you should generate a unique salt for each user, and a unique salt whenever that user requires one, for instance when they change their password. There are many ways to do this which I will cover in the sample code at the end of this post.

How should I store a Salt?

You might be thinking, “If someone gets a hold of my hashed passwords, how can I also stop them from getting my salts? Where do I store them?”. The simple answer is, you can store your salt in the same User record as the User’s password. The added complexity of storing the salt in a different database is not worth the hassle.

If your user table is exposed by an attacker, having the salt and the hash still means they need an incredible amount of computational power in order to find the password. It is common practice to just store the two fields in the same table.

Types of hashing algorithm

There are a couple of different types of hashing algorithm that you should be aware of. Whilst hashing a password with any of these algorithms will render the password impossible to read for a human, you should still be careful about which one you choose because they are not all secure.

For a long time, MD5 was commonly used throughout the internet. However, MD5 is now accepted as being broken and too vulnerable to attack.

Another set of hashing algorithms is the SHA family.

Whilst it’s important to understand which hashing algorithms to avoid, I don’t think it is necessary to know the ins and outs of every single one. Generally you shouldn’t be making your own password encryption code because without expert knowledge of how the whole thing works you could be leaving yourself exposed. It’s much easier to just use a prewritten safe solution that I will show you below.

Example code

A nice prewritten solution for hashing passwords and creating salts is PBKDF2 (Password-Based Key Derivation Function 2). PBKDF2 is a key derivation function that was written by RSA Security.

As I mentioned above, there’s really no point in trying to write your own secure method when experts have already created a nice and easy to use solution for you.

So first thing we need to do is to create a new class to hold our methods and properties.

class Security{}

The first method we need to create is the PBKDF2 method for creating secure hashed values. This method takes a password, a salt, an iteration count and a derived key length. Also, as you can see I’m setting the default hash algorithm to sha256, but this can be overridden.

/** PBKDF2 Implementation (described in RFC 2898) * * @param string p password * @param string s salt * @param int c iteration count (use 1000 or higher) * @param int kl derived key length * @param string a hash algorithm * * @return string derived key */public function pbkdf2($p, $s, $c, $kl, $a = 'sha256') { $hl = strlen(hash($a, null, true)); // Hash length $kb = ceil($kl / $hl); // Key blocks to compute $dk = ""; // Derived key // Create key for ($block = 1; $block <= $kb; $block++) { // Initial hash for this block $ib = $b = hash_hmac($a, $s . pack('N', $block), $p, true); // Perform block iterations for($i = 1; $i < $c; $i ++) // XOR each iterate $ib ^= ($b = hash_hmac($a, $b, $p, true)); $dk .= $ib; // Append iterated block } // Return derived key of correct length return base64_encode(substr($dk, 0, $kl));}

Next we need a method for creating salts. Our salt just needs to be a unique string.

public function salt() { return uniqid(mt_rand(), true);}

Now to use our Security class, we could do something like this.

// The password the user entered$user_password = "fsdfdsjs";// Instantiate a new Security object$s = new Security();// Generate a Salt$salt = $s->salt();// Generate a secure password$secure_password = $s->pbkdf2($user_password, $salt, 1000, 32);// Store secure password and Salt in database

Now the next time the user attempts to login in we can just grab a copy of the stored password and salt based upon the email address or username the user entered from the database.

Next we can simply run the password the user entered through our hashing method and compare the entered password with the stored password.

if ($s->pbkdf2($user_password, $salt, 1000, 32) == $secure_password) { // Correct password}

Conclusion

And there you have it, a simple and secure way to store your user’s passwords. Storing passwords shouldn’t be difficult and should be a problem you can solve once. However, if you don’t do it properly, you could face a serious backlash should your database ever be compromised.

In this tutorial we used the PBDKF2 method for hashing passwords, but there are other ways too. One such method is using Bcrypt which I will cover in a future post.

Why do you need to Salt and Hash passwords? | Culttt (2024)

FAQs

Why do you need to Salt and Hash passwords? | Culttt? ›

Salting

Salting
In cryptography, a salt is random data fed as an additional input to a one-way function that hashes data, a password or passphrase. Salting helps defend against attacks that use precomputed tables (e.g. rainbow tables), by vastly growing the size of table needed for a successful attack.
https://en.wikipedia.org › wiki › Salt_(cryptography)
is important because it adds a whole new level of required computational power in order to expose the hash. By adding a salt, you effectively render any lookup table useless. Hashing a password is not 100% secure as hashing alone is not that difficult to break.

Why do you need to salt and hash passwords? ›

Password salting increases password complexity, making them unique and secure without affecting user experience. It also helps prevent hash table attacks and slows down brute-force and dictionary attacks.

Why is salting important in hashing? ›

Salting is the process of adding a unique value to the end of a password before hashing takes place. Salting the hash is crucial because it ensures that the encryption process results in a different hash value, even when two passwords are the same.

Why are salts used in passwords? ›

Salts create unique passwords even in the instance of two users choosing the same passwords. Salts help us mitigate hash table attacks by forcing attackers to re-compute them using the salts for each user.

What is the purpose of storing password hashes rather than the passwords themselves? ›

Password hashing specifically is used to turn plain-text passwords into a short string of letters and numbers using an encrypted algorithm, making them unreadable to people.

What is salt and hash password? ›

In contrast, hashing cannot be reversed — it is essentially a form of one-way encryption. Salting is different, again, because it doesn't involve converting the original plaintext but simply complicates the text with additional characters.

What is hash and salt password in SQL Server? ›

Currently supported versions of SQL Server and Azure SQL DB use a SHA-512 hash with a 32-bit random and unique salt. It is statistically infeasible for an attacker to deduce the password knowing just the hash and the salt.

What is the purpose of salting? ›

Salt acts as a preservative by altering the availability of water in foods, thereby depriving microbes from using available water as a nutrient. The growth of pathogens and spoilage organisms is impeded when salt is present.

What is the purpose of salting process? ›

Salting is one of the oldest food preservation methods. Salting is a process where the common salt (NaCl), sodium chloride, is used as a preservative that penetrates the tissue; hence slows the bacterial growth and deactivates the enzymes.

Why is salting done? ›

Salting preserves food by drawing water out of the food, preventing bacteria growing and spoiling the food. The food is surrounded in salt and left in a cool dry place. As water will be drawn out into the salt it may be necessary to pour the accumulated liquid out.

What is an example of a salted password? ›

To salt a password, append or prepend the salt (random characters) to the password value and then use a password hashing algorithm. For example: Password value is abc123. Salt value is saltQwoptyu@123.

Does salting protect weak passwords? ›

Salting has several benefits for your information security. First, it increases the entropy, or randomness, of your hashes, making them more resistant to brute-force attacks. Second, it protects your passwords from being exposed by common or weak passwords that are shared by many users.

What can I use as salt for passwords? ›

  • Don't use the username as the salt.
  • Use a cryptographically-secure pseudorandom number generator to generate salts.
  • Each password should have its own unique salt. Having a systemwide salt for all passwords isn't very effective.
  • The length of the salt should at least be as long as the hash output.

Why should passwords be hashed and not encrypted? ›

Hashing and encryption can keep sensitive data safe, but in almost all circ*mstances, passwords should be hashed, NOT encrypted. Because hashing is a one-way function (i.e., it is impossible to "decrypt" a hash and obtain the original plaintext value), it is the most appropriate approach for password validation.

What should you use to hash passwords? ›

To protect passwords, experts suggest using a strong and slow hashing algorithm like Argon2 or Bcrypt, combined with salt (or even better, with salt and pepper). (Basically, avoid faster algorithms for this usage.) To verify file signatures and certificates, SHA-256 is among your best hashing algorithm choices.

Why are hashes secure? ›

The hash function then produces a fixed-size string that looks nothing like the original. These algorithms are designed to be one-way functions, meaning that once they're transformed into their respective hash values, it's virtually impossible to transform them back into the original data.

Is hash and salt the same as encryption? ›

TL;DR: Encryption is a reversible process, whereas hashed data cannot be decrypted. Salting is a method to make hashing more secure.

What is the main purpose of key stretching? ›

In cryptography, key stretching techniques are used to make a possibly weak key, typically a password or passphrase, more secure against a brute-force attack by increasing the resources (time and possibly space) it takes to test each possible key.

How are hashed passwords checked? ›

Whenever a user logs into the software or app, the provided value will first be hashed and then checked with the hash stored in the database to verify the user's identity. In this way, even if hackers manage to obtain the hash, they cannot use it to log in.

Top Articles
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 6015

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.