NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Apple announces SwiftCrypto, an open-source implementation of CryptoKit

Get Swifty cryptography everywhere

Paul Hudson       @twostraws

Update: Cory Benfield from Apple has confirmed that on Apple's platforms SwiftCrypto is limited to those that support CryptoKit, and can't act as a polyfill for older releases such as iOS 11/iOS 12 and macOS 10.14. However, there's hope that might change – Cory added "we’d be happy to discuss that use-case with the community." In theory that could mean we get SwiftCrypto support for iOS 12 and earlier, which would be awesome!

Apple today released SwiftCrypto, an open-source implementation of the CryptoKit framework that shipped in iOS 13 and macOS Catalina, allowing us to use the same APIs for encryption and hashing on Linux.

The release of SwiftCrypto is a big step forward for server-side Swift, because although we’ve had open-source Swift cryptography libraries in the past this is the first one officially supported by Apple. Even better, Apple states that the “vast majority of the SwiftCrypto code is intended to remain in lockstep with the current version of Apple CryptoKit,” which means it’s easy for developers to share code between Apple’s own platforms and Linux.

What’s particularly awesome about SwiftCrypto is that if you use it on Apple platforms it effectively becomes transparent – it just passes your calls directly on to CryptoKit. This means you can write your code once using import Crypto, then share it everywhere. As Apple describes it, this means SwiftCrypto “delegates all work to the core implementation of CryptoKit, as though SwiftCrypto was not even there.”

The only exception here is that SwiftCrypto doesn’t provide support for using Apple’s Secure Enclave hardware, which is incorporated into devices such as iPhones, Apple Watch, and modern Macs. As the Secure Enclave is only available on Apple hardware, this omission is unlikely to prove problematic.

Subscribe to Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Try it yourself

SwiftCrypto is available today, so why not give it a try?

If you’re using Xcode for your project, go to File > Swift Packages > Add Package Dependency to get started; if not, you can just edit the Package.swift file directly. Either way, you should make it point towards then choose “Up To Next Major”.

Once Xcode has downloaded the package (or if you’ve run swift package fetch from the command line), you can write some code to try it out.

For example, this will compute the SHA256 hash value of a string:

import Crypto
let inputString = "Hello, SwiftCrypto"
let inputData = Data(inputString.utf8)
let hashed = SHA256.hash(data: inputData)

If you want to read that back as a string – for example, if you want to print the SHA so users can verify a file locally – you can create it like this:

let hashString = hashed.compactMap { String(format: "%02x", $0) }.joined()

For more information on SwiftCrypto read the official announcement or check out the project on GitHub.

Subscribe to Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

About the author

Paul Hudson is the creator of Hacking with Swift, the most comprehensive series of Swift books in the world. He's also the editor of Swift Developer News, the maintainer of the Swift Knowledge Base, and a speaker at Swift events around the world. If you're curious you can learn more here.

Was this page useful? Let us know!

Average rating: 3.4/5

Link copied to your pasteboard.