Skip to content

Instantly share code, notes, and snippets.

@tjohns
Created November 13, 2013 03:16
Show Gist options
  • Save tjohns/7443106 to your computer and use it in GitHub Desktop.
Save tjohns/7443106 to your computer and use it in GitHub Desktop.
Code snippet providing pre-4.4 compatibility for Android apps using SecretKeyFactory with PBKDF2WithSHA1.
import android.os.Build;
SecretKeyFactory factory;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Use compatibility key factory -- only uses lower 8-bits of passphrase chars
factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1And8bit");
Log.i(TAG, "Selected KITKAT key factory");
} else {
// Traditional key factory. Will use lower 8-bits of passphrase chars on older
// devices, and all available bits on KitKat (or newer) devices.
factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
Log.i(TAG, "Selected LEGACY key factory");
}
@dherges
Copy link

dherges commented Dec 19, 2013

May that result in different keys generated on different devices/Android versions? If yes, there'll be trouble when users install an application prior to KitKat and then upgrade their device.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment