From c199732fa475b98add089e050262bbe6537b6cff Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Wed, 28 Feb 2018 12:31:14 +0100 Subject: [PATCH] First step --- app/build.gradle | 3 +- app/src/main/AndroidManifest.xml | 4 + .../Activities/SettingsActivity.java | 132 ++++++++++++++++++ .../FingerprintHelper/FingerprintHandler.java | 34 +++++ .../layout/fragment_fingerprint_scanner.xml | 19 +++ app/src/main/res/values/strings.xml | 3 + app/src/main/res/xml/pref_exchange.xml | 5 + 7 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/nauk/coinfolio/FingerprintHelper/FingerprintHandler.java create mode 100644 app/src/main/res/layout/fragment_fingerprint_scanner.xml diff --git a/app/build.gradle b/app/build.gradle index 36b5c2e..0d7d072 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,7 +4,7 @@ android { compileSdkVersion 26 defaultConfig { applicationId "com.nauk.coinfolio" - minSdkVersion 21 + minSdkVersion 23 targetSdkVersion 26 versionCode 1 versionName "1.0" @@ -41,6 +41,7 @@ dependencies { implementation 'com.squareup.retrofit2:converter-jackson:2.2.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0' implementation 'org.apache.commons:commons-lang3:3.6' + implementation 'com.mattprecious.swirl:swirl:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2a94e3e..98e6d33 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,7 +2,11 @@ + + + = Build.VERSION_CODES.M) + { + keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); + fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE); + + if(!fingerprintManager.isHardwareDetected()) + { + findViewById(R.id.fingerprint_switch).setVisibility(View.GONE); + } + + if(ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) + { + findViewById(R.id.fingerprint_switch).setVisibility(View.GONE); + } + + if(!fingerprintManager.hasEnrolledFingerprints()) + { + findViewById(R.id.fingerprint_switch).setVisibility(View.GONE); + } + + if(!keyguardManager.isKeyguardSecure()) + { + findViewById(R.id.fingerprint_switch).setVisibility(View.GONE); + } + else + { + try { + generateKey(); + } catch (FingerprintException e) { + e.printStackTrace(); + } + + if(initCipher()) + { + cryptoObject = new FingerprintManager.CryptoObject(cipher); + + FingerprintHandler helper = new FingerprintHandler(this); + helper.startAuth(fingerprintManager, cryptoObject); + } + } + } + } + + private void generateKey() throws FingerprintException + { + try { + keyStore = KeyStore.getInstance("AndroidKeyStore"); + keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); + keyStore.load(null); + keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) + .setBlockModes(KeyProperties.BLOCK_MODE_CBC) + .setUserAuthenticationRequired(true) + .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7) + .build()); + + keyGenerator.generateKey(); + + } catch (KeyStoreException + | NoSuchAlgorithmException + | NoSuchProviderException + | InvalidAlgorithmParameterException + | CertificateException + | IOException e) { + e.printStackTrace(); + throw new FingerprintException(e); + } + } + + public boolean initCipher() + { + try { + cipher = Cipher.getInstance( + KeyProperties.KEY_ALGORITHM_AES + "/" + + KeyProperties.BLOCK_MODE_CBC + "/" + + KeyProperties.ENCRYPTION_PADDING_PKCS7); + } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { + throw new RuntimeException("Failed to get Cipher", e); + } + + try { + keyStore.load(null); + SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null); + cipher.init(Cipher.ENCRYPT_MODE, key); + return true; + } catch (KeyPermanentlyInvalidatedException e) { + return false; + } catch (KeyStoreException | CertificateException + | UnrecoverableKeyException | IOException + | NoSuchAlgorithmException | InvalidKeyException e) { + throw new RuntimeException("Failed to init Cipher", e); + } + } + + private class FingerprintException extends Exception { + public FingerprintException(Exception e) + { + super(e); + } } /** diff --git a/app/src/main/java/com/nauk/coinfolio/FingerprintHelper/FingerprintHandler.java b/app/src/main/java/com/nauk/coinfolio/FingerprintHelper/FingerprintHandler.java new file mode 100644 index 0000000..fecc7b3 --- /dev/null +++ b/app/src/main/java/com/nauk/coinfolio/FingerprintHelper/FingerprintHandler.java @@ -0,0 +1,34 @@ +package com.nauk.coinfolio.FingerprintHelper; + +import android.Manifest; +import android.content.ContentValues; +import android.content.Context; +import android.content.pm.PackageManager; +import android.hardware.fingerprint.FingerprintManager; +import android.os.CancellationSignal; +import android.support.v4.app.ActivityCompat; + +/** + * Created by Guitoune on 28/02/2018. + */ + +public class FingerprintHandler extends FingerprintManager.AuthenticationCallback { + + private CancellationSignal cancellationSignal; + private Context context; + + public FingerprintHandler(Context context) + { + this.context = context; + } + + public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) + { + cancellationSignal = new CancellationSignal(); + if(ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) + { + return; + } + manager.authenticate(cryptoObject, cancellationSignal, 0, this, null); + } +} diff --git a/app/src/main/res/layout/fragment_fingerprint_scanner.xml b/app/src/main/res/layout/fragment_fingerprint_scanner.xml new file mode 100644 index 0000000..e9c3d78 --- /dev/null +++ b/app/src/main/res/layout/fragment_fingerprint_scanner.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bd86948..6419875 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -79,6 +79,9 @@ Vibrate + + Verify your fingerprint + Exchanges settings diff --git a/app/src/main/res/xml/pref_exchange.xml b/app/src/main/res/xml/pref_exchange.xml index a0f3139..9adc88c 100644 --- a/app/src/main/res/xml/pref_exchange.xml +++ b/app/src/main/res/xml/pref_exchange.xml @@ -1,6 +1,11 @@ + +