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



