Checking whether bytes changed
A cryptographic hash maps an arbitrary-length byte string to a fixed-length value. Comparing a file with a trusted digest can detect a change, but the source of that trusted digest matters. An attacker who replaces both a message and its attached digest defeats a bare checksum scheme. Collision resistance concerns finding two different inputs with the same output; preimage resistance concerns finding an input for a given output. Since the output space is finite, collisions exist. The security property is computational difficulty, not a mathematical claim that every possible input has a different digest.
Checking knowledge of a shared secret
A message authentication code uses a shared secret key to produce a tag that a receiver can verify. HMAC uses a specified construction around a hash function; an improvised hash of key concatenated with message is not an interchangeable design. A MAC does not hide the message. Both parties holding the key can also create valid tags, which differs from a publicly verifiable digital signature. Separate the goal of rejecting modifications by outsiders from the goal of demonstrating authorship to an independent third party. These mechanisms answer different questions even when their outputs look like similar byte strings.
Handling confidentiality and authentication together
Symmetric encryption protects confidentiality between parties sharing a key. Authenticated encryption with associated data, or AEAD, also verifies integrity. For AES-GCM, reusing a nonce with the same key can destroy security, so nonce generation must remain safe across restarts and multiple writers. Associated data might include a document identifier. It is authenticated without being encrypted, helping bind a ciphertext to its intended context. A failed tag check must not release unauthenticated plaintext to application logic. Distinguish a nonce’s required uniqueness from a secret key’s confidentiality; making a nonce public is normally expected.
Design randomness and key lifetimes
Generate secret keys with a cryptographically suitable random-number generator, not a clock or predictable sequence. Nonce rules depend on the construction: some require uniqueness rather than randomness. Plan key compromise, rotation, decryption of older records and restoration from backups that could repeat counters. Password storage is another separate problem; a fast unsalted hash is not a suitable password-storage design. Use a dedicated, salted password derivation mechanism with an appropriate cost. Naming an approved primitive does not establish the security of its key management, surrounding protocol or error handling.