CryptoKit for Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is CryptoKit and why is it needed?

\n\n

CryptoKit is a framework for cryptographic operations developed by Apple and built directly into the Swift ecosystem. It is available starting from iOS 13, macOS 10.15, watchOS 6, and tvOS 13. Unlike third-party libraries, CryptoKit is a native solution, which guarantees high performance, security, and full integration with the Secure Enclave key management system.

\n\n

Why is CryptoKit needed? In modern application development, data security comes to the forefront. CryptoKit provides developers with a simple yet powerful interface for:

\n
    \n
  • Data hashing (SHA-256, SHA-384, SHA-512)
  • \n
  • Symmetric encryption (AES-GCM, ChaCha20-Poly1305)
  • \n
  • Asymmetric encryption (Curve25519, P-256, P-384, P-521)
  • \n
  • Digital signatures (EdDSA, ECDSA)
  • \n
  • Generating cryptographically secure random numbers
  • \n
  • Key exchange (Elliptic Curve Diffie-Hellman)
  • \n
\n\n

The library is designed to minimize developer errors: it uses secure implementations by default, automatically manages memory, and does not allow the use of outdated or insecure algorithms (e.g., MD5 or SHA-1 for signatures).

\n\n

Installing CryptoKit

\n\n

Since CryptoKit is part of the Apple standard library, no installation via package managers (CocoaPods, Swift Package Manager, or Carthage) is required. All you need is a project with a minimum deployment target of iOS 13+ or macOS 10.15+.

\n\n

Important: If you are developing for older OS versions, CryptoKit is not available. In that case, use CommonCrypto (C-level) or third-party libraries (e.g., CryptoSwift).

\n\n

To check CryptoKit availability in code, use:

\n
import CryptoKit\n\nif #available(iOS 13.0, macOS 10.15, *) {\n    // Your CryptoKit code\n} else {\n    // Fallback to CommonCrypto\n}
\n\n

Quick Start with Code Example

\n\n

Let's look at a simple example: string hashing and symmetric encryption.

\n\n
import CryptoKit\nimport Foundation\n\n// 1. Hashing\nlet inputString = "Hello, CryptoKit!"\nlet inputData = Data(inputString.utf8)\nlet hash = SHA256.hash(data: inputData)\nprint("SHA256 hash: \\(hash.compactMap { String(format: "%02x", $0) }.joined())")\n\n// 2. Symmetric encryption (AES-GCM)\nlet key = SymmetricKey(size: .bits256) // Generate a 256-bit key\nlet plaintext = "Secret message".data(using: .utf8)!\n\ndo {\n    let sealedBox = try AES.GCM.seal(plaintext, using: key)\n    print("Encrypted data: \\(sealedBox.ciphertext.base64EncodedString())")\n    \n    // Decryption\n    let openedBox = try AES.GCM.open(sealedBox, using: key)\n    let decryptedString = String(data: openedBox, encoding: .utf8)!\n    print("Decrypted message: \\(decryptedString)")\n} catch {\n    print("Encryption error: \\(error)")\n}
\n\n

Main CryptoKit Methods and Functions

\n\n

Below are 10 key CryptoKit functions with detailed descriptions, parameters, and examples.

\n\n

1. SHA256.hash(data:)

\n

Description: Computes a SHA-256 hash of the provided data. Returns a SHA256Digest object.

\n

Parameters:

\n
    \n
  • data: Data — the source data to hash.
  • \n
\n

Example:

\n
let data = "Sample data".data(using: .utf8)!\nlet digest = SHA256.hash(data: data)\nprint(digest.description) // Output: SHA256 digest: 8f4343... (64 characters)
\n\n

2. SHA384.hash(data:)

\n

Description: Similar to SHA256, but uses a 384-bit digest. More secure but slower.

\n

Parameters: same a

Recommendations