我在使用EncryptedStore SQLCipher 包装器来加密核心数据时遇到问题。
我为此添加了 C 标志:
Debug = -DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2 -DSQLITE_THREADSAFE -DSQLCIPHER_CRYPTO_CC
Release = -DSQLITE_HAS_CODEC -DNDEBUG -DSQLITE_OS_UNIX=1 -DSQLITE_TEMP_STORE=2 -DSQLITE_THREADSAFE -DSQLCIPHER_CRYPTO_CC
并将其用作:
func encryptedCoordinator() -> NSPersistentStoreCoordinator {
var coordinator:NSPersistentStoreCoordinator?
let ops:[String : Any] = [NSMigratePersistentStoresAutomaticallyOption:(true), NSInferMappingModelAutomaticallyOption:(true), EncryptedStorePassphraseKey:sqlCipherKey, EncryptedStoreDatabaseLocation:self.sqliteFileURL()]
do {
coordinator = try EncryptedStore.make(options: ops, managedObjectModel: self.managedObjectModel, error: ())
}catch {
fatalError("Error opening encrypted DB: \(error)")
}
return coordinator!
}
它在 XCode8 中运行良好,但在 XCode9-beta 中出现错误。
错误行:
- (BOOL)changeDatabasePassphrase:(NSString *)passphrase error:(NSError *__autoreleasing*)error {
BOOL result;
int status;
if ([passphrase length] > 0) {
// Password provided, use it to key the DB
const char *string = [passphrase UTF8String];
status = sqlite3_rekey(database, string, (int)strlen(string));//ERROR line
string = NULL;
passphrase = nil;
} else {
// No password
status = SQLITE_OK;
}
result = status == SQLITE_OK;
if (result) {
result = [self checkDatabaseStatusWithError:error];
}
return result && (*error == nil);
}
函数声明EncryptedStroe/sqlite3.h
为:
SQLITE_API int sqlite3_rekey(
sqlite3 *db, /* Database to be rekeyed */
const void *pKey, int nKey /* The new key */
);
SQLITE_API int sqlite3_rekey_v2(
sqlite3 *db, /* Database to be rekeyed */
const char *zDbName, /* Name of the database */
const void *pKey, int nKey /* The new key */
);