Security Hashing – What Makes a Hashing Algorithm Secure?

hashingSecurity

After reading this interesting question, I felt like I had a good idea of which insecure hashing algorithm I'd use if I needed one, but no idea why I might use a secure algorithm instead.

So what is the distinction? Isn't the output just a random number representing the hashed thing? What makes some hashing algorithms secure?

Best Answer

There are three properties one wants from every cryptographic hash function H:

  • preimage resistance: Given h, it should be hard to find any value x with h = H(x).

  • second preimage resistance: Given x1, it should be hard to find x2 != x1 with H(x1) = H(x2).

  • collision resistance: It should be hard to find two values x1 != x2 with H(x1) = H(x2).

With hash functions as used in common programming languages for hash tables (of strings), usually none of these is given, they only provide for:

  • weak collision resistance: For randomly (or "typically") selected values of the domain, the chance of collision is small. This says nothing about an attacker intentionally trying to create collisions, or trying to find preimages.

The three properties above are (among) the design goals for every cryptographic hash function. For some functions (like MD4, SHA-0, MD5) it is known that this failed (at least partially). The current generation (SHA-2) is assumed to be secure, and the next one ("Secure Hash Algorithm 3") is currently in the process of being standardized, after a competition.

For some uses (like password hashing and key derivation from passwords), the domain of actually used values x is so small that brute-forcing this space becomes feasible with normal (fast) secure hash functions, and this is when we also want:

  • slow execution: Given x, it takes some minimum (preferably configurable) amount of resources to calculate the value H(x).

But for most other uses, this is not wanted, one wants instead:

  • fast execution: Given x, calculating the value of H(x) is as fast as possible (while still secure).

There are some constructions (like PBKDF2 and scrypt) to create a slow hash function from a fast one by iterating it often.

For some more details, have a look at the hash tag on our sister site Cryptography Stack Exchange.