Java Code Examples for net.sqlcipher.database.SQLiteDatabase#setVersion()

The following examples show how to use net.sqlcipher.database.SQLiteDatabase#setVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FullBackupImporter.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static void processVersion(@NonNull SQLiteDatabase db, DatabaseVersion version) throws IOException {
  if (version.getVersion() > db.getVersion()) {
    throw new DatabaseDowngradeException(db.getVersion(), version.getVersion());
  }

  db.setVersion(version.getVersion());
}
 
Example 2
Source File: SQLCipherUtils.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces this database with a version encrypted with the supplied
 * passphrase, deleting the original. Do not call this while the database
 * is open, which includes during any Room migrations.
 *
 * The passphrase is untouched in this call. If you are going to turn around
 * and use it with SafeHelperFactory.fromUser(), fromUser() will clear the
 * passphrase. If not, please set all bytes of the passphrase to 0 or something
 * to clear out the passphrase.
 *
 * @param ctxt a Context
 * @param originalFile a File pointing to the database
 * @param passphrase the passphrase from the user
 * @throws IOException
 */
public static void encrypt(Context ctxt, File originalFile, byte[] passphrase)
  throws IOException {
  SQLiteDatabase.loadLibs(ctxt);

  if (originalFile.exists()) {
    File newFile=File.createTempFile("sqlcipherutils", "tmp",
        ctxt.getCacheDir());
    SQLiteDatabase db=
      SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(),
        "", null, SQLiteDatabase.OPEN_READWRITE);
    int version=db.getVersion();

    db.close();

    db=SQLiteDatabase.openDatabase(newFile.getAbsolutePath(), passphrase,
      null, SQLiteDatabase.OPEN_READWRITE, null, null);

    final SQLiteStatement st=db.compileStatement("ATTACH DATABASE ? AS plaintext KEY ''");

    st.bindString(1, originalFile.getAbsolutePath());
    st.execute();

    db.rawExecSQL("SELECT sqlcipher_export('main', 'plaintext')");
    db.rawExecSQL("DETACH DATABASE plaintext");
    db.setVersion(version);
    st.close();
    db.close();

    originalFile.delete();
    newFile.renameTo(originalFile);
  }
  else {
    throw new FileNotFoundException(originalFile.getAbsolutePath()+" not found");
  }
}
 
Example 3
Source File: SQLCipherUtils.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces this database with a decrypted version, deleting the original
 * encrypted database. Do not call this while the database is open, which
 * includes during any Room migrations.
 *
 * The passphrase is untouched in this call. Please set all bytes of the
 * passphrase to 0 or something to clear out the passphrase if you are done
 * with it.
 *
 * @param ctxt a Context
 * @param originalFile a File pointing to the encrypted database
 * @param passphrase the passphrase from the user for the encrypted database
 * @throws IOException
 */
public static void decrypt(Context ctxt, File originalFile, byte[] passphrase)
  throws IOException {
  SQLiteDatabase.loadLibs(ctxt);

  if (originalFile.exists()) {
    File newFile=
      File.createTempFile("sqlcipherutils", "tmp",
        ctxt.getCacheDir());
    SQLiteDatabase db=
      SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(),
        passphrase, null, SQLiteDatabase.OPEN_READWRITE, null, null);

    final SQLiteStatement st=db.compileStatement("ATTACH DATABASE ? AS plaintext KEY ''");

    st.bindString(1, newFile.getAbsolutePath());
    st.execute();

    db.rawExecSQL("SELECT sqlcipher_export('plaintext')");
    db.rawExecSQL("DETACH DATABASE plaintext");

    int version=db.getVersion();

    st.close();
    db.close();

    db=SQLiteDatabase.openDatabase(newFile.getAbsolutePath(), "",
      null, SQLiteDatabase.OPEN_READWRITE);
    db.setVersion(version);
    db.close();

    originalFile.delete();
    newFile.renameTo(originalFile);
  }
  else {
    throw new FileNotFoundException(originalFile.getAbsolutePath()+" not found");
  }
}
 
