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.

  1. What is the difference between hashing, a MAC, encryption and a digital signature, and when do you use CryptoKit instead of CommonCrypto? 14.1.1 · L3
    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.

  1. How do you use cryptography safely: key management, nonces, key derivation, password storage and comparison? 14.1.2 · L5
    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