Skip to content

Commit

Permalink
Support encrypt data while updating.
Browse files Browse the repository at this point in the history
  • Loading branch information
sinyu890807 committed Jun 21, 2017
1 parent 5486c33 commit b8a98b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
30 changes: 24 additions & 6 deletions litepal/src/main/java/org/litepal/crud/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,7 @@ protected void putContentValuesForSave(DataSupport baseObj, Field field, Content
}
Encrypt annotation = field.getAnnotation(Encrypt.class);
if (annotation != null && "java.lang.String".equals(field.getType().getName())) {
String algorithm = annotation.algorithm();
if (DataSupport.AES.equalsIgnoreCase(algorithm)) {
fieldValue = CipherUtil.aesEncrypt((String) fieldValue);
} else if (DataSupport.MD5.equalsIgnoreCase(algorithm)) {
fieldValue = CipherUtil.md5Encrypt((String) fieldValue);
}
fieldValue = encryptValue(annotation.algorithm(), fieldValue);
}
Object[] parameters = new Object[] { changeCase(DBUtility.convertToValidColumnName(field.getName())), fieldValue };
Class<?>[] parameterTypes = getParameterTypes(field, fieldValue, parameters);
Expand Down Expand Up @@ -311,11 +306,34 @@ protected void putContentValuesForUpdate(DataSupport baseObj, Field field, Conte
Date date = (Date) fieldValue;
fieldValue = date.getTime();
}
Encrypt annotation = field.getAnnotation(Encrypt.class);
if (annotation != null && "java.lang.String".equals(field.getType().getName())) {
fieldValue = encryptValue(annotation.algorithm(), fieldValue);
}
Object[] parameters = new Object[] { changeCase(DBUtility.convertToValidColumnName(field.getName())), fieldValue };
Class<?>[] parameterTypes = getParameterTypes(field, fieldValue, parameters);
DynamicExecutor.send(values, "put", parameters, values.getClass(), parameterTypes);
}

/**
* Encrypt the field value with targeted algorithm.
* @param algorithm
* The algorithm to encrypt value.
* @param fieldValue
* Field value to encrypt.
* @return Encrypted value by targeted algorithm.
*/
protected Object encryptValue(String algorithm, Object fieldValue) {
if (algorithm != null) {
if (DataSupport.AES.equalsIgnoreCase(algorithm)) {
fieldValue = CipherUtil.aesEncrypt((String) fieldValue);
} else if (DataSupport.MD5.equalsIgnoreCase(algorithm)) {
fieldValue = CipherUtil.md5Encrypt((String) fieldValue);
}
}
return fieldValue;
}

/**
* Get the field value for model.
*
Expand Down
9 changes: 1 addition & 8 deletions litepal/src/main/java/org/litepal/crud/SaveHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ private boolean shouldGiveModelIdValue(String idName, Class<?> idType, long id)
private void updateGenericTables(DataSupport baseObj, List<Field> supportedGenericFields,
long id) throws IllegalAccessException, InvocationTargetException {
for (Field field : supportedGenericFields) {
String genericTypeName = getGenericTypeName(field);
Encrypt annotation = field.getAnnotation(Encrypt.class);
String algorithm = null;
if (annotation != null && "java.lang.String".equals(getGenericTypeName(field))) {
Expand All @@ -579,13 +578,7 @@ private void updateGenericTables(DataSupport baseObj, List<Field> supportedGener
for (Object object : collection) {
ContentValues values = new ContentValues();
values.put(genericValueIdColumnName, id);
if (algorithm != null) {
if (DataSupport.AES.equalsIgnoreCase(algorithm)) {
object = CipherUtil.aesEncrypt((String) object);
} else if (DataSupport.MD5.equalsIgnoreCase(algorithm)) {
object = CipherUtil.md5Encrypt((String) object);
}
}
object = encryptValue(algorithm, object);
Object[] parameters = new Object[] { changeCase(DBUtility.convertToValidColumnName(field.getName())), object };
Class<?>[] parameterTypes = new Class[] { String.class, getGenericTypeClass(field) };
DynamicExecutor.send(values, "put", parameters, values.getClass(), parameterTypes);
Expand Down
7 changes: 7 additions & 0 deletions litepal/src/main/java/org/litepal/crud/UpdateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;

import org.litepal.annotation.Encrypt;
import org.litepal.crud.model.AssociationsInfo;
import org.litepal.exceptions.DataSupportException;
import org.litepal.util.BaseUtility;
Expand Down Expand Up @@ -331,6 +332,11 @@ private void updateGenericTables(DataSupport baseObj, List<Field> supportedGener
long... ids) throws IllegalAccessException, InvocationTargetException {
if (ids != null && ids.length > 0) {
for (Field field : supportedGenericFields) {
Encrypt annotation = field.getAnnotation(Encrypt.class);
String algorithm = null;
if (annotation != null && "java.lang.String".equals(getGenericTypeName(field))) {
algorithm = annotation.algorithm();
}
field.setAccessible(true);
Collection<?> collection = (Collection<?>) field.get(baseObj);
if (collection != null && !collection.isEmpty()) {
Expand All @@ -341,6 +347,7 @@ private void updateGenericTables(DataSupport baseObj, List<Field> supportedGener
for (Object object : collection) {
ContentValues values = new ContentValues();
values.put(genericValueIdColumnName, id);
object = encryptValue(algorithm, object);
Object[] parameters = new Object[] { DBUtility.convertToValidColumnName(changeCase(field.getName())), object };
Class<?>[] parameterTypes = new Class[] { String.class, getGenericTypeClass(field) };
DynamicExecutor.send(values, "put", parameters, values.getClass(), parameterTypes);
Expand Down

0 comments on commit b8a98b2

Please sign in to comment.