Skip to content

SQLiteCipher NSData key support #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Source/Cipher/Cipher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

}
22 changes: 22 additions & 0 deletions Tests/CipherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<UInt8>(keyData.mutableBytes))
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')")

super.setUp()
}
Expand All @@ -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<UInt8>(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"
Expand Down