Skip to content

Instantly share code, notes, and snippets.

@billymeltdown
Created August 25, 2017 16:18
Show Gist options
  • Save billymeltdown/4ade52290eb2d18f5c1a71529bae9f70 to your computer and use it in GitHub Desktop.
Save billymeltdown/4ade52290eb2d18f5c1a71529bae9f70 to your computer and use it in GitHub Desktop.
Using sqlite3_key to open a SQLCipher db in Objective-C
#import <sqlite3.h>
NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent: @"sqlcipher.db"];
sqlite3 *db;
bool sqlcipher_valid = NO;
if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
const char* key = [@"BIGSecret" UTF8String];
sqlite3_key(db, key, strlen(key));
if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
if(sqlite3_prepare_v2(database, "PRAGMA cipher_version;", -1, &stmt, NULL) == SQLITE_OK) {
if(sqlite3_step(stmt)== SQLITE_ROW) {
const unsigned char *ver = sqlite3_column_text(stmt, 0);
if(ver != NULL) {
sqlcipher_valid = YES;
// password is correct (or database initialize), and verified to be using sqlcipher
}
}
sqlite3_finalize(stmt);
}
}
sqlite3_close(db);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment