@@ -6,6 +6,8 @@ import SQLCipher
66extension Connection {
77
88 /// - Returns: the SQLCipher version
9+ ///
10+ /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_version
911 public var cipherVersion : String ? {
1012 ( try ? scalar ( " PRAGMA cipher_version " ) ) as? String
1113 }
@@ -23,6 +25,8 @@ extension Connection {
2325 /// of key data.
2426 /// e.g. x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'
2527 /// @param db name of the database, defaults to 'main'
28+ ///
29+ /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlite3_key
2630 public func key( _ key: String , db: String = " main " ) throws {
2731 try _key_v2 ( db: db, keyPointer: key, keySize: key. utf8. count)
2832 }
@@ -39,6 +43,7 @@ extension Connection {
3943 /// As "PRAGMA cipher_migrate;" is time-consuming, it is recommended to use this function
4044 /// only after failure of `key(_ key: String, db: String = "main")`, if older versions of
4145 /// your app may ise older version of SQLCipher
46+ ///
4247 /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_migrate
4348 /// and https://discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283
4449 /// for more details regarding SQLCipher upgrade
@@ -51,11 +56,13 @@ extension Connection {
5156 try _key_v2 ( db: db, keyPointer: key. bytes, keySize: key. bytes. count, migrate: true )
5257 }
5358
54- /// Change the key on an open database. If the current database is not encrypted, this routine
55- /// will encrypt it.
59+ /// Change the key on an open database. NB: only works if the database is already encrypted.
60+ ///
5661 /// To change the key on an existing encrypted database, it must first be unlocked with the
5762 /// current encryption key. Once the database is readable and writeable, rekey can be used
5863 /// to re-encrypt every page in the database with a new key.
64+ ///
65+ /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlite3_rekey
5966 public func rekey( _ key: String , db: String = " main " ) throws {
6067 try _rekey_v2 ( db: db, keyPointer: key, keySize: key. utf8. count)
6168 }
@@ -64,6 +71,17 @@ extension Connection {
6471 try _rekey_v2 ( db: db, keyPointer: key. bytes, keySize: key. bytes. count)
6572 }
6673
74+ /// Converts a non-encrypted database to an encrypted one.
75+ ///
76+ /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlcipher_export
77+ public func sqlcipher_export( _ location: Location , key: String ) throws {
78+ let schemaName = " cipher_export "
79+
80+ try attach ( location, as: schemaName, key: key)
81+ try run ( " SELECT sqlcipher_export(?) " , schemaName)
82+ try detach ( schemaName)
83+ }
84+
6785 // MARK: - private
6886 private func _key_v2( db: String ,
6987 keyPointer: UnsafePointer < UInt8 > ,
0 commit comments