liveBook · Manning

SHA-256

As explained in Software Security for Developers

SHA-256 is a member of the SHA-2 family of cryptographic hash algorithms that produces a 256-bit digest.

SHA-256 is a cryptographic hash algorithm in the SHA-2 family. It converts input of any size into a repeatable 256-bit digest, making changes to files detectable. section 4.2.2 section 4.3

A warehouse service can publish refunds.json alongside refunds.json.sha256. The payment service hashes the received file and processes refunds only when the computed and expected digests match. section 4.2.2 section 4.4

Table 4.3 Pseudocode for using a cryptographic hash function to validate file integrity
Warehouse Management Application
Payments Service
saveRefunds(Path location, byte[] content)
hashValue = CryptoUtils.sha256(content)
writeFile(location, content)
writeFile(location + ".sha256", hashValue)
isRefundsFileValid(Path location)
content = readFile(location)
expected = CryptoUtils.sha256(content)
actual = readFile(location + ".sha256")
if (expected == actual) return true
else return false

Follow the producer-and-consumer pseudocode: compute the digest, store it beside the content, recompute it later, and compare the values. section 4.2.2

Use SHA-256 with these boundaries in mind:

  • Matching digests detect accidental corruption from network errors, disk failures, or accidental edits. section 4.2.2
  • A plain digest does not authenticate its source: an attacker can modify both a file and its accompanying digest. section 4.2.2 section 4.7
  • For integrity plus authentication, use HMAC with SHA-256 and protect its secret key; do not replace HMAC with simple key-and-message concatenation. section 4.5.2 section 4.7

Java usage

Java requests the standard algorithm by name with MessageDigest.getInstance("SHA-256"), digests the input bytes, and can encode the result as hexadecimal for storage or comparison. section 4.3

How Software Security for Developers covers it

How deep it goes
Explains repeatable hashing, comparison, rejection behavior, and active-tampering limits, but defers protection against malicious modification to HMAC.
Examples
Uses refunds.json and refunds.json.sha256, a warehouse-to-payments workflow, and Java digest computation.
What it assumes
Assumes basic files and byte arrays, while distinguishing cryptographic hashes from Java hashCode() and explaining integrity from fundamentals.
What you can do after
Apply it via Java MessageDigest to compute and compare file digests, while recognizing when HMAC is required.
How it connects
Connects cryptographic hash functions and message integrity with SHA-2, Java MessageDigest, HMAC, and authentication.

Also explained in

Compare how 4 books explain SHA-256 →

Related concepts

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