Java Code Examples for net.sqlcipher.database.SQLiteDatabase#close()
The following examples show how to use
net.sqlcipher.database.SQLiteDatabase#close() .
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: SQLCipherUtils.java From cwac-saferoom with Apache License 2.0 | 6 votes |
/** * Determine whether or not this database appears to be encrypted, based * on whether we can open it without a passphrase. * * NOTE: You are responsible for ensuring that net.sqlcipher.database.SQLiteDatabase.loadLibs() * is called before calling this method. This is handled automatically with the * getDatabaseState() method that takes a Context as a parameter. * * @param dbPath a File pointing to the database * @return the detected state of the database */ public static State getDatabaseState(File dbPath) { if (dbPath.exists()) { SQLiteDatabase db=null; try { db= SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READONLY); db.getVersion(); return(State.UNENCRYPTED); } catch (Exception e) { return(State.ENCRYPTED); } finally { if (db != null) { db.close(); } } } return(State.DOES_NOT_EXIST); }
Example 2
Source File: PersonDBHelper.java From Android-Debug-Database with Apache License 2.0 | 6 votes |
public ArrayList<String> getAllPerson() { ArrayList<String> arrayList = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(DB_PASSWORD); Cursor res = db.rawQuery("select * from person", null); res.moveToFirst(); while (!res.isAfterLast()) { arrayList.add( res.getString(res.getColumnIndex(PERSON_COLUMN_FIRST_NAME)) + " " + res.getString(res.getColumnIndex(PERSON_COLUMN_LAST_NAME))); res.moveToNext(); } res.close(); db.close(); return arrayList; }
Example 3
Source File: SQLCipherUtils.java From kripton with Apache License 2.0 | 6 votes |
/** * Determine whether or not this database appears to be encrypted, based * on whether we can open it without a passphrase. * * NOTE: You are responsible for ensuring that net.sqlcipher.database.SQLiteDatabase.loadLibs() * is called before calling this method. This is handled automatically with the * getDatabaseState() method that takes a Context as a parameter. * * @param dbPath a File pointing to the database * @return the detected state of the database */ public static State getDatabaseState(File dbPath) { if (dbPath.exists()) { SQLiteDatabase db=null; try { db= SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READONLY); db.getVersion(); return(State.UNENCRYPTED); } catch (Exception e) { return(State.ENCRYPTED); } finally { if (db != null) { db.close(); } } } return(State.DOES_NOT_EXIST); }
Example 4
Source File: DemoUserBuilder.java From commcare-android with Apache License 2.0 | 6 votes |
private void writeNewUser(UserKeyRecord keyRecord) { SQLiteDatabase userDatabase = null; try { userDatabase = new DatabaseUserOpenHelper(CommCareApplication.instance(), keyRecord.getUuid()).getWritableDatabase(UserSandboxUtils.getSqlCipherEncodedKey(randomKey)); User user = new User(username, passwordHash, username, userType); SqlStorage<User> userStorage = new SqlStorage<>(User.STORAGE_KEY, User.class, new ConcreteAndroidDbHelper(context, userDatabase)); userStorage.write(user); } finally { if (userDatabase != null) { userDatabase.close(); } } }
Example 5
Source File: SQLCipherUtils.java From cwac-saferoom with Apache License 2.0 | 5 votes |
/** * 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 6
Source File: SQLCipherUtils.java From cwac-saferoom with Apache License 2.0 | 5 votes |
/** * 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 7
Source File: PersonDBHelper.java From Android-Debug-Database with Apache License 2.0 | 5 votes |
public boolean insertPerson(String firstName, String lastName, String address) { SQLiteDatabase db = this.getWritableDatabase(DB_PASSWORD); ContentValues contentValues = new ContentValues(); contentValues.put("first_name", firstName); contentValues.put("last_name", lastName); contentValues.put("address", address); db.insert("person", null, contentValues); db.close(); return true; }
Example 8
Source File: PersonDBHelper.java From Android-Debug-Database with Apache License 2.0 | 5 votes |
public boolean updatePerson(Integer id, String firstName, String lastName, String address, float mileage) { SQLiteDatabase db = this.getWritableDatabase(DB_PASSWORD); ContentValues contentValues = new ContentValues(); contentValues.put("first_name", firstName); contentValues.put("last_name", lastName); contentValues.put("address", address); db.update("person", contentValues, "id = ? ", new String[]{Integer.toString(id)}); db.close(); return true; }
Example 9
Source File: PersonDBHelper.java From Android-Debug-Database with Apache License 2.0 | 5 votes |
public int count() { SQLiteDatabase db = getReadableDatabase(DB_PASSWORD); Cursor cursor = db.rawQuery("select * from person", null); try { if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); return cursor.getInt(0); } else { return 0; } } finally { cursor.close(); db.close(); } }
Example 10
Source File: SQLCipherUtils.java From kripton with Apache License 2.0 | 5 votes |
/** * 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 11
Source File: SQLCipherUtils.java From kripton with Apache License 2.0 | 5 votes |
/** * 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 12
Source File: UserSandboxUtils.java From commcare-android with Apache License 2.0 | 5 votes |
public static void migrateData(Context c, CommCareApp app, UserKeyRecord incomingSandbox, byte[] unwrappedOldKey, UserKeyRecord newSandbox, byte[] unwrappedNewKey) throws IOException { Logger.log(LogTypes.TYPE_MAINTENANCE, "Migrating an existing user sandbox for " + newSandbox.getUsername()); String newKeyEncoded = rekeyDB(c, incomingSandbox, newSandbox, unwrappedOldKey, unwrappedNewKey); Logger.log(LogTypes.TYPE_MAINTENANCE, "Database is re-keyed and ready for use. Copying over files now"); //OK, so now we have the Db transitioned. What we need to do now is go through and rekey all of our file references. final SQLiteDatabase db = new DatabaseUserOpenHelper(CommCareApplication.instance(), newSandbox.getUuid()).getWritableDatabase(newKeyEncoded); try { //If we were able to iterate over the users, the key was fine, so let's use it to open our db AndroidDbHelper dbh = new AndroidDbHelper(c) { @Override public SQLiteDatabase getHandle() { return db; } }; //TODO: At some point we should really just encode the existence/operations on files in the record models themselves //Models with Files: Form Record. Log Record SqlStorage<DeviceReportRecord> reports = new SqlStorage<>(DeviceReportRecord.STORAGE_KEY, DeviceReportRecord.class, dbh); migrateDeviceReports(reports, newSandbox); Logger.log(LogTypes.TYPE_MAINTENANCE, "Copied over all of the device reports. Moving on to the form records"); SqlStorage<FormRecord> formRecords = new SqlStorage<>(FormRecord.STORAGE_KEY, FormRecord.class, dbh); migrateFormRecords(formRecords, newSandbox); } finally { db.close(); } Logger.log(LogTypes.TYPE_MAINTENANCE, "All form records copied over"); //OK! So we should be all set, here. Mark the new sandbox as ready and the old sandbox as ready for cleanup. finalizeMigration(app, incomingSandbox, newSandbox); }
Example 13
Source File: UserSandboxUtils.java From commcare-android with Apache License 2.0 | 5 votes |
/** * Make a copy of the incoming sandbox's database and re-key it to use the new key. */ private static String rekeyDB(Context c, UserKeyRecord incomingSandbox, UserKeyRecord newSandbox, byte[] unwrappedOldKey, byte[] unwrappedNewKey) throws IOException { File oldDb = c.getDatabasePath(DatabaseUserOpenHelper.getDbName(incomingSandbox.getUuid())); File newDb = c.getDatabasePath(DatabaseUserOpenHelper.getDbName(newSandbox.getUuid())); //TODO: Make sure old sandbox is already on newest version? if (newDb.exists()) { if (!newDb.delete()) { throw new IOException("Couldn't clear file location " + newDb.getAbsolutePath() + " for new sandbox database"); } } FileUtil.copyFile(oldDb, newDb); Logger.log(LogTypes.TYPE_MAINTENANCE, "Created a copy of the DB for the new sandbox. Re-keying it..."); String oldKeyEncoded = getSqlCipherEncodedKey(unwrappedOldKey); String newKeyEncoded = getSqlCipherEncodedKey(unwrappedNewKey); SQLiteDatabase rawDbHandle = SQLiteDatabase.openDatabase(newDb.getAbsolutePath(), oldKeyEncoded, null, SQLiteDatabase.OPEN_READWRITE); rawDbHandle.execSQL("PRAGMA key = '" + oldKeyEncoded + "';"); rawDbHandle.execSQL("PRAGMA rekey = '" + newKeyEncoded + "';"); rawDbHandle.close(); return newKeyEncoded; }