Skip to content

Commit 0ecb7c4

Browse files
Add direct change password support via SQLiteDatabase.changePassword
1 parent e5066aa commit 0ecb7c4

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

jni/net_sqlcipher_database_SQLiteDatabase.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,68 @@ void native_key_str(JNIEnv* env, jobject object, jstring jKey)
171171
env->ReleaseStringUTFChars(jKey, key);
172172
}
173173

174+
void native_rekey_char(JNIEnv* env, jobject object, jcharArray jKey)
175+
{
176+
char *keyUtf8 = 0;
177+
int lenUtf8 = 0;
178+
jchar* keyUtf16 = 0;
179+
jsize lenUtf16 = 0;
180+
UErrorCode status = U_ZERO_ERROR;
181+
UConverter *encoding = 0;
182+
183+
sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);
184+
185+
keyUtf16 = env->GetCharArrayElements(jKey, 0);
186+
lenUtf16 = env->GetArrayLength(jKey);
187+
188+
if ( lenUtf16 == 0 ) goto done;
189+
190+
encoding = ucnv_open("UTF-8", &status);
191+
if( U_FAILURE(status) ) {
192+
throw_sqlite3_exception(env, "native_key_char: opening encoding converter failed");
193+
goto done;
194+
}
195+
196+
lenUtf8 = ucnv_fromUChars(encoding, NULL, 0, keyUtf16, lenUtf16, &status);
197+
status = (status == U_BUFFER_OVERFLOW_ERROR) ? U_ZERO_ERROR : status;
198+
if( U_FAILURE(status) ) {
199+
throw_sqlite3_exception(env, "native_key_char: utf8 length unknown");
200+
goto done;
201+
}
202+
203+
keyUtf8 = (char*) malloc(lenUtf8 * sizeof(char));
204+
ucnv_fromUChars(encoding, keyUtf8, lenUtf8, keyUtf16, lenUtf16, &status);
205+
if( U_FAILURE(status) ) {
206+
throw_sqlite3_exception(env, "native_key_char: utf8 conversion failed");
207+
goto done;
208+
}
209+
210+
if ( sqlite3_rekey(handle, keyUtf8, lenUtf8) != SQLITE_OK ) {
211+
throw_sqlite3_exception(env, handle);
212+
}
213+
214+
done:
215+
env->ReleaseCharArrayElements(jKey, keyUtf16, 0);
216+
if(encoding != 0) ucnv_close(encoding);
217+
if(keyUtf8 != 0) free(keyUtf8);
218+
}
219+
220+
void native_rekey_str(JNIEnv* env, jobject object, jstring jKey)
221+
{
222+
sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);
223+
224+
char const * key = env->GetStringUTFChars(jKey, NULL);
225+
jsize keyLen = env->GetStringUTFLength(jKey);
226+
227+
if ( keyLen > 0 ) {
228+
int status = sqlite3_rekey(handle, key, keyLen);
229+
if ( status != SQLITE_OK ) {
230+
throw_sqlite3_exception(env, handle);
231+
}
232+
}
233+
env->ReleaseStringUTFChars(jKey, key);
234+
}
235+
174236
void native_rawExecSQL(JNIEnv* env, jobject object, jstring sql)
175237
{
176238
sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);
@@ -561,6 +623,8 @@ static JNINativeMethod sMethods[] =
561623
{"native_status", "(IZ)I", (void *)native_status},
562624
{"native_key", "([C)V", (void *)native_key_char},
563625
{"native_key", "(Ljava/lang/String;)V", (void *)native_key_str},
626+
{"native_rekey", "([C)V", (void *)native_rekey_char},
627+
{"native_rekey", "(Ljava/lang/String;)V", (void *)native_rekey_str},
564628
};
565629

566630
int register_android_database_SQLiteDatabase(JNIEnv *env)

src/net/sqlcipher/database/SQLiteDatabase.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ public class SQLiteDatabase extends SQLiteClosable {
7777
public int status(int operation, boolean reset){
7878
return native_status(operation, reset);
7979
}
80-
80+
81+
public void changePassword(String password) throws SQLiteException {
82+
native_rekey(password);
83+
}
84+
85+
public void changePassword(char[] password) throws SQLiteException {
86+
native_rekey(password);
87+
}
88+
8189
private static void loadICUData(Context context, File workingDir) {
8290

8391
try {
@@ -2428,4 +2436,7 @@ private static ArrayList<Pair<String, String>> getAttachedDbs(SQLiteDatabase dbO
24282436

24292437
private native void native_key(char[] key) throws SQLException;
24302438
private native void native_key(String key) throws SQLException;
2439+
2440+
private native void native_rekey(String key) throws SQLException;
2441+
private native void native_rekey(char[] key) throws SQLException;
24312442
}

0 commit comments

Comments
 (0)