Skip to content

Commit

Permalink
extract a function, add null checks (oblador#146)
Browse files Browse the repository at this point in the history
* extract a function, add null checks

* rename function
  • Loading branch information
vonovak authored Sep 4, 2018
1 parent 575cd8c commit 1abc167
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public static boolean isFingerprintAuthAvailable(Context context) {
if (android.os.Build.VERSION.SDK_INT >= 23) {
FingerprintManager fingerprintManager =
(FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
return fingerprintManager.isHardwareDetected() &&
return fingerprintManager != null && fingerprintManager.isHardwareDetected() &&
fingerprintManager.hasEnrolledFingerprints();
}
return false;
}

public static boolean isSecure(Context context) {
public static boolean isDeviceSecure(Context context) {
KeyguardManager keyguardManager =
(KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
return android.os.Build.VERSION.SDK_INT >= 23 && keyguardManager.isDeviceSecure();
return Build.VERSION.SDK_INT >= 23 && keyguardManager != null && keyguardManager.isDeviceSecure();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;

@TargetApi(Build.VERSION_CODES.M)
public class CipherStorageKeystoreAESCBC implements CipherStorage {
public static final String CIPHER_STORAGE_NAME = "KeystoreAESCBC";
public static final String DEFAULT_SERVICE = "RN_KEYCHAIN_DEFAULT_ALIAS";
Expand All @@ -52,7 +53,6 @@ public int getMinSupportedApiLevel() {
return Build.VERSION_CODES.M;
}

@TargetApi(Build.VERSION_CODES.M)
@Override
public EncryptionResult encrypt(@NonNull String service, @NonNull String username, @NonNull String password) throws CryptoFailedException {
service = getDefaultServiceIfEmpty(service);
Expand All @@ -61,21 +61,7 @@ public EncryptionResult encrypt(@NonNull String service, @NonNull String usernam
KeyStore keyStore = getKeyStoreAndLoad();

if (!keyStore.containsAlias(service)) {
AlgorithmParameterSpec spec;
spec = new KeyGenParameterSpec.Builder(
service,
KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT)
.setBlockModes(ENCRYPTION_BLOCK_MODE)
.setEncryptionPaddings(ENCRYPTION_PADDING)
.setRandomizedEncryptionRequired(true)
//.setUserAuthenticationRequired(true) // Will throw InvalidAlgorithmParameterException if there is no fingerprint enrolled on the device
.setKeySize(ENCRYPTION_KEY_SIZE)
.build();

KeyGenerator generator = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM, KEYSTORE_TYPE);
generator.init(spec);

generator.generateKey();
generateKeyAndStoreUnderAlias(service);
}

Key key = keyStore.getKey(service, null);
Expand All @@ -93,6 +79,23 @@ public EncryptionResult encrypt(@NonNull String service, @NonNull String usernam
}
}

private void generateKeyAndStoreUnderAlias(@NonNull String service) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
AlgorithmParameterSpec spec = new KeyGenParameterSpec.Builder(
service,
KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT)
.setBlockModes(ENCRYPTION_BLOCK_MODE)
.setEncryptionPaddings(ENCRYPTION_PADDING)
.setRandomizedEncryptionRequired(true)
//.setUserAuthenticationRequired(true) // Will throw InvalidAlgorithmParameterException if there is no fingerprint enrolled on the device
.setKeySize(ENCRYPTION_KEY_SIZE)
.build();

KeyGenerator generator = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM, KEYSTORE_TYPE);
generator.init(spec);

generator.generateKey();
}

@Override
public DecryptionResult decrypt(@NonNull String service, @NonNull byte[] username, @NonNull byte[] password) throws CryptoFailedException {
service = getDefaultServiceIfEmpty(service);
Expand Down

0 comments on commit 1abc167

Please sign in to comment.