Migrating SQLCipher ver. 2.x DB to ver. 3.x using by FMDB

南楼画角 提交于 2019-12-25 04:45:21

问题


I saw the following page and I understand I should execute the query "PRAGMA cipher_migrate".

https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_migrate

But I have no idea how to use it on FMDatabase. The following code did not work... (Seems like not only the timing of the execution is wrong...)

I'd like you to let me know your workaround if you have tried migrating SQLCipher ver.2.x DB to ver.3.x using by FMDatabase.

- (FMDatabase *)openDB {

    NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *directory = [directories objectAtIndex:0];
    NSString *path = [directory stringByAppendingPathComponent:dbFileName];
    FMDatabase *dataBase = [FMDatabase databaseWithPath:path];

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    int version = [userDefault integerForKey:DATABASE_TABLE_VERSION];

    if(version==1){

        if(![dataBase openWithFlags:SQLITE_OPEN_READWRITE]){
            @throw [NSException exceptionWithName:@"DBException"
                                           reason:@"openWithFlags"
                                         userInfo:nil];
        }

        if(![dataBase setKey:SQLCIPHER_KEY]){
            @throw [NSException exceptionWithName:@"DBException"
                                           reason:@"setKey"
                                         userInfo:nil];
        }

        if(![dataBase executeStatements:@"PRAGMA kdf_iter = 4000"]){
            @throw [NSException exceptionWithName:@"DBException"
                                           reason:@"executeStatements:kdf_iter"
                                         userInfo:nil];
        }

        [userDefault setInteger:2 forKey:DATABASE_TABLE_VERSION];
        [userDefault synchronize];

        return dataBase;
    }

    if(![dataBase open]){
        @throw [NSException exceptionWithName:@"DBException"
                                       reason:@"open"
                                     userInfo:nil];
    }

    if(![dataBase setKey:SQLCIPHER_KEY]){
        @throw [NSException exceptionWithName:@"DBException"
                                       reason:@"setKey"
                                     userInfo:nil];
    }

    return dataBase;
}

- (NSMutableArray *)selectUser{

    FMDatabase *dataBase = [self openDB];
    NSString *sql = @"select * from t_user";
    FMResultSet *resultSet = [dataBase executeQuery:sql];

    NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects: nil];

    while ([resultSet next]){
        [mutableArray addObject:[resultSet resultDictionary]];
    }

    [resultSet close];
    [dataBase close];
    return mutableArray;
}

回答1:


I think you need to set cipher key right after connection and before any execution.

Also if you use

PRAGMA kdf_iter = 4000

you don't need to use

PRAGMA cipher_migrate


来源:https://stackoverflow.com/questions/27036546/migrating-sqlcipher-ver-2-x-db-to-ver-3-x-using-by-fmdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!