Example 4
Source File: SQLCipherUtils.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces this database with a version encrypted with the supplied
 * passphrase, deleting the original. Do not call this while the database
 * is open, which includes during any Room migrations.
 *
 * The passphrase is untouched in this call. If you are going to turn around
 * and use it with SafeHelperFactory.fromUser(), fromUser() will clear the
 * passphrase. If not, please set all bytes of the passphrase to 0 or something
 * to clear out the passphrase.
 *
 * @param ctxt a Context
 * @param originalFile a File pointing to the database
 * @param passphrase the passphrase from the user
 * @throws IOException
 */
public static void encrypt(Context ctxt, File originalFile, byte[] passphrase)
  throws IOException {
  SQLiteDatabase.loadLibs(ctxt);

  if (originalFile.exists()) {
    File newFile=File.createTempFile("sqlcipherutils", "tmp",
        ctxt.getCacheDir());
    SQLiteDatabase db=
      SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(),
        "", null, SQLiteDatabase.OPEN_READWRITE);
    int version=db.getVersion();

    db.close();

    db=SQLiteDatabase.openDatabase(newFile.getAbsolutePath(), passphrase,
      null, SQLiteDatabase.OPEN_READWRITE, null, null);

    final SQLiteStatement st=db.compileStatement("ATTACH DATABASE ? AS plaintext KEY ''");

    st.bindString(1, originalFile.getAbsolutePath());
    st.execute();

    db.rawExecSQL("SELECT sqlcipher_export('main', 'plaintext')");
    db.rawExecSQL("DETACH DATABASE plaintext");
    db.setVersion(version);
    st.close();
    db.close();

    originalFile.delete();
    newFile.renameTo(originalFile);
  }
  else {
    throw new FileNotFoundException(originalFile.getAbsolutePath()+" not found");
  }
}
 
Example 5
Source File: SQLCipherUtils.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces this database with a decrypted version, deleting the original
 * encrypted database. Do not call this while the database is open, which
 * includes during any Room migrations.
 *
 * The passphrase is untouched in this call. Please set all bytes of the
 * passphrase to 0 or something to clear out the passphrase if you are done
 * with it.
 *
 * @param ctxt a Context
 * @param originalFile a File pointing to the encrypted database
 * @param passphrase the passphrase from the user for the encrypted database
 * @throws IOException
 */
public static void decrypt(Context ctxt, File originalFile, byte[] passphrase)
  throws IOException {
  SQLiteDatabase.loadLibs(ctxt);

  if (originalFile.exists()) {
    File newFile=
      File.createTempFile("sqlcipherutils", "tmp",
        ctxt.getCacheDir());
    SQLiteDatabase db=
      SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(),
        passphrase, null, SQLiteDatabase.OPEN_READWRITE, null, null);

    final SQLiteStatement st=db.compileStatement("ATTACH DATABASE ? AS plaintext KEY ''");

    st.bindString(1, newFile.getAbsolutePath());
    st.execute();

    db.rawExecSQL("SELECT sqlcipher_export('plaintext')");
    db.rawExecSQL("DETACH DATABASE plaintext");

    int version=db.getVersion();

    st.close();
    db.close();

    db=SQLiteDatabase.openDatabase(newFile.getAbsolutePath(), "",
      null, SQLiteDatabase.OPEN_READWRITE);
    db.setVersion(version);
    db.close();

    originalFile.delete();
    newFile.renameTo(originalFile);
  }
  else {
    throw new FileNotFoundException(originalFile.getAbsolutePath()+" not found");
  }
}
 
