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















