A cryptographic hash function converts input of any size into a fixed-size digest. The same input always produces the same digest, while changing the input changes the result. Its one-way property makes recovering the original input infeasible, and collision resistance makes finding two different inputs with the same digest infeasible. section 4.2.1
Hashes are useful for detecting accidental data corruption, not for proving who changed a file. If an attacker can modify both a file and its separate hash, a plain hash check cannot detect the tampering. Use an HMAC when authenticity is also required. section 4.2.2 section 4.7
Validating a refund file
A warehouse application writes refunds.json and a corresponding refunds.json.sha256 file. Before issuing refunds, the payment service hashes refunds.json and compares the result with the expected digest. Matching values allow processing; a mismatch raises an error and stops all refunds. Rejecting a valid file because its companion hash is corrupt is safer than processing an invalid one. section 4.2 section 4.2.2
Apply cryptographic hashes with these practical rules:
- Choose standardized, peer-reviewed algorithms rather than designing or implementing a hash function yourself. section 4.2.1
- Prefer current secure algorithms such as SHA-2 or SHA-3; treat MD5 and SHA-1 as insecure and retain them only for historical compatibility. section 4.2.2 section 4.3
- Design formats, schemas, and data structures for algorithm changes by recording an algorithm version, while preventing downgrade to insecure versions. section 4.2.3
- Distinguish cryptographic hashes from Java hashCode(): a digest is a security-oriented fixed-size value, whereas hashCode() is a 32-bit hash-table value. section 4.7
Hashing with Java
Java exposes standard hash implementations through java.security.MessageDigest. Requesting MessageDigest.getInstance("SHA3-256"), reading a file, digesting its bytes, and formatting the result as hexadecimal produces a repeatable digest without depending on a vendor-specific cryptography API. section 4.3
Figure 4.2 ACME staff members approve refunds using the warehouse management application. Once a day, the application generates a refunds.json file. The payment service refunds customer credit cards for the amount specified in that file.
Trace the refund workflow from warehouse approval through file generation to payment-service validation and controlled refund processing. section 4.2



