project-imas/encrypted-core-data — pull request #105 by rhodgkins (2014-08-18 21:08:41+00:00)
Title: Added error for incorrect passcode

Split out the database configuration of passcode and cache size into two separate methods.
Then added a check in the passcode method to see if the provided passcode was correct, returning the new error code.

Also added 2 test cases for testing opening the DB with a correct and incorrect passcode.

Below is the implementation used to check for incorrect keys as per [SQLCipher API documentation](http://sqlcipher.net/sqlcipher-api/#key):
>#### Testing the Key
>
>When opening an existing database, PRAGMA key will not immediately throw an error if the key provided is incorrect. To test that the database can be successfully opened with the provided key, it is necessary to perform some operation on the database (i.e. read from it) and confirm it is success.
>
>The easiest way to do this is select off the sqlite_master table, which will attempt to read the first page of the database and will parse the schema.
>
`sqlite> PRAGMA key = 'passphrase';`
`sqlite> SELECT count(*) FROM sqlite_master; -- if this throws an error, the key was incorrect. If it succeeds and returns a numeric value, the key is correct;`
>
>The same check can be implemented in C code
```c
sqlite3_key(database, "test123", 7);
if (sqlite3_exec(database, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
// key is correct.
} else {
// key is incorrect
}
```