Example 6
Source File: DatabaseGlobalOpenHelper.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase database) {
    database.beginTransaction();
    try {
        TableBuilder builder = new TableBuilder(ApplicationRecord.class);
        database.execSQL(builder.getTableCreateString());
        
        builder = new TableBuilder(AndroidSharedKeyRecord.class);
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(AndroidLogEntry.STORAGE_KEY);
        builder.addData(new AndroidLogEntry());
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(ForceCloseLogEntry.STORAGE_KEY);
        builder.addData(new ForceCloseLogEntry());
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(AppAvailableToInstall.STORAGE_KEY);
        builder.addData(new AppAvailableToInstall());
        database.execSQL(builder.getTableCreateString());

        DbUtil.createNumbersTable(database);

        database.setVersion(GLOBAL_DB_VERSION);

        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}
 
Example 7
Source File: SQLCipherOpenHelper.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public void markCurrent(SQLiteDatabase db) {
  db.setVersion(DATABASE_VERSION);
}
 
Example 8
Source File: DatabaseUserOpenHelper.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase database) {
    database.beginTransaction();
    try {
        TableBuilder builder = new TableBuilder(ACase.STORAGE_KEY);
        builder.addData(new ACase());
        builder.setUnique(ACase.INDEX_CASE_ID);
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder("USER");
        builder.addData(new User());
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(FormRecord.class);
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(SessionStateDescriptor.class);
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(DeviceReportRecord.class);
        database.execSQL(builder.getTableCreateString());

        // add table for dedicated xpath error logging for reporting xpath
        // errors on specific cc app builds.
        builder = new TableBuilder(XPathErrorEntry.STORAGE_KEY);
        builder.addData(new XPathErrorEntry());
        database.execSQL(builder.getTableCreateString());

        // Add tables for storing normal device logs and force close logs in user storage
        // (as opposed to global storage) whenever possible
        builder = new TableBuilder(AndroidLogEntry.STORAGE_KEY);
        builder.addData(new AndroidLogEntry());
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(ForceCloseLogEntry.STORAGE_KEY);
        builder.addData(new ForceCloseLogEntry());
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder("fixture");
        builder.addFileBackedData(new FormInstance());
        database.execSQL(builder.getTableCreateString());

        DbUtil.createOrphanedFileTable(database);

        IndexedFixturePathUtils.createStorageBackedFixtureIndexTable(database);

        builder = new TableBuilder(Ledger.STORAGE_KEY);
        builder.addData(new Ledger());
        builder.setUnique(Ledger.INDEX_ENTITY_ID);
        database.execSQL(builder.getTableCreateString());

        //The uniqueness index should be doing this for us
        database.execSQL(DatabaseIndexingUtils.indexOnTableCommand(
                "case_id_index", "AndroidCase", TableBuilder.scrubName(Case.INDEX_CASE_ID)));
        database.execSQL(DatabaseIndexingUtils.indexOnTableCommand(
                "case_type_index", "AndroidCase", TableBuilder.scrubName(Case.INDEX_CASE_TYPE)));
        database.execSQL(DatabaseIndexingUtils.indexOnTableCommand(
                "case_status_index", "AndroidCase", TableBuilder.scrubName(Case.INDEX_CASE_STATUS)));
        database.execSQL(DatabaseIndexingUtils.indexOnTableCommand(
                "case_owner_id_index", "AndroidCase", TableBuilder.scrubName(Case.INDEX_OWNER_ID)));
        database.execSQL(DatabaseIndexingUtils.indexOnTableCommand(
                "case_external_id_index", "AndroidCase", TableBuilder.scrubName(Case.INDEX_EXTERNAL_ID)));

        database.execSQL(DatabaseIndexingUtils.indexOnTableCommand(
                "case_status_open_index", "AndroidCase", "case_type,case_status"));

        database.execSQL(DatabaseIndexingUtils.indexOnTableCommand(
                "ledger_entity_id", "ledger", "entity_id"));

        DbUtil.createNumbersTable(database);

        database.execSQL(EntityStorageCache.getTableDefinition());
        EntityStorageCache.createIndexes(database);

        database.execSQL(AndroidCaseIndexTable.getTableDefinition());
        AndroidCaseIndexTable.createIndexes(database);

        database.setVersion(USER_DB_VERSION);

        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}