Skip to content

Commit a267c4b

Browse files
Add overloads to openDatabase and openOrCreateDatabase for providing password
1 parent cc47c99 commit a267c4b

File tree

1 file changed

+191
-6
lines changed

1 file changed

+191
-6
lines changed

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteDatabase.java

Lines changed: 191 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040

4141
import java.io.File;
4242
import java.io.FileFilter;
43+
import java.nio.ByteBuffer;
44+
import java.nio.CharBuffer;
45+
import java.nio.charset.Charset;
4346
import java.util.ArrayList;
4447
import java.util.HashMap;
4548
import java.util.List;
@@ -255,11 +258,12 @@ protected SQLiteSession initialValue() {
255258
*/
256259
public static final int MAX_SQL_CACHE_SIZE = 100;
257260

258-
private SQLiteDatabase(String path, int openFlags, CursorFactory cursorFactory,
259-
DatabaseErrorHandler errorHandler) {
261+
262+
private SQLiteDatabase(String path, byte[] password, int openFlags, CursorFactory cursorFactory,
263+
DatabaseErrorHandler errorHandler, SQLiteDatabaseHook hook) {
260264
mCursorFactory = cursorFactory;
261265
mErrorHandler = errorHandler != null ? errorHandler : new DefaultDatabaseErrorHandler();
262-
mConfigurationLocked = new SQLiteDatabaseConfiguration(path, openFlags);
266+
mConfigurationLocked = new SQLiteDatabaseConfiguration(path, openFlags, password, hook);
263267
}
264268

265269
@Override
@@ -673,6 +677,48 @@ public static SQLiteDatabase openDatabase(String path, CursorFactory factory, in
673677
return openDatabase(path, factory, flags, null);
674678
}
675679

680+
/**
681+
* Open the database according to the flags {@link #OPEN_READWRITE}
682+
* {@link #OPEN_READONLY} {@link #CREATE_IF_NECESSARY} and/or {@link #NO_LOCALIZED_COLLATORS}.
683+
*
684+
* <p>Sets the locale of the database to the the system's current locale.
685+
* Call {@link #setLocale} if you would like something else.</p>
686+
*
687+
* @param path to database file to open and/or create
688+
* @param password for use with a SQLCipher database
689+
* @param factory an optional factory class that is called to instantiate a
690+
* cursor when query is called, or null for default
691+
* @param flags to control database access mode
692+
* @param databaseHook to invoke preKey and postKey operations with SQLCipher
693+
* @return the newly opened database
694+
* @throws SQLiteException if the database cannot be opened
695+
*/
696+
public static SQLiteDatabase openDatabase(String path, String password, CursorFactory factory,
697+
int flags, SQLiteDatabaseHook databaseHook) {
698+
return openDatabase(path, getBytes(password), factory, flags, null, databaseHook);
699+
}
700+
701+
/**
702+
* Open the database according to the flags {@link #OPEN_READWRITE}
703+
* {@link #OPEN_READONLY} {@link #CREATE_IF_NECESSARY} and/or {@link #NO_LOCALIZED_COLLATORS}.
704+
*
705+
* <p>Sets the locale of the database to the the system's current locale.
706+
* Call {@link #setLocale} if you would like something else.</p>
707+
*
708+
* @param path to database file to open and/or create
709+
* @param password for use with a SQLCipher database
710+
* @param factory an optional factory class that is called to instantiate a
711+
* cursor when query is called, or null for default
712+
* @param flags to control database access mode
713+
* @param databaseHook to invoke preKey and postKey operations with SQLCipher
714+
* @return the newly opened database
715+
* @throws SQLiteException if the database cannot be opened
716+
*/
717+
public static SQLiteDatabase openDatabase(String path, byte[] password, CursorFactory factory,
718+
int flags, SQLiteDatabaseHook databaseHook) {
719+
return openDatabase(path, password, factory, flags, null, databaseHook);
720+
}
721+
676722
/**
677723
* Open the database according to the flags {@link #OPEN_READWRITE}
678724
* {@link #OPEN_READONLY} {@link #CREATE_IF_NECESSARY} and/or {@link #NO_LOCALIZED_COLLATORS}.
@@ -693,8 +739,62 @@ public static SQLiteDatabase openDatabase(String path, CursorFactory factory, in
693739
* @throws SQLiteException if the database cannot be opened
694740
*/
695741
public static SQLiteDatabase openDatabase(String path, CursorFactory factory, int flags,
696-
DatabaseErrorHandler errorHandler) {
697-
SQLiteDatabase db = new SQLiteDatabase(path, flags, factory, errorHandler);
742+
DatabaseErrorHandler errorHandler) {
743+
return openDatabase(path, new byte[0], factory, flags, errorHandler, null);
744+
}
745+
746+
/**
747+
* Open the database according to the flags {@link #OPEN_READWRITE}
748+
* {@link #OPEN_READONLY} {@link #CREATE_IF_NECESSARY} and/or {@link #NO_LOCALIZED_COLLATORS}.
749+
*
750+
* <p>Sets the locale of the database to the the system's current locale.
751+
* Call {@link #setLocale} if you would like something else.</p>
752+
*
753+
* <p>Accepts input param: a concrete instance of {@link DatabaseErrorHandler} to be
754+
* used to handle corruption when sqlite reports database corruption.</p>
755+
*
756+
* @param path to database file to open and/or create
757+
* @param password for use with a SQLCipher database
758+
* @param factory an optional factory class that is called to instantiate a
759+
* cursor when query is called, or null for default
760+
* @param flags to control database access mode
761+
* @param errorHandler the {@link DatabaseErrorHandler} obj to be used to handle corruption
762+
* when sqlite reports database corruption
763+
* @param databaseHook to invoke preKey and postKey operations with SQLCipher
764+
* @return the newly opened database
765+
* @throws SQLiteException if the database cannot be opened
766+
*/
767+
public static SQLiteDatabase openDatabase(String path, String password, CursorFactory factory,
768+
int flags, DatabaseErrorHandler errorHandler,
769+
SQLiteDatabaseHook databaseHook) {
770+
return openDatabase(path, getBytes(password), factory, flags, errorHandler, databaseHook);
771+
}
772+
773+
/**
774+
* Open the database according to the flags {@link #OPEN_READWRITE}
775+
* {@link #OPEN_READONLY} {@link #CREATE_IF_NECESSARY} and/or {@link #NO_LOCALIZED_COLLATORS}.
776+
*
777+
* <p>Sets the locale of the database to the the system's current locale.
778+
* Call {@link #setLocale} if you would like something else.</p>
779+
*
780+
* <p>Accepts input param: a concrete instance of {@link DatabaseErrorHandler} to be
781+
* used to handle corruption when sqlite reports database corruption.</p>
782+
*
783+
* @param path to database file to open and/or create
784+
* @param password for use with a SQLCipher database
785+
* @param factory an optional factory class that is called to instantiate a
786+
* cursor when query is called, or null for default
787+
* @param flags to control database access mode
788+
* @param errorHandler the {@link DatabaseErrorHandler} obj to be used to handle corruption
789+
* when sqlite reports database corruption
790+
* @param databaseHook to invoke preKey and postKey operations with SQLCipher
791+
* @return the newly opened database
792+
* @throws SQLiteException if the database cannot be opened
793+
*/
794+
public static SQLiteDatabase openDatabase(String path, byte[] password, CursorFactory factory,
795+
int flags, DatabaseErrorHandler errorHandler,
796+
SQLiteDatabaseHook databaseHook) {
797+
SQLiteDatabase db = new SQLiteDatabase(path, password, flags, factory, errorHandler, databaseHook);
698798
db.open();
699799
return db;
700800
}
@@ -717,10 +817,86 @@ public static SQLiteDatabase openOrCreateDatabase(String path, CursorFactory fac
717817
* Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY, errorHandler).
718818
*/
719819
public static SQLiteDatabase openOrCreateDatabase(String path, CursorFactory factory,
720-
DatabaseErrorHandler errorHandler) {
820+
DatabaseErrorHandler errorHandler) {
721821
return openDatabase(path, factory, CREATE_IF_NECESSARY, errorHandler);
722822
}
723823

824+
/**
825+
* Equivalent to openDatabase(file.getAbsolutePath(), password, factory, CREATE_IF_NECESSARY, errorHandler, null).
826+
*/
827+
public static SQLiteDatabase openOrCreateDatabase(File file, String password,
828+
CursorFactory factory,
829+
DatabaseErrorHandler errorHandler) {
830+
return openDatabase(file.getAbsolutePath(), password, factory, CREATE_IF_NECESSARY, errorHandler, null);
831+
}
832+
833+
/**
834+
* Equivalent to openDatabase(file.getAbsolutePath(), password, factory, CREATE_IF_NECESSARY, errorHandler, null).
835+
*/
836+
public static SQLiteDatabase openOrCreateDatabase(File file, byte[] password,
837+
CursorFactory factory,
838+
DatabaseErrorHandler errorHandler) {
839+
return openDatabase(file.getAbsolutePath(), password, factory, CREATE_IF_NECESSARY, errorHandler, null);
840+
}
841+
842+
/**
843+
* Equivalent to openDatabase(path, password, factory, CREATE_IF_NECESSARY, errorHandler, null).
844+
*/
845+
public static SQLiteDatabase openOrCreateDatabase(String path, String password,
846+
CursorFactory factory,
847+
DatabaseErrorHandler errorHandler) {
848+
return openDatabase(path, password, factory, CREATE_IF_NECESSARY, errorHandler, null);
849+
}
850+
851+
/**
852+
* Equivalent to openDatabase(path, password, factory, CREATE_IF_NECESSARY, errorHandler, null).
853+
*/
854+
public static SQLiteDatabase openOrCreateDatabase(String path, byte[] password,
855+
CursorFactory factory,
856+
DatabaseErrorHandler errorHandler) {
857+
return openDatabase(path, password, factory, CREATE_IF_NECESSARY, errorHandler, null);
858+
}
859+
860+
/**
861+
* Equivalent to openDatabase(file.getAbsolutePath(), password, factory, CREATE_IF_NECESSARY, errorHandler, databaseHook).
862+
*/
863+
public static SQLiteDatabase openOrCreateDatabase(File file, String password,
864+
CursorFactory factory,
865+
DatabaseErrorHandler errorHandler,
866+
SQLiteDatabaseHook databaseHook) {
867+
return openDatabase(file.getAbsolutePath(), password, factory, CREATE_IF_NECESSARY, errorHandler, databaseHook);
868+
}
869+
870+
/**
871+
* Equivalent to openDatabase(file.getAbsolutePath(), password, factory, CREATE_IF_NECESSARY, errorHandler, databaseHook).
872+
*/
873+
public static SQLiteDatabase openOrCreateDatabase(File file, byte[] password,
874+
CursorFactory factory,
875+
DatabaseErrorHandler errorHandler,
876+
SQLiteDatabaseHook databaseHook) {
877+
return openDatabase(file.getAbsolutePath(), password, factory, CREATE_IF_NECESSARY, errorHandler, databaseHook);
878+
}
879+
880+
/**
881+
* Equivalent to openDatabase(path, password, factory, CREATE_IF_NECESSARY, errorHandler, databaseHook).
882+
*/
883+
public static SQLiteDatabase openOrCreateDatabase(String path, String password,
884+
CursorFactory factory,
885+
DatabaseErrorHandler errorHandler,
886+
SQLiteDatabaseHook databaseHook) {
887+
return openDatabase(path, password, factory, CREATE_IF_NECESSARY, errorHandler, databaseHook);
888+
}
889+
890+
/**
891+
* Equivalent to openDatabase(path, password, factory, CREATE_IF_NECESSARY, errorHandler, databaseHook).
892+
*/
893+
public static SQLiteDatabase openOrCreateDatabase(String path, byte[] password,
894+
CursorFactory factory,
895+
DatabaseErrorHandler errorHandler,
896+
SQLiteDatabaseHook databaseHook) {
897+
return openDatabase(path, password, factory, CREATE_IF_NECESSARY, errorHandler, databaseHook);
898+
}
899+
724900
/**
725901
* Deletes a database including its journal file and other auxiliary files
726902
* that may have been created by the database engine.
@@ -2220,4 +2396,13 @@ public static boolean hasCodec() {
22202396
public void enableLocalizedCollators() {
22212397
mConnectionPoolLocked.enableLocalizedCollators();
22222398
}
2399+
2400+
private static byte[] getBytes(String data) {
2401+
if(data == null || data.length() == 0) return new byte[0];
2402+
CharBuffer charBuffer = CharBuffer.wrap(data);
2403+
ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
2404+
byte[] result = new byte[byteBuffer.limit()];
2405+
byteBuffer.get(result);
2406+
return result;
2407+
}
22232408
}

0 commit comments

Comments
 (0)