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

The following examples show how to use net.sqlcipher.database.SQLiteDatabase#loadLibs() . 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: MigratingImportTest.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@Test
public void safe() {
  final Context ctxt=InstrumentationRegistry.getTargetContext();

  SQLiteDatabase.loadLibs(ctxt);
  SQLiteOpenHelper helper = new SafeNonRoomHelper(ctxt);

  helper.getWritableDatabase(PASSPHRASE);
  helper.close();

  ImportingSafeDatabase room = ImportingSafeDatabase.gimme(ctxt);
  SupportSQLiteDatabase db=room.getOpenHelper().getWritableDatabase();

  try {
    assertTrue(db.isWriteAheadLoggingEnabled());
  }
  finally {
    room.close();
  }
}
 
Example 2
Source File: DatabaseContentProvider.java    From bitseal with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Gets a writable SQLiteDatabase object
   * 
   * @return The SQLiteDatabase object
   */
  public static SQLiteDatabase openDatabase()
  {
  	Log.i(TAG, "DatabaseContentProvider.openDatabase() called");
  	
  	try
  	{
  		SQLiteDatabase.loadLibs(sContext);
  		sDatabase = sDatabaseHelper.getWritableDatabase();
  	}
  	catch (Exception e)
  	{
  		Log.e(TAG, "Exception occurred while running DatabaseContentProvider.openDatabase(). The exception message was:\n" 
  				+ e.getMessage());
  	}
  	
return sDatabase;
  }
 
Example 3
Source File: SecureApplication.java    From android-showcase-template with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    initInjector();

    try {
        SQLiteDatabase.loadLibs(this);
    } catch (UnsatisfiedLinkError e) {
        //only thrown during tests, ignore it
    }
}
 
Example 4
Source File: SecurityApp.java    From android-security with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    Hawk.init(this).build();
    SQLiteDatabase.loadLibs(this);

    int times = Hawk.get(PREF_TIMES, 0) + 1;
    Log.d(TAG, "Psss! Let me tell a secret: you opened this app " + times + " times.");
    Hawk.put(PREF_TIMES, times);
}
 
Example 5
Source File: DatabaseFactory.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private DatabaseFactory(@NonNull Context context) {
  SQLiteDatabase.loadLibs(context);

  DatabaseSecret      databaseSecret   = new DatabaseSecretProvider(context).getOrCreateDatabaseSecret();
  AttachmentSecret    attachmentSecret = AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret();

  this.databaseHelper       = new SQLCipherOpenHelper(context, databaseSecret);
  this.sms                  = new SmsDatabase(context, databaseHelper);
  this.mms                  = new MmsDatabase(context, databaseHelper);
  this.attachments          = new AttachmentDatabase(context, databaseHelper, attachmentSecret);
  this.media                = new MediaDatabase(context, databaseHelper);
  this.thread               = new ThreadDatabase(context, databaseHelper);
  this.mmsSmsDatabase       = new MmsSmsDatabase(context, databaseHelper);
  this.identityDatabase     = new IdentityDatabase(context, databaseHelper);
  this.draftDatabase        = new DraftDatabase(context, databaseHelper);
  this.pushDatabase         = new PushDatabase(context, databaseHelper);
  this.groupDatabase        = new GroupDatabase(context, databaseHelper);
  this.recipientDatabase    = new RecipientDatabase(context, databaseHelper);
  this.groupReceiptDatabase = new GroupReceiptDatabase(context, databaseHelper);
  this.contactsDatabase     = new ContactsDatabase(context);
  this.preKeyDatabase       = new OneTimePreKeyDatabase(context, databaseHelper);
  this.signedPreKeyDatabase = new SignedPreKeyDatabase(context, databaseHelper);
  this.sessionDatabase      = new SessionDatabase(context, databaseHelper);
  this.searchDatabase       = new SearchDatabase(context, databaseHelper);
  this.jobDatabase          = new JobDatabase(context, databaseHelper);
  this.stickerDatabase      = new StickerDatabase(context, databaseHelper, attachmentSecret);
  this.storageKeyDatabase   = new StorageKeyDatabase(context, databaseHelper);
  this.keyValueDatabase     = new KeyValueDatabase(context, databaseHelper);
  this.megaphoneDatabase    = new MegaphoneDatabase(context, databaseHelper);
}
 
Example 6
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 7
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 8
Source File: Helper.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
Helper(Context context, String name, Callback callback, byte[] passphrase,
       SafeHelperFactory.Options options) {
  SQLiteDatabase.loadLibs(context);
  clearPassphrase=options.clearPassphrase;
  delegate=createDelegate(context, name, callback, options);
  this.passphrase=passphrase;
}
 
Example 9
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 10
Source File: KriptonSQLCipherHelper.java    From kripton with Apache License 2.0 5 votes vote down vote up
KriptonSQLCipherHelper(Context context, String name, Callback callback, byte[] passphrase,
		KriptonSQLCipherHelperFactory.Options options) {
	SQLiteDatabase.loadLibs(context);
	clearPassphrase = options.clearPassphrase;
	delegate = createDelegate(context, name, callback, options);
	this.passphrase = passphrase;
	this.requiredPassphrase = options.requiredPassphrase;
}
 
