Skip to content

Hash function

Also called: hash functions, cryptographic hash function, cryptographic hash functions

Published:

How does a hash function work?

NIST defines a hash function as one that maps a bit string of arbitrary length to a fixed-length bit string [1]. The output often serves as a condensed representation of the input and depends on the entire contents of the file or message, which is why NIST compares it to a fingerprint [1]. A cryptographic hash function is expected to offer collision resistance, preimage resistance and second-preimage resistance [1]. NIST-approved functions are specified in FIPS 180 and FIPS 202 [1].

Where are hash functions used?

FIPS 180-4 says the digests produced by its algorithms are used to detect whether a message has changed since the digest was generated [2]. NIST's definitions also mention uses such as digital signatures and key derivation [1].

A concrete example: in Python, hashlib.sha256(b"Nobody inspects the spammish repetition").hexdigest() returns a 64-character hex value starting with 031edd7d [3]. Feeding the same text in two pieces through update() yields exactly the same digest [3]. If the checksum on a download page matches the one you compute, the file arrived unchanged.

Is every hash function secure?

No. The Python documentation notes that MD5 and SHA-1 have known collision weaknesses [3]. A general-purpose hash is not enough for passwords either: naive approaches like sha1(password) do not resist brute-force attacks, and a good password hash should be tunable, slow and salted [3].

Frequently asked questions

Are MD5 and SHA-1 still safe to use?

Not for security. Python's documentation notes that both have known collision weaknesses.

Should I store passwords with SHA-256?

Not on its own. Use a slow, salted key-derivation function such as PBKDF2 or scrypt.

Sources

  1. hash function - Glossary — NIST CSRC
  2. FIPS 180-4, Secure Hash Standard (SHS) — NIST CSRC
  3. hashlib — Secure hashes and message digests — Python documentation

Related terms