From c400615b93bb96e2139d1e3ee2573bbf6a8c6205 Mon Sep 17 00:00:00 2001 From: agisboye Date: Sun, 10 Jan 2016 19:46:00 +0100 Subject: [PATCH 1/2] SQLiteCipher NSData key support --- Source/Cipher/Cipher.swift | 12 ++++++++++++ Tests/CipherTests.swift | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/Source/Cipher/Cipher.swift b/Source/Cipher/Cipher.swift index 8cf8396c..2596f239 100644 --- a/Source/Cipher/Cipher.swift +++ b/Source/Cipher/Cipher.swift @@ -36,4 +36,16 @@ extension Connection { try check(sqlite3_rekey(handle, key, Int32(key.utf8.count))) } + public func key(key: Blob) throws { + try check(sqlite3_key(handle, key.bytes, Int32(key.bytes.count))) + try execute( + "CREATE TABLE \"__SQLCipher.swift__\" (\"cipher key check\");\n" + + "DROP TABLE \"__SQLCipher.swift__\";" + ) + } + + public func rekey(key: Blob) throws { + try check(sqlite3_rekey(handle, key.bytes, Int32(key.bytes.count))) + } + } diff --git a/Tests/CipherTests.swift b/Tests/CipherTests.swift index f9919999..e2977678 100644 --- a/Tests/CipherTests.swift +++ b/Tests/CipherTests.swift @@ -4,12 +4,22 @@ import SQLiteCipher class CipherTests: XCTestCase { let db = try! Connection() + let db2 = try! Connection() override func setUp() { + // db try! db.key("hello") try! db.run("CREATE TABLE foo (bar TEXT)") try! db.run("INSERT INTO foo (bar) VALUES ('world')") + + // db2 + let keyData = NSMutableData(length: 64)! + let _ = SecRandomCopyBytes(kSecRandomDefault, 64, UnsafeMutablePointer(keyData.mutableBytes)) + try! db2.key("hello") + + try! db2.run("CREATE TABLE foo (bar TEXT)") + try! db2.run("INSERT INTO foo (bar) VALUES ('world')") super.setUp() } @@ -22,6 +32,18 @@ class CipherTests: XCTestCase { try! db.rekey("goodbye") XCTAssertEqual(1, db.scalar("SELECT count(*) FROM foo") as? Int64) } + + func test_data_key() { + XCTAssertEqual(1, db2.scalar("SELECT count(*) FROM foo") as? Int64) + } + + func test_data_rekey() { + let keyData = NSMutableData(length: 64)! + SecRandomCopyBytes(kSecRandomDefault, 64, UnsafeMutablePointer(keyData.mutableBytes)) + + try! db2.rekey(Blob(bytes: keyData.bytes, length: keyData.length)) + XCTAssertEqual(1, db2.scalar("SELECT count(*) FROM foo") as? Int64) + } func test_keyFailure() { let path = "\(NSTemporaryDirectory())/db.sqlite3" From 21b3bf5a1006ed65a89809caee428858c18b0d23 Mon Sep 17 00:00:00 2001 From: agisboye Date: Sun, 10 Jan 2016 20:53:05 +0100 Subject: [PATCH 2/2] Fix typo --- Tests/CipherTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/CipherTests.swift b/Tests/CipherTests.swift index e2977678..2c4dde0e 100644 --- a/Tests/CipherTests.swift +++ b/Tests/CipherTests.swift @@ -16,7 +16,7 @@ class CipherTests: XCTestCase { // db2 let keyData = NSMutableData(length: 64)! let _ = SecRandomCopyBytes(kSecRandomDefault, 64, UnsafeMutablePointer(keyData.mutableBytes)) - try! db2.key("hello") + try! db2.key(Blob(bytes: keyData.bytes, length: keyData.length)) try! db2.run("CREATE TABLE foo (bar TEXT)") try! db2.run("INSERT INTO foo (bar) VALUES ('world')")