diff --git a/FingerprintAuthentication/.gitignore b/FingerprintAuthentication/.gitignore new file mode 100644 index 0000000..a4c7838 --- /dev/null +++ b/FingerprintAuthentication/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/FingerprintAuthentication/.idea/compiler.xml b/FingerprintAuthentication/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/FingerprintAuthentication/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FingerprintAuthentication/.idea/copyright/profiles_settings.xml b/FingerprintAuthentication/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..c7d1c5a --- /dev/null +++ b/FingerprintAuthentication/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/FingerprintAuthentication/.idea/gradle.xml b/FingerprintAuthentication/.idea/gradle.xml new file mode 100644 index 0000000..7ac24c7 --- /dev/null +++ b/FingerprintAuthentication/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/FingerprintAuthentication/.idea/misc.xml b/FingerprintAuthentication/.idea/misc.xml new file mode 100644 index 0000000..5d19981 --- /dev/null +++ b/FingerprintAuthentication/.idea/misc.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FingerprintAuthentication/.idea/modules.xml b/FingerprintAuthentication/.idea/modules.xml new file mode 100644 index 0000000..5e30b25 --- /dev/null +++ b/FingerprintAuthentication/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/FingerprintAuthentication/app/.gitignore b/FingerprintAuthentication/app/.gitignore new file mode 100644 index 0000000..3543521 --- /dev/null +++ b/FingerprintAuthentication/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/FingerprintAuthentication/app/build.gradle b/FingerprintAuthentication/app/build.gradle new file mode 100644 index 0000000..74b0670 --- /dev/null +++ b/FingerprintAuthentication/app/build.gradle @@ -0,0 +1,30 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 25 + buildToolsVersion "26.0.2" + defaultConfig { + applicationId "com.example.dhruv.fingerprintauthentication" + minSdkVersion 23 + targetSdkVersion 25 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:25.3.1' + compile 'com.android.support.constraint:constraint-layout:1.0.2' + testCompile 'junit:junit:4.12' +} diff --git a/FingerprintAuthentication/app/proguard-rules.pro b/FingerprintAuthentication/app/proguard-rules.pro new file mode 100644 index 0000000..775e288 --- /dev/null +++ b/FingerprintAuthentication/app/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\Users\Dhruv\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/FingerprintAuthentication/app/src/androidTest/java/com/example/dhruv/fingerprintauthentication/ExampleInstrumentedTest.java b/FingerprintAuthentication/app/src/androidTest/java/com/example/dhruv/fingerprintauthentication/ExampleInstrumentedTest.java new file mode 100644 index 0000000..e78e68f --- /dev/null +++ b/FingerprintAuthentication/app/src/androidTest/java/com/example/dhruv/fingerprintauthentication/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.dhruv.fingerprintauthentication; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.example.dhruv.fingerprintauthentication", appContext.getPackageName()); + } +} diff --git a/FingerprintAuthentication/app/src/main/AndroidManifest.xml b/FingerprintAuthentication/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..0f3e2e8 --- /dev/null +++ b/FingerprintAuthentication/app/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FingerprintAuthentication/app/src/main/java/com/example/dhruv/fingerprintauthentication/FingerprintHandler.java b/FingerprintAuthentication/app/src/main/java/com/example/dhruv/fingerprintauthentication/FingerprintHandler.java new file mode 100644 index 0000000..a00d3a5 --- /dev/null +++ b/FingerprintAuthentication/app/src/main/java/com/example/dhruv/fingerprintauthentication/FingerprintHandler.java @@ -0,0 +1,53 @@ +package com.example.dhruv.fingerprintauthentication; + +import android.hardware.fingerprint.FingerprintManager; +import android.os.Build; +import android.os.CancellationSignal; +import android.support.annotation.RequiresApi; +import android.widget.TextView; + + +@RequiresApi(api = Build.VERSION_CODES.M) +public class FingerprintHandler extends FingerprintManager.AuthenticationCallback { + + private TextView tv; + + + public FingerprintHandler(TextView tv) { + this.tv = tv; + } + + public void doAuth(FingerprintManager manager, FingerprintManager.CryptoObject obj) { + CancellationSignal signal = new CancellationSignal(); + + try { + manager.authenticate(obj, signal, 0, this, null); + } + catch(SecurityException sce) {} + } + + @Override + public void onAuthenticationError(int errorCode, CharSequence errString) { + super.onAuthenticationError(errorCode, errString); + tv.setText("Auth Error"); + } + + @Override + public void onAuthenticationHelp(int helpCode, CharSequence helpString) { + super.onAuthenticationHelp(helpCode, helpString); + + } + + @Override + public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { + super.onAuthenticationSucceeded(result); + tv.setText("Auth Success"); + } + + @Override + public void onAuthenticationFailed() { + super.onAuthenticationFailed(); + } + + +} \ No newline at end of file diff --git a/FingerprintAuthentication/app/src/main/java/com/example/dhruv/fingerprintauthentication/MainActivity.java b/FingerprintAuthentication/app/src/main/java/com/example/dhruv/fingerprintauthentication/MainActivity.java new file mode 100644 index 0000000..fc2c89f --- /dev/null +++ b/FingerprintAuthentication/app/src/main/java/com/example/dhruv/fingerprintauthentication/MainActivity.java @@ -0,0 +1,174 @@ +package com.example.dhruv.fingerprintauthentication; + +import android.app.KeyguardManager; +import android.hardware.fingerprint.FingerprintManager; +import android.security.keystore.KeyGenParameterSpec; +import android.security.keystore.KeyGenParameterSpec.Builder; +import android.security.keystore.KeyProperties; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; + +public class MainActivity extends AppCompatActivity { + + private TextView message; + private static final String KEY_NAME = "SwA"; + + private KeyStore keyStore; + private KeyGenerator keyGenerator; + private FingerprintManager.CryptoObject cryptoObject; + + private FingerprintManager fingerprintManager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + message = (TextView) findViewById(R.id.fingerStatus); + Button btn = (Button) findViewById(R.id.authBtn); + + final FingerprintHandler fph = new FingerprintHandler(message); + + if (!checkFinger()) { + btn.setEnabled(false); + } + else { + // We are ready to set up the cipher and the key + try { + generateKey(); + Cipher cipher = generateCipher(); + cryptoObject = + new FingerprintManager.CryptoObject(cipher); + + } + catch(FingerprintException fpe) { + // Handle exception + btn.setEnabled(false); + } + } + + btn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + message.setText("Swipe your finger"); + fph.doAuth(fingerprintManager, cryptoObject); + } + }); + } + + + private boolean checkFinger() { + + // Keyguard Manager + KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); + + // Fingerprint Manager + fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE); + + try { + // Check if the fingerprint sensor is present + if (!fingerprintManager.isHardwareDetected()) { + message.setText("Fingerprint authentication not supported"); + return false; + } + + if (!fingerprintManager.hasEnrolledFingerprints()) { + message.setText("No fingerprint configured."); + return false; + } + + if (!keyguardManager.isKeyguardSecure()) { + message.setText("Secure lock screen not enabled"); + return false; + } + + } + catch(SecurityException se) { + se.printStackTrace(); + } + + + return true; + + } + + private void generateKey() throws FingerprintException { + try { + // Get the reference to the key store + keyStore = KeyStore.getInstance("AndroidKeyStore"); + + // Key generator to generate the key + 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 exc) { + exc.printStackTrace(); + throw new FingerprintException(exc); + } + + + } + + private Cipher generateCipher() throws FingerprintException { + try { + Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + + KeyProperties.BLOCK_MODE_CBC + "/" + + KeyProperties.ENCRYPTION_PADDING_PKCS7); + SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, + null); + cipher.init(Cipher.ENCRYPT_MODE, key); + return cipher; + } + catch (NoSuchAlgorithmException + | NoSuchPaddingException + | InvalidKeyException + | UnrecoverableKeyException + | KeyStoreException exc) { + exc.printStackTrace(); + throw new FingerprintException(exc); + } + } + + private class FingerprintException extends Exception { + + public FingerprintException(Exception e) { + super(e); + } + } +} \ No newline at end of file diff --git a/FingerprintAuthentication/app/src/main/res/layout/activity_main.xml b/FingerprintAuthentication/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..4992756 --- /dev/null +++ b/FingerprintAuthentication/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,34 @@ + + + + + + + + +