Hashing Techniques for Password Storage (2024)

Picture yourself a year from now. Someone just put your entire password database on Pastebin. User accounts are being hacked by the thousands and your local privacy regulator just called to have a chat. Depending on the hashing algorithm you were using to store your user passwords, you could be in a lot of trouble. As they say, an ounce of prevention is worth a pound of cure, so how does hashing work and what are the best hashing techniques for password storage?

Below, we’ll go over the dos and dont’s of password hashing and shed some light on the differences between some of the better-known algorithms.

Hashing Techniques Explained

At its core, a hash function takes a string input and turns it into a garbled string called a hash (or digest). And while that may sound simplistic, password hashing functions have several key properties.

There are one-way functions. While a hash function can generate a digest from a given input, it is computationally prohibitive to reverse the function and produce the input given only a digest.They are deterministic. Hashing functions will produce the same digest given the same input, every time.They are also unique. No two inputs will ever produce the same digest.

By hashing a password according to best practices and storing the digest, a web site can prevent leaking a user’s raw (plain text) password in the event that its password database is breached. If an attacker breaches a database of password hashes, they wouldn’t have access to users’ plain text passwords, which could be used to compromise their identities.

Evaluating Hashing Algorithms

There are many hashing algorithms that people have used to hash passwords. Two of the most common hashing algorithms you may have come across are MD5 and the SHA-* family of algorithms (SHA-1, SHA-2, SHA-3), but there are several reasons not to use these.

For starters, they are extremely fast. In most areas of computing, you want fast algorithms, but for password hashing, the opposite is true. A slow hashing function makes brute-forcing passwords an expensive, time-consuming process thereby protecting organizations from such attacks.

Cryptographic hashing algorithms like MD5 and the SHA family of algorithms make it easier for attackers to brute force passwords as attackers can use a lot of compute resources (CPUs, GPUs, etc.) to make a lot of password “guesses” in a short period of time.

Another problem with these algorithms is that they don’t natively use a salt, which is a useful tool in protecting hashed passwords. Unless coerced, users often take the path of least resistance by using passwords that are easily guessed. An attacker can hash lots of common passwords then search a database of stolen password digests to find a match.

A salt can fix that problem. Salts are random, unique strings that are either appended or prepended to the user’s password before hashing. Because the salt changes the input, it produces a unique hash that won’t match with any stolen digests. Last.fm and Lifeboat both used unsalted MD5 hashes for their password storage, which made it extremely easy for attackers to find all of their users’ plain text passwords.

Password Hashing Functions

In stark contrast to cryptographic hashing functions which are built for speed, password hashing functions are designed to be slow to compute and to use as many resources as possible to make brute force attacks slower and more expensive. There are three primary password hashing functions you should know about when storing passwords.

The first is bcrypt. Not only does bcrypt include salts, it also makes brute force attacks substantially more difficult. bcrypt is considered “CPU-hardened”, which means that computing a hash requires many CPU cycles. You can also set the number of iterations using a work factor which makes it exponentially harder to brute force. As computers get faster and compute resources become less expensive, you can simply increase the bcrypt work factor to increase resource consumption.

Another option is scrypt. Like bcrypt, scrypt is a CPU-hardened function, but it also has the advantage of being memory-hard. That means it deliberately consumes more memory resources. In addition to specifying how much compute resources are required to compute an scrypt hash, you can also control how much memory is required. This means you can make computing scrypt hashes expensive by forcing attackers to acquire a large amount of both compute and memory resources.

Another less-established but promising hashing mechanism, Argon2, seems to offer the most flexibility yet. Argon2 adds a third dimension of complexity/cost: the number of threads used to compute the hash. This means that attackers need to have not only a large amount of compute and memory resources available but also more physical CPU cores. This makes brute force attacks substantially more expensive to execute.

