Skip to content

Commit

Permalink
fix: Add a fixture for parsing date in rtl languages for new SDKs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad Rezaei committed Jul 27, 2022
1 parent 214a971 commit a6ba3ff
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
Expand All @@ -24,6 +26,7 @@
import java.security.spec.InvalidKeySpecException;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Locale;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
Expand Down Expand Up @@ -109,10 +112,6 @@ private boolean generateKeyIfNecessary(@NonNull Context context, @NonNull KeySto
return false;
}

private boolean generateKey(Context context, String keystoreAlias, boolean isAuthenticationRequired) {
return generateKey(keystoreAlias, isAuthenticationRequired);
}

private String decode(String encodedString, Cipher cipher) throws PFSecurityException {
try {
final byte[] bytes = Base64.decode(encodedString, Base64.NO_WRAP);
Expand Down Expand Up @@ -143,8 +142,14 @@ public String decode(String alias, String encodedString) throws PFSecurityExcept
}

@TargetApi(Build.VERSION_CODES.M)
private boolean generateKey(String keystoreAlias, boolean isAuthenticationRequired) {
private boolean generateKey(Context context, String keystoreAlias, boolean isAuthenticationRequired) {
try {
// Set English locale as default (workaround for rtl parsing date exception)
// From https://stackoverflow.com/a/46602170
// FIXME: A temporary fixture for issue described at https://issuetracker.google.com/issues/37095309
Locale initialLocale = Locale.getDefault();
setLocale(context, Locale.ENGLISH);

final KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
keyGenerator.initialize(
Expand All @@ -156,6 +161,8 @@ private boolean generateKey(String keystoreAlias, boolean isAuthenticationRequir
.setUserAuthenticationRequired(isAuthenticationRequired)
.build());
keyGenerator.generateKeyPair();
// Reset default locale
setLocale(context, initialLocale);
return true;

} catch ( NoSuchAlgorithmException
Expand All @@ -166,6 +173,14 @@ private boolean generateKey(String keystoreAlias, boolean isAuthenticationRequir
}
}

private void setLocale(Context context, Locale locale) {
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
config.locale = locale;
resources.updateConfiguration(config, resources.getDisplayMetrics());
}

private Cipher getCipherInstance() throws PFSecurityException {
try {
final Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
Expand Down

0 comments on commit a6ba3ff

Please sign in to comment.