Closed R4N closed 1 year ago
Thanks for pointing this out, I'll change the comments and clarify this in the docs. It might also be worth to expose sqlcipher_export()
in the API of SQLite.swift/SQLCipher
.
But I'm wondering, is there a reason why rekey
doesn't raise an error when called on an unencrypted db? Seems like an obvious user error to catch.
It might also be worth to expose sqlcipher_export() in the API of SQLite.swift/SQLCipher.
That makes sense to me so it provides your lib's users a streamlined way of going from plaintext <> encrypted
But I'm wondering, is there a reason why rekey doesn't raise an error when called on an unencrypted db? Seems like an obvious user error to catch
That is a fair point, rekey just "does nothing" (and returns SQLITE_OK) when called with a key and the open database is not encrypted. Attempting to key the db on subsequent opens with the same key that was provided during rekey will fail with error though. That being said, we think it might be a prudent to return an error in this scenario.
Going the other direction (attempting to rekey with an empty string when the open db is already encrypted) does fail with SQLITE_ERROR already.
I was directed over to this project from an issue raised in the SQLCipher project which was linked to from an issue within your project of similar affect.
I saw that you adjusted the documentation to explicitly call out only using the SQLCipher subspec so as not to cause a conflict with standard SQLite which was a good adjustment (even though your documentation previously correctly only referenced the subspec)
One other thing I noticed in both issue reports, which I responded to in the SQLCipher issue report, is that both users believed that rekey encrypts a plaintext database. When correctly linking SQLCipher this is absolutely not correct. You'll need to use the sqlcipher_export() convenience function.
Specifically the second sentence in the comments here is incorrect:
https://github.com/stephencelis/SQLite.swift/blob/master/Sources/SQLite/Extensions/Cipher.swift#L54-L55
In the SQLCipher specific sqlite3.h you'll notice these extra comments above the rekey functions:
Here's a example using the sample code from your project to create a plaintext database, call rekey on it, then open it without providing a key to display that its not encrypted:
Which outputs:
Notice that the openDbWithoutKey() was able to access the database without a key because it wasn't encrypted when calling rekey on a plaintext db.