Migration for iOS.

This commit is contained in:
M
2020-09-23 21:26:10 +03:00
parent 5f88c4bafd
commit b0a31147dd
18 changed files with 467 additions and 213 deletions
-50
View File
@@ -1,50 +0,0 @@
import Foundation
import CryptoSwift
class EncryptedFile {
private(set) var fileName: String
private(set) var url: URL
private let key: Array<UInt8>
private let salt: Array<UInt8>
init(url: URL, key: String, salt: String) {
self.key = key.data(using: .utf8)?.bytes ?? []
self.salt = salt.data(using: .utf8)?.bytes ?? []
self.url = url
self.fileName = url.lastPathComponent
}
func readRawContent() -> String? {
guard let binaryContent = try? Data(contentsOf: url) else {
return nil
}
return String(data: binaryContent, encoding: .utf8)
}
func decryptedContent() -> String? {
guard
let rawContent = readRawContent(),
let decryptedBytes = try? cipherBuilder().decrypt(rawContent.bytes) else {
return nil
}
let decryptedData = Data(decryptedBytes)
return String(data: decryptedData, encoding: .utf8)
}
func cipherBuilder() -> Cipher {
let PBKDF2key = try! PKCS5.PBKDF2(password: key, salt: salt, iterations: 4096, variant: .sha256).calculate()
return try! Blowfish(key: PBKDF2key, padding: .pkcs7)
}
}
func readTradesList(key: String, salt: String) -> String? {
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("trades_list.json")
return EncryptedFile(
url: url,
key: key,
salt: salt).decryptedContent()
}
+16
View File
@@ -0,0 +1,16 @@
import Foundation
import CryptoSwift
func decrypt(data: Data, key: String, salt: String) -> String? {
let keyBytes = key.data(using: .utf8)?.bytes ?? []
let saltBytes = salt.data(using: .utf8)?.bytes ?? []
guard let PBKDF2key = try? PKCS5.PBKDF2(password: keyBytes, salt: saltBytes, iterations: 4096, variant: .sha256).calculate(),
let cipher = try? Blowfish(key: PBKDF2key, padding: .pkcs7),
let decryptedBytes = try? cipher.decrypt(data.bytes) else {
return nil
}
let decryptedData = Data(decryptedBytes)
return String(data: decryptedData, encoding: .utf8)
}