how to encrypted sql file by use corestore #120

Closed
opened 2025-12-29 15:24:54 +01:00 by adam · 1 comment
Owner

Originally created by @yaochenfeng on GitHub (Jan 18, 2017).

how to encrypted sql file by use corestore

Originally created by @yaochenfeng on GitHub (Jan 18, 2017). how to encrypted sql file by use corestore
adam closed this issue 2025-12-29 15:24:54 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jan 19, 2017):

There is no built-in way to do this right now. Are you talking about Core Data's NSPersistentStoreFileProtectionKey option?

In the meantime, you can duplicate the code for SQLiteStore and implement your own LocalStorage that adds NSPersistentStoreFileProtectionKey option to the options dictionary:

class EncryptedSQLiteStore: LocalStorage, DefaultInitializableStore {
    // ...
    public let storeOptions: [AnyHashable: Any]? = [
        NSSQLitePragmasOption: ["journal_mode": "WAL"],
        NSPersistentStoreFileProtectionKey: NSFileProtectionComplete // add this
    ]
    // ...
}

Note that this just uses iOS's Data Protection API and the file is only encrypted when the user's device is passcode-locked.

Otherwise, if you are looking for a way to fully encrypt the database you would need to look for a separate framework, wrap it in an NSIncrementalStore implementation (I found a very old library here), and then implement CoreStore's LocalStorage protocol. Needless to say, this seems like a lot of work.

@JohnEstropia commented on GitHub (Jan 19, 2017): There is no built-in way to do this right now. Are you talking about Core Data's `NSPersistentStoreFileProtectionKey` option? In the meantime, you can duplicate the code for `SQLiteStore` and implement your own `LocalStorage` that adds `NSPersistentStoreFileProtectionKey` option to the options dictionary: ```swift class EncryptedSQLiteStore: LocalStorage, DefaultInitializableStore { // ... public let storeOptions: [AnyHashable: Any]? = [ NSSQLitePragmasOption: ["journal_mode": "WAL"], NSPersistentStoreFileProtectionKey: NSFileProtectionComplete // add this ] // ... } ``` Note that this just uses iOS's Data Protection API and the file is only encrypted when the user's device is passcode-locked. Otherwise, if you are looking for a way to fully encrypt the database you would need to look for a separate framework, wrap it in an `NSIncrementalStore` implementation (I found [a very old library here](https://github.com/project-imas/encrypted-core-data/)), and then implement CoreStore's `LocalStorage` protocol. Needless to say, this seems like a lot of work.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#120