net.sqlcipher.database.SQLiteStatement Java Examples

The following examples show how to use net.sqlcipher.database.SQLiteStatement. 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: SmsDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
SQLiteStatement createInsertStatement(SQLiteDatabase database) {
  return database.compileStatement("INSERT INTO " + TABLE_NAME + " (" + RECIPIENT_ID + ", " +
                                                                        PERSON + ", " +
                                                                        DATE_SENT + ", " +
                                                                        DATE_RECEIVED  + ", " +
                                                                        PROTOCOL + ", " +
                                                                        READ + ", " +
                                                                        STATUS + ", " +
                                                                        TYPE + ", " +
                                                                        REPLY_PATH_PRESENT + ", " +
                                                                        SUBJECT + ", " +
                                                                        BODY + ", " +
                                                                        SERVICE_CENTER +
                                                                        ", " + THREAD_ID + ") " +
                                   " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
}
 
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: SqlStorage.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
protected SqlStorageIterator<T> getIndexSpanningIteratorOrNull(SQLiteDatabase db, boolean includeData) {
    //If we're just iterating over ID's, we may want to use a different, much
    //faster method depending on our stats. This method retrieves the
    //index records that _don't_ exist so we can assume the spans that
    //do.
    if (!includeData && STORAGE_OPTIMIZATIONS_ACTIVE) {

        SQLiteStatement min = db.compileStatement("SELECT MIN(" + DatabaseHelper.ID_COL + ") from " + table);

        SQLiteStatement max = db.compileStatement("SELECT MAX(" + DatabaseHelper.ID_COL + ") from " + table);

        SQLiteStatement count = db.compileStatement("SELECT COUNT(" + DatabaseHelper.ID_COL + ") from " + table);

        int minValue = (int)min.simpleQueryForLong();
        int maxValue = (int)max.simpleQueryForLong() + 1;
        int countValue = (int)count.simpleQueryForLong();

        min.close();
        max.close();
        count.close();

        double density = countValue / (maxValue - minValue * 1.0);

        //Ok, so basic metrics:
        //1) Only use a covering iterator if the number of records is > 1k
        //2) Only use a covering iterator if the number of records is less than 100k (vital, hard limit)
        //3) Only use a covering iterator if the record density is 50% or more
        if (countValue > 1000 &&
                countValue < 100000 &&
                density >= 0.5) {
            return getCoveringIndexIterator(db, minValue, maxValue, countValue);
        }
    }
    return null;
}
 
Example #7
Source File: Statement.java    From cwac-saferoom with Apache License 2.0 4 votes vote down vote up
Statement(SQLiteStatement safeStatement) {
  super(safeStatement);
  this.safeStatement=safeStatement;
}
 
Example #8
Source File: SQLCipherStmt.java    From alchemy with Apache License 2.0 4 votes vote down vote up
SQLCipherStmt(SQLiteStatement stmt) {
    mStmt = stmt;
}
 
Example #9
Source File: Statement.java    From kripton with Apache License 2.0 4 votes vote down vote up
Statement(SQLiteStatement safeStatement) {
  super(safeStatement);
  this.safeStatement=safeStatement;
}
 
Example #10
Source File: EncryptedStatementWrapper.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
EncryptedStatementWrapper(SQLiteStatement s) {
    this.s = s;
}
 
Example #11
Source File: SQLiteDatabaseNative.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
@Implementation
public SQLiteStatement compileStatement(String sql) throws SQLException {
    throw new UnsupportedOperationException("Can't use compiled statements on mock db");
}