14.1 · Hashing, MACs, encryption and signatures with CryptoKit
Apps use cryptography to answer four different questions: has this changed, who sent it, who can read it, and who vouches for it. Each has its own primitive, and mixing them up is the commonest crypto bug.
Mental model. Do not invent. Pick the primitive for the question you are asking, use the platform’s implementation, and let the library choose nonces and parameters.
The primitives
Hash, MAC, authenticated encryption and signatures.
- What is the difference between hashing, a MAC, encryption and a digital signature, and when do you use CryptoKit instead of CommonCrypto?
Four tools, four questions. A hash (SHA256.hash(data:)) maps any input to a fixed-size digest (32 bytes for SHA-256): the same input always gives the same…
Using them safely
Keys, nonces, derivation and common mistakes.
- How do you use cryptography safely: key management, nonces, key derivation, password storage and comparison?
Most crypto failures are not broken algorithms but misused ones. Keys: generate them with the system’s random source (SymmetricKey(size: .bits256)), never…
More in Security & privacy
- 14.2 · LocalAuthentication, the Secure Enclave, and App Attest
- 14.3 · Privacy manifests, tracking and ATT, and everyday data hygiene
- 14.4 · Deep links, secrets in the binary, and jailbreak detection
- 14.5 · A threat model for a consumer app
- 14.6 · CryptoKit in practice: AEAD, key agreement, signatures, one-time codes and tokens
- 14.7 · Trust in practice: pinning by public key, ATS scope, code signatures and Secure Enclave keys
- 14.8 · Defensive coding: secure decoding, injection, dependency integrity and file protection