Cyber Security: Discussing Password Hashing Algorithms

Introduction

Securing passwords is often an overlooked part of a developer’s efforts. It is not a glamorous topic, as it does not often focus on new and emerging technologies nor the user’s experience. However, hashing, which is a type of algorithm that essentially translates data (like a password) into different, fixed-size data. It is often used when developers want to compare a large sample data to a dataset. For example, if you wanted to query an object that is several MB against the database, it would be easier to hash it into a much smaller size first.

Why Hash passwords?

We have come a long way since passwords were stored as plaintext in a database. As it turns out, companies and services get hacked all the time. When these breaches happen, the biggest targets that hackers leak to the internet are users’ passwords. Because of this, and for many other reasons, developers store passwords as a hash in databases.

So, what actually qualifies as a hash? What are some of the most popular hashing algorithms, and what qualifies as a hashing function? All of these have been standardized, and, throughout the years, have had their encryption and cryptographic strength tested.

unsplash-logoJose Fontano

A hashing function, which is a function that takes in data and outputs the fixed length data, will have these attributes:

  • Fast hash value computation
  • Infeasible or impossible to generate a message with the given hash
  • Infeasible or impossible to change the input data without changing the resulting hash
  • Infeasible to find two inputs with the same resulting hash (collisions)

Some of the more well-known hashing algorithm families are as follows: MD-5, SHA-1, SHA-2 [SHA-256 and SHA-512], and SHA-3. Various implementations that derive and build upon the aforementioned families exist across many programming languages, such as bcrypt and scrypt.

The BAD: MD5/SHA1

MD-5 is a popular hashing algorithm, but, unfortunately, is weak and susceptible. Its weakness is that MD-5 hashes are prone to collisions, where two different inputs could potentially have the same hash result. For some more discussion on why MD-5 is not recommended anymore, consider visiting this article.

SHA-1 (Secure Hashing Algorithm) was the first in a series of cryptographic hash functions published by the National Institute of Standards and Technology. It is proven to be vulnerable to attacks for similar reasons to MD-5: two inputs can feasibly have the same resulting hash. With the advent of commercial cloud services, hackers have more ability and capacity to launch powerful attacks on SHA-1 and MD-5 hashed passwords that would have previously been impractical. For further discussion, consider reading a few Google Security Blogs here https://security.googleblog.com/2017/02/announcing-first-sha1-collision.html.

Password Salting

Hashing passwords typically require more than just using the well-known hashing algorithms. This is where the concept of “salt” comes into play with cyber security. A salt is a unique, non-secret value in a data store that is added to the password before the hash function is run with the input. Salts are used so that hackers cannot simply do large hash table lookups, where they run comparisons of passwords against hash equivalents (also known as rainbow attacks). The only stipulation to using a salt is that it must be unique in the database and not provided by a user.

unsplash-logoJason Tuinstra

Take for example the SHA-512 equivalent of the password “password”:

b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86  

Now, let’s see what the hashed version of it would look like if all our passwords were appended with a salt string of “[email protected]&59” to the end:

c2ddc75476c181eb1274771d573392993f7c1eaeb152fe371b92a985c9ffe297ecc979d56c0f3996f30c9493a1f5ea3956a341dc8aa6ebb19050888edc021fdb  

Should a hacker have retrieved the list of all a company’s hashed passwords, they would still run into a hashcode that does not resemble even the hashed version of “password.” Company “salts” that are added to passwords should be a heavily guarded secret, as they serve as one more line of defense for password security. If a salt were to become known, all a hacker would have to do is replicate what is done in the application code and append the salt.

Case Study: Java BCrypt

Enter into the stage bcrypt, which has become an industry standard for password hashing algorithms. With implementations across all the popular programming languages like Java, Python, C, Ruby, etc, bcrypt is well-known and trusted. Here are some of the reasons why bcrypt is very popular:

  • One-way hashing not stored as plain-text
  • Salted hashing to prevent rainbow table attacks
  • Key stretching, to mitigate brute force attacks. The hashing function is executed several times sequentially, meaning the function becomes CPU intensive.

An example is provided in a Java implementation, which can be cloned on Github:
git clone https://github.com/jeremyh/jBCrypt.git

Sample code:

package org.mindrot;

public class jBCryptExample {  
    public static void main(String[] args) {
        String password = "password";
        String candidate = "password";
        String hashed = BCrypt.hashpw(password, BCrypt.gensalt(12));

        if (BCrypt.checkpw(candidate, hashed)) {
            System.out.println("Candidate Plaintext: " + candidate);
            System.out.println("Plaintext Actual: " + password);
            System.out.println("Hashed Actual: " + hashed);
            System.out.println("Hashed candidate matches hashed actual!");

        } else {
            System.out.println("The candidate password does not match the actual password.");
        }
    }
}

The resulting output shows that the two strings match, since they were both the same String "password."

Candidate Plaintext: password  
Plaintext Actual: password  
Hashed Actual: $2a$12$v5rw.feVVaU/U8MP05OTeOGBrSDRoA4WBYfCXWdvAWrZuYgslWlvK  
Hashed candidate matches hashed actual!