mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Added configuration options to GRDBcipher for kdf_iterations
This commit is contained in:
1 parent
090eea9923
commit
d4eb7ba27b
3 files changed
+50
No files matched your search
@@ -93,6 +93,8 @@ public struct Configuration {
|
||||
}
|
||||
|
||||
public var cipherPageSize: CipherPageSize = .pageSize1K
|
||||
|
||||
public var cipherKDFIterations: Int = 64000
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -174,6 +174,7 @@ public final class Database {
|
||||
if let passphrase = configuration.passphrase {
|
||||
try Database.set(passphrase: passphrase, forConnection: sqliteConnection)
|
||||
try Database.set(cipherPageSize: configuration.cipherPageSize, forConnection: sqliteConnection)
|
||||
try Database.set(kdfIterations: configuration.cipherKDFIterations, forConnection: sqliteConnection)
|
||||
}
|
||||
#endif
|
||||
try Database.validateDatabaseFormat(sqliteConnection)
|
||||
@@ -277,6 +278,21 @@ extension Database {
|
||||
throw DatabaseError(resultCode: .SQLITE_MISUSE, message: "Unable to set cipher_page_size")
|
||||
}
|
||||
}
|
||||
|
||||
private static func set(kdfIterations: Int, forConnection sqliteConnection: SQLiteConnection) throws {
|
||||
var sqliteStatement: SQLiteStatement? = nil
|
||||
let code = sqlite3_prepare_v2(sqliteConnection, "PRAGMA kdf_iter = \(kdfIterations)", -1, &sqliteStatement, nil)
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
defer {
|
||||
sqlite3_finalize(sqliteStatement)
|
||||
}
|
||||
let step = sqlite3_step(sqliteStatement)
|
||||
if step != SQLITE_DONE {
|
||||
throw DatabaseError(resultCode: .SQLITE_MISUSE, message: "Unable to set kdf_iter")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private static func validateDatabaseFormat(_ sqliteConnection: SQLiteConnection) throws {
|
||||
|
||||
@@ -360,6 +360,38 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func testCipherKDFSettings() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
dbConfiguration.cipherKDFIterations = 128000
|
||||
|
||||
var dbQueue: DatabaseQueue? = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
try dbQueue!.inDatabase { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, "PRAGMA kdf_iter"), 128000)
|
||||
}
|
||||
|
||||
dbQueue = nil
|
||||
print("Closed")
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.cipherKDFIterations = 128000
|
||||
|
||||
var dbQueue: DatabasePool? = try makeDatabasePool(filename: "testpool.sqlite")
|
||||
try dbQueue!.write { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, "PRAGMA kdf_iter"), 128000)
|
||||
try db.execute("CREATE TABLE data(value INTEGER)")
|
||||
try db.execute("INSERT INTO data(value) VALUES(1)")
|
||||
}
|
||||
|
||||
try dbQueue!.read { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, "PRAGMA kdf_iter"), 128000)
|
||||
XCTAssertEqual(try Int.fetchOne(db, "SELECT value FROM data"), 1)
|
||||
}
|
||||
|
||||
dbQueue = nil
|
||||
print("Closed")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user