Skip to content

Commit

Permalink
closes #97
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinezehp committed Jun 14, 2023
1 parent 6b65ec4 commit 3df4f1c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.idea/
dist/
capacitor-native-biometric-4.1.3.tgz
*.tgz
/node_modules
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.security.KeyPairGeneratorSpec;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.security.keystore.StrongBoxUnavailableException;
import android.util.Base64;

import androidx.activity.result.ActivityResult;
Expand Down Expand Up @@ -283,7 +284,17 @@ private String decryptString(String stringToDecrypt, String KEY_ALIAS) throws Ge
return new String(decryptedData, "UTF-8");
}

@SuppressLint("NewAPI") // API level is already checked
private Key generateKey(String KEY_ALIAS) throws GeneralSecurityException, IOException {
Key key;
try {
key = generateKey(KEY_ALIAS, true);
} catch (StrongBoxUnavailableException e){
key = generateKey(KEY_ALIAS, false);
}
return key;
}
private Key generateKey(String KEY_ALIAS, boolean isStrongBoxBacked) throws GeneralSecurityException, IOException, StrongBoxUnavailableException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
KeyGenerator generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE);
KeyGenParameterSpec.Builder paramBuilder = new KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
Expand All @@ -293,7 +304,7 @@ private Key generateKey(String KEY_ALIAS) throws GeneralSecurityException, IOExc

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
paramBuilder.setUnlockedDeviceRequired(true);
paramBuilder.setIsStrongBoxBacked(true);
paramBuilder.setIsStrongBoxBacked(isStrongBoxBacked);
}

generator.init(paramBuilder.build());
Expand Down

0 comments on commit 3df4f1c

Please sign in to comment.