From df09d0c8bbcf415bde54e379df5faf5333752b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 10 Sep 2016 16:03:42 +0200 Subject: [PATCH] Swift 3 and SQLCipher --- CHANGELOG.md | 9 +++++++++ GRDB/Core/Database.swift | 14 +++++++++----- GRDB/Core/DatabasePool.swift | 6 +++--- GRDB/Core/DatabaseQueue.swift | 4 ++-- README.md | 2 +- Tests/Public/Encryption/EncryptionTests.swift | 8 ++++---- 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 291cee8d4..6c5a9a15e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 { diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 34d33240c..a56a0d95e 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -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))) } diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 779aeba08..e335ff52c 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -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 fileprivate var functions = Set() @@ -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 } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index a06e94e2a..a0e3daa39 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -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) } } } diff --git a/README.md b/README.md index 941e2a9dd..927b112f0 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/Tests/Public/Encryption/EncryptionTests.swift b/Tests/Public/Encryption/EncryptionTests.swift index 9dfcdbb9f..ae3e3bbe3 100644 --- a/Tests/Public/Encryption/EncryptionTests.swift +++ b/Tests/Public/Encryption/EncryptionTests.swift @@ -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")