Skip to content

Instantly share code, notes, and snippets.

@benbahrenburg
Last active July 13, 2022 11:37
Show Gist options
  • Save benbahrenburg/2ac7cf7c42367e2ee06dfe9675266ff5 to your computer and use it in GitHub Desktop.
Save benbahrenburg/2ac7cf7c42367e2ee06dfe9675266ff5 to your computer and use it in GitHub Desktop.
CryptoKit ChaChaPoly Helper Functions
import UIKit
import CryptoKit
struct CryptoKitHelpers {
static func encrypt(secret: String, image: UIImage, compressionRatio: CGFloat = 0.9) throws -> Data? {
guard let encryptionKey = getKey(secret: secret) else { return nil }
guard let contentData = UIImageToDataJPEG2(image: image, compressionRatio: compressionRatio) else {
return nil
}
return try ChaChaPoly.seal(contentData, using: encryptionKey).combined
}
static func encrypt(secret: String, contentData: Data) throws -> Data? {
guard let encryptionKey = getKey(secret: secret) else { return nil }
return try ChaChaPoly.seal(contentData, using: encryptionKey).combined
}
static func encryptFromFile(secret: String, url: URL) throws -> Data? {
guard fileExists(url) else {
return nil
}
guard let encryptionKey = getKey(secret: secret) else { return nil }
let contentData = try Data.init(contentsOf: url, options: .mappedIfSafe)
return try ChaChaPoly.seal(contentData, using: encryptionKey).combined
}
static func encryptSaveToFile(secret: String, contentData: Data, url: URL) throws {
guard let encryptionKey = getKey(secret: secret) else { return }
let encryptedData = try ChaChaPoly.seal(contentData, using: encryptionKey).combined
try encryptedData.write(to: url)
}
static func decryptFromFile(secret: String, url: URL) throws -> Data? {
guard fileExists(url) else {
return nil
}
guard let encryptionKey = getKey(secret: secret) else { return nil }
let encryptedContent = try Data.init(contentsOf: url, options: .mappedIfSafe)
let sealedBox = try ChaChaPoly.SealedBox(combined: encryptedContent)
return try ChaChaPoly.open(sealedBox, using: encryptionKey)
}
static func decryptFromFile(secret: String, url: URL) throws -> UIImage? {
guard fileExists(url) else {
return nil
}
guard let encryptionKey = getKey(secret: secret) else { return nil }
let encryptedContent = try Data.init(contentsOf: url, options: .mappedIfSafe)
let sealedBox = try ChaChaPoly.SealedBox(combined: encryptedContent)
let data = try ChaChaPoly.open(sealedBox, using: encryptionKey)
return UIImage(data: data)
}
static func decrypt(secret: String, encryptedContent: Data) throws -> Data? {
guard let encryptionKey = getKey(secret: secret) else { return nil }
let sealedBox = try ChaChaPoly.SealedBox(combined: encryptedContent)
return try ChaChaPoly.open(sealedBox, using: encryptionKey)
}
static func decrypt(secret: String, encryptedContent: Data) throws -> UIImage? {
guard let encryptionKey = getKey(secret: secret) else { return nil }
let sealedBox = try ChaChaPoly.SealedBox(combined: encryptedContent)
let data = try ChaChaPoly.open(sealedBox, using: encryptionKey)
return UIImage(data: data)
}
private static func getKey(secret: String) -> SymmetricKey? {
guard let passwordData: Data = secret.data(using: .utf8) else {
return nil
}
return SymmetricKey(data: SHA256.hash(data: passwordData))
}
private static func fileExists(_ url: URL) -> Bool {
return FileManager.default.fileExists(atPath: url.path)
}
private static func UIImageToDataJPEG2(image: UIImage, compressionRatio: CGFloat = 0.9) -> Data? {
return autoreleasepool(invoking: { () -> Data? in
return image.jpegData(compressionQuality: compressionRatio)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment