skydoves / PreferenceRoom

:truck: Android processing library for managing SharedPreferences persistence efficiently and structurally.
Apache License 2.0
378 stars 26 forks source link

Any way of encrypting the whole Entity? #9

Closed albert0m closed 5 years ago

albert0m commented 5 years ago

I have a class like this:

@PreferenceEntity(name = "MyPreferences")
public class MyPreferences {

    @TypeConverter(converter = BaseGsonConverter.class)
    protected Index MyClass;

}

I need to encrypt MyClass. How can I do that?

skydoves commented 5 years ago

Hello! You can encrypt using BaseGsonConverter.class or PreferenceFunction.

public class BaseGsonConverter<T> extends PreferenceTypeConverter<T> {

    private final Gson gson;

    /**
     * default constructor will be called by PreferenceRoom
     */
    public BaseGsonConverter(Class<T> clazz) {
        super(clazz);
        this.gson = new Gson();
    }

    @Override
    public String convertObject(T object) {
        return SecurityUtils.encrypt(gson.toJson(object)); // added encrypt logic
    }

    @Override
    public T convertType(String string) {
        return gson.fromJson(SecurityUtils.decrypt(string), clazz); // added decrpyt logic
    }
}

But there are no way of encrypting the whole entity using annotation or function yet. I will consider the best way to encrypt functions on the next version. Thank you for your issue!

skydoves commented 5 years ago

@marconealberto It's implemented encrypt & decrypt automatically using @EncryptEntity annotation at version 1.1.6. It is based on AES128 encryption. You can reference demo-project. Thank you!