liveBook · Manning

Hash function

As explained in Grokking Web Application Security

A hashing algorithm deterministically maps input data to a fixed-size hash value while being designed to make different inputs produce different values with high probability.

A hash function turns input data into a fixed-size hash value. Give it the same input and it reliably returns the same value; different inputs almost always produce different values. That makes a hash useful as a compact fingerprint for data: compare fingerprints to tell whether data is the same or has changed, without retaining the original data. section 2.6 section 3.2

Hashing is one-way: a hash value cannot be decrypted back into its original input. An attacker trying to discover an input from its hash must instead use brute force, repeatedly hashing possible inputs until one matches. A collision is the near-zero-chance event in which two different inputs produce the same hash. section 3.2

Detecting a changed script

A script loaded from a CDN can be replaced by an attacker who compromises the CDN or intercepts traffic. Subresource Integrity (SRI) stores a SHA-384 hash in the script tag. The browser hashes the downloaded script and compares the result with that expected value. If they differ, the browser treats the script as changed and does not execute it. Generate the integrity value during the build process, fix it at deployment, and use SRI whenever possible as an extra defense against malicious edits and MITM injection. section 2.6

<script src="/js/application.js"
integrity="sha384-5O3lno38vOKjoSa8HT863w10M7hKzvj+
HjknFmPkOJz50htAHuPtPLj6J6lfziE">

The integrity attribute contains the expected SHA-384 hash for a script; the browser compares it with a newly calculated value before execution. section 2.6

Checking passwords without storing them

Store a password hash rather than a plain-text password. When someone logs in, hash the newly entered password and compare that result with the stored hash. This reduces the damage if an attacker obtains a database backup or database credentials: password hashes are harder to turn into usable passwords than plain-text values. Use a strong hashing algorithm such as bcrypt; older algorithms such as MD5 are weak because available computing power has increased. section 3.8

Use a hash function when you need to recognize data without keeping or exposing the original value:

  • Compare a newly calculated hash with an expected hash to detect unexpected changes to scripts or other data. section 2.6
  • Store hashes for credentials so a login can be checked by recalculating and comparing a hash. section 3.8
  • Retain a hash instead of a large or sensitive input when you need to know whether the same value appears again. section 3.2

How Grokking Web Application Security covers it

How deep it goes
Explains deterministic, one-way behavior, collisions, brute force, and hash comparison through security scenarios.
Examples
Uses CDN script verification and Ruby bcrypt password hashing with a salt and comparison.
What it assumes
Introduces hash behavior in plain language before using SHA-384 and bcrypt examples.
What you can do after
Apply it via Subresource Integrity and bcrypt to detect tampering and verify passwords without storing plain text.
How it connects
Connects hashing with Subresource Integrity, SHA-384, bcrypt, MD5, encryption, and brute force.

Also explained in

Compare how 16 books explain hash function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Web Application Security. The section links point to the parts of the book it draws on.