liveBook · Manning

Hash value

As explained in Grokking Web Application Security

A hash value is the fixed-size digest produced by applying a hashing algorithm to input data.

A hash value, or hash, is the output produced by running data through a hashing algorithm. Think of the algorithm as a reliable sausage machine: the same input produces the same hash value, while a changed input will almost always produce a different one. That makes a hash useful for checking whether data is still the data you expected. section 2.6 section 3.2

A hash is not a copy of the original input. Hashing is one-way in the sense that the hash cannot be decrypted to recover the input. An attacker trying to discover an input from its hash must instead try many possible inputs until one produces a matching value. Different inputs producing the same hash are called a hash collision, but the likelihood is described as near-zero. section 3.2 section 3.8

Hash values provide practical security checks in several situations:

  • Protect a loaded JavaScript file with subresource integrity. Generate a SHA-384 hash during the build process, fix it in the integrity attribute at deployment, and let the browser recalculate and compare the hash when loading the script. A mismatch indicates an unexpected change, so the browser does not execute the script. section 2.6
  • Store a password hash rather than the password itself. When someone logs in, hash the newly entered password and compare that result with the stored hash to decide whether it is correct. section 3.8
  • Detect changed or repeated data without retaining the original data, which can help when the input is too large to keep or should not be stored for security reasons. This also supports detection of suspicious web-server events. section 3.2

Rejecting a modified script

A page can attach the expected SHA-384 hash value to a script URL through an integrity attribute. If an attacker changes the hosted JavaScript while it is being delivered, the browser's recalculated hash no longer matches the expected value. The browser can then treat the script as unintended code and refuse to run it, adding protection against MITM attacks and malicious edits. section 2.6

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

The integrity attribute holds the expected SHA-384 hash value for the script. It gives the browser a value to compare against the script it actually receives. section 2.6

Hashing reduces the damage from password-database exposure because attackers do not receive plain-text passwords. It does not remove risk: weak older algorithms such as MD5 are no longer considered strong as computing resources have grown. Use a strong password-hashing algorithm such as bcrypt, and treat hashes as security-sensitive records even though they cannot simply be decrypted. section 3.8

How Grokking Web Application Security covers it

How deep it goes
Defines deterministic and one-way behavior, then demonstrates SRI and bcrypt comparisons without detailing hashing-algorithm internals.
Examples
Uses an SHA-384 integrity attribute for JavaScript and a salted bcrypt password-checking snippet.
What you can do after
Apply it via browser integrity attributes and bcrypt-style password hash comparison.
How it connects
Connects hashing to subresource integrity, MITM attacks, password storage, hash collisions, and brute force.

Also explained in

Compare how 7 books explain hash value →

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.