Browse Questions
  • Tasks support cooperative cancellation — you cancel from outside, and the task must check for cancellation and stop itself.
  • Encrypt mode makes the payload unreadable to anyone but the named peer and also proves its origin, while sign/verify mode leaves the payload in the clear and only proves who produced it and that…
  • The three modes differ in where the authentication token lives and whether the output grows: Seal appends the token to the ciphertext, Token Protect returns the token separately so the ciphertext…
  • They're often confused because both involve "hiding" something, but they hide different things for different reasons: - Encapsulation hides internal state to protect it from invalid mutation (an…

Answer: The three modes differ in where the authentication token lives and whether the output grows: Seal appends the token to the ciphertext, Token Protect returns the token separately so the ciphertext stays the same length as the plaintext, and Context Imprint produces length-preserving output with no token and therefore no integrity check.

ModeOutput sizeIntegrity checkUse when
Sealplaintext + ~44 bytesYesDefault — you control the storage
Token Protectexactly plaintext length, plus a separate tokenYesFixed-width DB column; token stored in another column
Context Imprintexactly plaintext lengthNoLegacy formats with zero room for overhead

Context Imprint is a last resort. Without a token it cannot detect tampering, and it requires a non-empty context to be secure at all.

Code Example:

// Seal — one blob, simplest
let sealed = try TSCellSeal(key: key)!.encrypt(data)

// Token Protect — ciphertext and token stored separately
let cell = TSCellToken(key: key)!
let result = try cell.encrypt(data, context: context)
store(cipherText: result.cipherText, token: result.token)
let back = try cell.decrypt(result.cipherText, token: result.token, context: context)

// Context Imprint — no integrity guarantee, context is mandatory
let imprint = TSCellContextImprint(key: key)!
let same = try imprint.encrypt(data, context: context)  // same length as `data`

Key Points:

  • Default to Seal unless a storage constraint forces otherwise.
  • Token Protect keeps the ciphertext column width unchanged.
  • Context Imprint trades integrity for size — never use it with an empty context.