mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Swift 3 and SQLCipher
This commit is contained in:
1 parent
749aa28b9a
commit
df09d0c8bb
6 files changed
+28
-15
No files matched your search
@@ -50,6 +50,10 @@ Release Notes
|
||||
#if os(iOS)
|
||||
- func setupMemoryManagement(application application: UIApplication)
|
||||
+ func setupMemoryManagement(in application: UIApplication)
|
||||
#endif
|
||||
#if SQLITE_HAS_CODEC
|
||||
- func changePassphrase(passphrase: String) throws
|
||||
+ func change(passphrase: String) throws
|
||||
#endif
|
||||
}
|
||||
class DatabaseQueue {
|
||||
@@ -57,6 +61,7 @@ Release Notes
|
||||
- func addCollation(collation: DatabaseCollation)
|
||||
- func removeFunction(function: DatabaseFunction)
|
||||
- func removeCollation(collation: DatabaseCollation)
|
||||
|
||||
+ func add(function: DatabaseFunction)
|
||||
+ func add(collation: DatabaseCollation)
|
||||
+ func remove(function: DatabaseFunction)
|
||||
@@ -64,6 +69,10 @@ Release Notes
|
||||
#if os(iOS)
|
||||
- func setupMemoryManagement(application application: UIApplication)
|
||||
+ func setupMemoryManagement(in application: UIApplication)
|
||||
#endif
|
||||
#if SQLITE_HAS_CODEC
|
||||
- func changePassphrase(passphrase: String) throws
|
||||
+ func change(passphrase: String) throws
|
||||
#endif
|
||||
}
|
||||
protocol DatabaseReader {
|
||||
|
||||
@@ -122,7 +122,7 @@ public final class Database {
|
||||
do {
|
||||
#if SQLITE_HAS_CODEC
|
||||
if let passphrase = configuration.passphrase {
|
||||
try Database.setPassphrase(passphrase, forConnection: sqliteConnection)
|
||||
try Database.set(passphrase: passphrase, forConnection: sqliteConnection!)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -751,17 +751,21 @@ public func ==(lhs: DatabaseCollation, rhs: DatabaseCollation) -> Bool {
|
||||
|
||||
#if SQLITE_HAS_CODEC
|
||||
extension Database {
|
||||
private class func setPassphrase(passphrase: String, forConnection sqliteConnection: SQLiteConnection) throws {
|
||||
fileprivate class func set(passphrase: String, forConnection sqliteConnection: SQLiteConnection) throws {
|
||||
let data = passphrase.data(using: .utf8)!
|
||||
let code = sqlite3_key(sqliteConnection, data.bytes, Int32(data.length))
|
||||
let code = data.withUnsafeBytes { bytes in
|
||||
sqlite3_key(sqliteConnection, bytes, Int32(data.count))
|
||||
}
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(code: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
|
||||
func changePassphrase(passphrase: String) throws {
|
||||
func change(passphrase: String) throws {
|
||||
let data = passphrase.data(using: .utf8)!
|
||||
let code = sqlite3_rekey(sqliteConnection, data.bytes, Int32(data.length))
|
||||
let code = data.withUnsafeBytes { bytes in
|
||||
sqlite3_rekey(sqliteConnection, bytes, Int32(data.count))
|
||||
}
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(code: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ public final class DatabasePool {
|
||||
|
||||
let store: DatabaseStore // Not private for tests that require syncing with the store
|
||||
fileprivate let writer: SerializedDatabase
|
||||
private var readerConfig: Configuration
|
||||
fileprivate var readerConfig: Configuration
|
||||
fileprivate let readerPool: Pool<SerializedDatabase>
|
||||
|
||||
fileprivate var functions = Set<DatabaseFunction>()
|
||||
@@ -236,10 +236,10 @@ public enum CheckpointMode: Int32 {
|
||||
extension DatabasePool {
|
||||
|
||||
/// Changes the passphrase of an encrypted database
|
||||
public func changePassphrase(passphrase: String) throws {
|
||||
public func change(passphrase: String) throws {
|
||||
try readerPool.clear {
|
||||
try writer.sync { db in
|
||||
try db.changePassphrase(passphrase)
|
||||
try db.change(passphrase: passphrase)
|
||||
}
|
||||
readerConfig.passphrase = passphrase
|
||||
}
|
||||
|
||||
@@ -204,9 +204,9 @@ public final class DatabaseQueue {
|
||||
extension DatabaseQueue {
|
||||
|
||||
/// Changes the passphrase of an encrypted database
|
||||
public func changePassphrase(passphrase: String) throws {
|
||||
public func change(passphrase: String) throws {
|
||||
try serializedDatabase.sync { db in
|
||||
try db.changePassphrase(passphrase)
|
||||
try db.change(passphrase: passphrase)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3312,7 +3312,7 @@ let dbQueue = try DatabaseQueue(path: "...", configuration: configuration)
|
||||
**You can change the passphrase** of an already encrypted database:
|
||||
|
||||
```swift
|
||||
try dbQueue.changePassphrase("newSecret")
|
||||
try dbQueue.change(passphrase: "newSecret")
|
||||
```
|
||||
|
||||
Providing a passphrase won't encrypt a clear-text database that already exists, though. SQLCipher can't do that, and you will get an error instead: `SQLite error 26: file is encrypted or is not a database`.
|
||||
|
||||
@@ -90,7 +90,7 @@ class EncryptionTests: GRDBTestCase {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.changePassphrase("newSecret")
|
||||
try dbQueue.change(passphrase: "newSecret")
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("INSERT INTO data (value) VALUES (2)")
|
||||
}
|
||||
@@ -194,7 +194,7 @@ class EncryptionTests: GRDBTestCase {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.changePassphrase("newSecret")
|
||||
try dbPool.change(passphrase: "newSecret")
|
||||
try dbPool.write { db in
|
||||
try db.execute("INSERT INTO data (value) VALUES (2)")
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM data")!, 2)
|
||||
@@ -297,7 +297,7 @@ class EncryptionTests: GRDBTestCase {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.changePassphrase("newSecret")
|
||||
try dbPool.change(passphrase: "newSecret")
|
||||
try dbPool.write { db in
|
||||
try db.execute("INSERT INTO data (value) VALUES (2)")
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM data")!, 2)
|
||||
@@ -375,7 +375,7 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
dbConfiguration.passphrase = "secret"
|
||||
do {
|
||||
try makeDatabaseQueue("plaintext.sqlite")
|
||||
_ = try makeDatabaseQueue(filename: "plaintext.sqlite")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.code, 26) // SQLITE_NOTADB
|
||||
XCTAssertEqual(error.message!, "file is encrypted or is not a database")
|
||||
|
||||
Reference in new issue
Block a user