Example 11
Source File: PerfTestSqlite.java    From android-database-performance with Apache License 2.0 5 votes vote down vote up
@Override
protected void onRunSetup() throws Exception {
    super.onRunSetup();

    // set up database
    SQLiteDatabase.loadLibs(getTargetContext());
    DbHelper dbHelper = new DbHelper(getTargetContext(), DATABASE_NAME, DATABASE_VERSION);
    database = dbHelper.getWritableDatabase(DATABASE_PASSWORD);
}
 
Example 12
Source File: CommCareApplication.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    turnOnStrictMode();

    CommCareApplication.app = this;
    CrashUtil.init();
    DataChangeLogger.init(this);

    logFirstCommCareRun();
    CommCarePreferenceManagerFactory.init(new AndroidPreferenceManager());

    configureCommCareEngineConstantsAndStaticRegistrations();
    initNotifications();

    //TODO: Make this robust
    PreInitLogger pil = new PreInitLogger();
    Logger.registerLogger(pil);

    // Workaround because android is written by 7 year-olds (re-uses http connection pool
    // improperly, so the second https request in a short time period will flop)
    System.setProperty("http.keepAlive", "false");

    initTls12IfNeeded();

    Thread.setDefaultUncaughtExceptionHandler(new CommCareExceptionHandler(Thread.getDefaultUncaughtExceptionHandler(), this));

    SQLiteDatabase.loadLibs(this);

    setRoots();

    prepareTemporaryStorage();

    if (LegacyInstallUtils.checkForLegacyInstall(this)) {
        dbState = STATE_LEGACY_DETECTED;
    } else {
        // Init global storage (Just application records, logs, etc)
        dbState = initGlobalDb();
    }

    setupLoggerStorage(false);
    pil.dumpToNewLogger();

    initializeDefaultLocalizerData();

    if (dbState != STATE_MIGRATION_FAILED && dbState != STATE_MIGRATION_QUESTIONABLE) {
        AppUtils.checkForIncompletelyUninstalledApps();
        initializeAnAppOnStartup();
    }

    LocalePreferences.saveDeviceLocale(Locale.getDefault());
}
 
Example 13
Source File: EncryptedDatabaseOpenHelper.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
EncryptedDatabaseOpenHelper(Context context, String databaseName, int targetVersion) {
    super(context, databaseName, null, targetVersion, hook);
    SQLiteDatabase.loadLibs(context);
    this.baseHelper = new BaseDatabaseOpenHelper(context, targetVersion);
}
 
Example 14
Source File: PersonDBHelper.java    From Android-Debug-Database with Apache License 2.0 4 votes vote down vote up
public PersonDBHelper(Context context) {

        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase.loadLibs(context);
    }
 
Example 15
Source File: DebugDBEncryptFactory.java    From Android-Debug-Database with Apache License 2.0 4 votes vote down vote up
@Override
public SQLiteDB create(Context context, String path, String password) {
    SQLiteDatabase.loadLibs(context);
    return new DebugEncryptSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, password, null));
}
 
Example 16
Source File: AlchemySQLCipher4aTest.java    From alchemy with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void loadJni() {
    SQLiteDatabase.loadLibs(InstrumentationRegistry.getContext());
}
 
Example 17
Source File: SQLCipherDbTest.java    From alchemy with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void loadJni() {
    SQLiteDatabase.loadLibs(InstrumentationRegistry.getContext());
}
 
Example 18
Source File: DBUtill.java    From hayoou-wechat-export with GNU General Public License v3.0 4 votes vote down vote up
public static void init(Context context) {
    SQLiteDatabase.loadLibs(context);
    File file = new File(context.getFilesDir().getPath() + "EnMicroMsg0.db");

}
 
Example 19
Source File: SQLCipherUtils.java    From kripton with Apache License 2.0 2 votes vote down vote up
/**
 * Determine whether or not this database appears to be encrypted, based
 * on whether we can open it without a passphrase.
 *
 * @param ctxt a Context
 * @param dbName the name of the database, as used with Room, SQLiteOpenHelper,
 *               etc.
 * @return the detected state of the database
 */
public static State getDatabaseState(Context ctxt, String dbName) {
  SQLiteDatabase.loadLibs(ctxt);

  return(getDatabaseState(ctxt.getDatabasePath(dbName)));
}
 
Example 20
Source File: SQLCipherUtils.java    From cwac-saferoom with Apache License 2.0 2 votes vote down vote up
/**
 * Determine whether or not this database appears to be encrypted, based
 * on whether we can open it without a passphrase.
 *
 * @param ctxt a Context
 * @param dbName the name of the database, as used with Room, SQLiteOpenHelper,
 *               etc.
 * @return the detected state of the database
 */
public static State getDatabaseState(Context ctxt, String dbName) {
  SQLiteDatabase.loadLibs(ctxt);

  return(getDatabaseState(ctxt.getDatabasePath(dbName)));
}