2

I've been having some trouble figuring out how to encrypt my sqlite database. I'm using core data and this following project:

https://cocoapods.org/?q=EncryptedCoreData

What I can't figure out is how I am suppose to use this project to encrypt my database. I've already install the project and I can import the library EncryptedCoreData. However I don't find any information regarding a pratical example with swift. In my appdelegate I have the following code

import UIKit
//import CoreData
//import SQLCipher
import EncryptedCoreData


lazy var persistentContainer: NSPersistentContainer = {
    // my attempt to initialize the container
    let modelURL = Bundle.main.url(forResource: "DbModel", withExtension: "momd")!
    var coordinator = NSPersistentStoreCoordinator.init(managedObjectModel: NSManagedObjectModel(contentsOf: modelURL)!)

    //originaly its
    let container = NSPersistentContainer(name: "DbModel")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

Can someone provide an example on how I'am suppose to initialize the container?

1 Answer 1

2

I translated the Objective-C to Swift and it worked, I just added this lines

let container = NSPersistentContainer(name: "DbModel")
// Begin of my code
let cOpts : NSDictionary = [
            EncryptedStore.optionPassphraseKey() : "123deOliveira4", //your Key
            EncryptedStore.optionFileManager() : EncryptedStoreFileManager.default()
        ]
let desc = try! EncryptedStore.makeDescription(options: cOpts as! [AnyHashable : Any], configuration: nil)
container.persistentStoreDescriptions = [desc]
//End
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
})
Sign up to request clarification or add additional context in comments.

7 Comments

I did the same. But I need to know that after adding above lines in my appdelegate file, my data encryption will start automatically? or I have to add some extra code while saving data or in saveContext() method?
For what I remember the Data file will be encrypted by doing just this, you can check it by trying to open the file created by the simulator. If it requests the passphrase key it is encrypted.
This code causes error Error Domain=Foundation._GenericObjCError Code=0 "(null) at: let DelTable = NSBatchDeleteRequest(fetchRequest: NSFetchRequest<NSFetchRequestResult>(entityName: "MyTable")) do { try managedContext.execute(DelTable) print("all data deleted from entity: MyTable ") } catch { let nserror = error as NSError print(nserror) }
Although it did lock my *.sqlite file. I tried opening it in DB browser app, it asks me for a passphrase.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.