Files
logseq/ios/App/App/FileSync/Data+ChaChaPoly.swift
Charlie 01d879c18e feat!: File Sync (#5355)
- file sync for electron/ios/android
- age encryption of both file content and file path
- massive UI enhancement
- corresponding CI tasks

Co-authored-by: llcc <lzhes43@gmail.com>
Co-authored-by: rcmerci <rcmerci@gmail.com>
Co-authored-by: Tienson Qin <tiensonqin@gmail.com>
Co-authored-by: Andelf <andelf@gmail.com>
Co-authored-by: Gabriel Horner <gabriel@logseq.com>
2022-09-01 00:31:49 +08:00

46 lines
1.3 KiB
Swift

//
// Data+ChaChaPoly.swift
// Logseq
//
// Created by Mono Wang on 5/20/R4.
//
import Foundation
import CryptoKit
extension Data {
/**
Encrypts current data using ChaChaPoly cipher.
*/
public func sealChaChaPoly(with passphrase: String) -> Data? {
guard let symmetricKey = try? SymmetricKey(passwordString: passphrase) else {
return nil
}
let nonce = try? ChaChaPoly.Nonce(data: Data(repeating: 0x00, count: 12))
if let encrypted = try? ChaChaPoly.seal(self, using: symmetricKey, nonce: nonce!).combined {
var ret = Data(hexEncoded: "4c530031")
ret?.append(encrypted)
return ret
}
return nil
}
/**
Decrypts current combined ChaChaPoly Selead box data (nonce || ciphertext || tag) using ChaChaPoly cipher.
*/
public func openChaChaPoly(with passphrase: String) -> Data? {
if self.count <= 4 {
return nil
}
guard let symmetricKey = try? SymmetricKey(passwordString: passphrase) else {
return nil
}
guard let chaChaPolySealedBox = try? ChaChaPoly.SealedBox(combined: self[4...]) else {
return nil
}
return try? ChaChaPoly.open(chaChaPolySealedBox, using: symmetricKey)
}
}