diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index be362e5d6..7bdf22898 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -1237,11 +1237,11 @@ extension Database { /// try db.usePassphrase("secret") /// } public func usePassphrase(_ passphrase: String) throws { - guard !passphrase.isEmpty, var data = passphrase.data(using: .utf8) else { - throw DatabaseError(message: "usePassphrase Failed: invalid or empty passphrase") + guard var data = passphrase.data(using: .utf8) else { + throw DatabaseError(message: "invalid passphrase") } defer { - data.resetBytes(in: 0.. 0) { data.resetBytes(in: 0.. 0 { data.resetBytes(in: 0.. schema of the original db into the new one: // > https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/ // swiftlint:disable:previous line_length - guard !passphrase.isEmpty else { - throw DatabaseError(message: "usePassphrase Failed: invalid or empty passphrase") - } - let code = passphrase.withUnsafeBytes { sqlite3_rekey(sqliteConnection, $0.baseAddress, Int32($0.count)) } diff --git a/README.md b/README.md index 4e09df114..2d4d46480 100644 --- a/README.md +++ b/README.md @@ -6412,7 +6412,23 @@ config.prepareDatabase { db in } ``` -This technique manages the lifetime of the passphrase string. Some demanding users will want to go further, and manage the lifetime of the raw passphrase bytes. See below. +This technique helps manages the lifetime of the passphrase, although keep in mind that the content of a String may remain intact in memory long after the object has been released. + +For even better control over the lifetime of the passphrase in memory, use a Data object which natively provides the `resetBytes` function. + +```swift +// RECOMMENDED: only load the passphrase when it is needed and reset its content immediately after use +var config = Configuration() +config.prepareDatabase { db in + let passphrase: Data = try getPassphraseData() + defer { + if (data.count > 0) { data.resetBytes(in: 0..