As you look for the right hashing approach, the main thing to remember is that you shouldn’t try and roll your own hashing functions. It’s an incredibly complex space with some rarified math. Instead, use established libraries (the more established the better) and accepted algorithms. A little due diligence and extra care now will save your organization considerable embarrassment in the future, not to mention potential regulatory fines.

Further Security Reading

Interested in more developer-focused security content? Check out some of our other web security articles:

  • Add Authentication to Any Web Page in 10 Minutes
  • Simple Node Authentication
  • How to Prevent Your Users from Using Breached Passwords
  • Announcing PassProtect - Proactive Web Security

We’ve also got a new security site where we’re exclusively publishing security-focused articles and guides that you might find interesting.

Hashing Techniques for Password Storage (2024)

FAQs

What is the best method to hash passwords? ›

While Argon2id should be the best choice for password hashing, scrypt should be used when the former is not available. Like Argon2id, scrypt has three different parameters that can be configured: the minimum CPU/memory cost parameter (N), the blocksize (r) and the degree of parallelism (p).

Which technique is used commonly used for password storage? ›

A: Among various techniques which are common for securely storing passwords, hashing, and encryption are used. Hashing is more effective as it's one-way and it's quite impossible to decrypt to obtain the original value.

Is SHA-256 secure for passwords? ›

It produces a fixed-size output of 256 bits, regardless of the input size. Known for its security, SHA-256 is widely used to hash passwords, making them resistant to various attacks.

What is an example of password hashing? ›

1 Hashing basics

The same input always generates the same hash, but different inputs produce different hashes. For example, the input "password" might produce the hash "5f4dcc3b5aa765d61d8327deb882cf99", while the input "passw0rd" might produce the hash "6c569aabbf7775ef8fc5705a9f1f9b2f".

What is the difference between SHA-256 and bcrypt? ›

The main difference between bcrypt and SHA256 is that bcrypt is created to calculate the hash as slowly as possible without hindering users, whereas SHA256 is designed to be computationally fast.

What is the most commonly used hashing algorithm today? ›

The MD5 algorithm, defined in RFC 1321, is probably the most well-known and widely used hash function.

What is the difference between SHA-256 and SHA 512? ›

The primary difference between SHA-256 and SHA-512 is the word size; SHA-256 uses 32-byte words whereas SHA-512 uses 64-byte words. There are also modified versions of each standard, known as SHA-224, SHA-384, SHA-512/224, and SHA-512/256.

Which is a strong password storage strategy? ›

Using password managers

Instead of writing your passwords on paper where someone might find them, you can use a password manager to store them securely online. Password managers can remember and enter your password on different websites, which means you won't have to remember longer passwords.

Which datatype is best for storing passwords? ›

Using a char[] is certainly a safer option than using a String for handling passwords in Java. It should be noted though, that even if you are using a char[] , you should still be hashing passwords for security. Consider the following guide from the Open Web Application Security Project on password storage.

What is the safest password hash? ›

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.

Is SHA-256 outdated? ›

"SHA-2" is the traditional codename for a family of six functions that includes SHA-256 and SHA-512. These functions are considered completely fine and current and non-obsolete.

What is the weakness of SHA-256? ›

3 What are the disadvantages of SHA256? SHA256 is not a perfect solution and has some drawbacks, such as being slower and more computationally intensive than MD5 and SHA-1, which can affect the performance and efficiency of applications or systems.

What is the algorithm for password encryption? ›

Hash function: The algorithm that uses the key to create password encryption and decryption. A hash function is essentially a piece of code that runs every time someone saves a password or logs into an application. Hash: A random series of numbers and letters representing your password.

What hashing algorithm does Windows 10 use to store passwords? ›

The password is hashed by using the MD4 algorithm and stored.

What type of hashing algorithm was used to protect passwords Goldman Sachs? ›

Ans: Md5 2)What level of protection does the mechanism offer for passwords? Ans: MD5 is insecure and provides a very low level of protection and should not be used in any application.

Top Articles
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6170

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.