CryptoKit Question

Hi all,

In my app I have an option for users to create a PDF of their data. I'd like them to be able to save the pdf to a flashdrive or hard disk, but in encrypted form. Is it possibe with CryptoKit? Messing about in playground I've been trying to encrypt a plain string, but I get the error message that String doesn't conform to DataProtocol. How do I need to package the string in order to successfully pass it into the function?


let key = SymmetricKey(size: .bits256)


let contents = try String(contentsOfFile: filePath.path)


let data = contents


let encryptedData = data


let sealedBox = try? ChaChaPoly.seal(encryptedData, using: key)

Replies

I managed to find an example of how to pass in a string.

let data = "Hello world." .data(using: .utf8)!

I managed to find an example of how to pass in a string.

That works. Another option is:

let d = Data("Hello Cruel World!".utf8)

which avoids the force unwrap.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for that.