Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make equals overridable to support different comparison logic #2

Merged
merged 1 commit into from
Nov 28, 2017

Conversation

gliechtenstein
Copy link
Contributor

Adding an override-able "equals" method enables you to switch out comparison logic.

I love the look and feel of the library but one of my top priorities is security, so I wanted to encrypt the passcode before saving, and then later use the encrypted user input to compare the two.

Instead of building the encryption into the library I thought a better approach is to just expose the comparison method so that the inherited classes can override it.

For example I'm already using a library called SecurePreferences--which makes it easy to implement an encrypted version of SharedPreferences--in my project. So I simply need to extend PasscodeView and override the equals method. The end result looks something like this:

First, override the listener so that it encrypts the passcode before saving:

passcodeView.setListener(new PasscodeView.PasscodeViewListener() {
  @Override
  public void onFail() {
  }

  @Override
  public void onSuccess(String number) {
    String encrypted = SecurePreferences.hashPrefKey(raw);
    SharedPreferences.Editor editor = keys.edit();
    editor.putString("passcode", encrypted);
    editor.commit();
    finish();
  }
});

Second, compare using the overridden equals() method:

class PView extends PasscodeView {
  public PView(Context context) {
    super(context);
  }
  @Override protected Boolean equals(String psd) {
    String after = SecurePreferences.hashPrefKey(raw);
    return after.equals(encrypted_passcode);
  }
}
PView passcodeView = new PView(PasscodeActivity.this);

So far it's working fine for me without having to change much to the original library. What do you think?

@hanks-zyh hanks-zyh merged commit 62b7f8d into hanks-zyh:master Nov 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants