Skip to content

Commit a3f04cc

Browse files
committed
Merge branch 'master' into master
2 parents d76092c + 4a0fa9c commit a3f04cc

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Source/Cipher/Cipher.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,16 @@ extension Connection {
3636
try check(sqlite3_rekey(handle, key, Int32(key.utf8.count)))
3737
}
3838

39+
public func key(key: Blob) throws {
40+
try check(sqlite3_key(handle, key.bytes, Int32(key.bytes.count)))
41+
try execute(
42+
"CREATE TABLE \"__SQLCipher.swift__\" (\"cipher key check\");\n" +
43+
"DROP TABLE \"__SQLCipher.swift__\";"
44+
)
45+
}
46+
47+
public func rekey(key: Blob) throws {
48+
try check(sqlite3_rekey(handle, key.bytes, Int32(key.bytes.count)))
49+
}
50+
3951
}

Tests/CipherTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@ import SQLiteCipher
44
class CipherTests: XCTestCase {
55

66
let db = try! Connection()
7+
let db2 = try! Connection()
78

89
override func setUp() {
10+
// db
911
try! db.key("hello")
1012

1113
try! db.run("CREATE TABLE foo (bar TEXT)")
1214
try! db.run("INSERT INTO foo (bar) VALUES ('world')")
15+
16+
// db2
17+
let keyData = NSMutableData(length: 64)!
18+
let _ = SecRandomCopyBytes(kSecRandomDefault, 64, UnsafeMutablePointer<UInt8>(keyData.mutableBytes))
19+
try! db2.key(Blob(bytes: keyData.bytes, length: keyData.length))
20+
21+
try! db2.run("CREATE TABLE foo (bar TEXT)")
22+
try! db2.run("INSERT INTO foo (bar) VALUES ('world')")
1323

1424
super.setUp()
1525
}
@@ -22,6 +32,18 @@ class CipherTests: XCTestCase {
2232
try! db.rekey("goodbye")
2333
XCTAssertEqual(1, db.scalar("SELECT count(*) FROM foo") as? Int64)
2434
}
35+
36+
func test_data_key() {
37+
XCTAssertEqual(1, db2.scalar("SELECT count(*) FROM foo") as? Int64)
38+
}
39+
40+
func test_data_rekey() {
41+
let keyData = NSMutableData(length: 64)!
42+
SecRandomCopyBytes(kSecRandomDefault, 64, UnsafeMutablePointer<UInt8>(keyData.mutableBytes))
43+
44+
try! db2.rekey(Blob(bytes: keyData.bytes, length: keyData.length))
45+
XCTAssertEqual(1, db2.scalar("SELECT count(*) FROM foo") as? Int64)
46+
}
2547

2648
func test_keyFailure() {
2749
let path = "\(NSTemporaryDirectory())/db.sqlite3"

0 commit comments

Comments
 (0)