· 7 years ago · May 10, 2018, 11:22 PM
1@RequiresApi(Build.VERSION_CODES.M)
2class FingerprintHelper(var context: Context, var fingerInterface: FingerInterface?) : FingerprintManager.AuthenticationCallback() {
3 private var keyguardManager: KeyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
4 private var fingerManager: FingerprintManager = context.getSystemService(Context.FINGERPRINT_SERVICE) as FingerprintManager
5 private var cancellationSignal: CancellationSignal = CancellationSignal()
6
7 private var keyStore: KeyStore? = null
8 private var keyGenerator: KeyGenerator? = null
9 private var cipher: Cipher? = null
10 private var cryptoObject: FingerprintManager.CryptoObject? = null
11
12 private val KEY_NAME = "yourKey"
13
14 init {
15 generateKeys()
16
17 initCipher()
18
19 cryptoObject = FingerprintManager.CryptoObject(cipher)
20 }
21
22 fun startAuth() {
23 if (grantedPermission)
24 when {
25 !fingerManager.isHardwareDetected ->
26 fingerInterface?.failed(context.getString(R.string.finger_print_hint), context.getString(R.string.finger_print_not_found))
27
28 !fingerManager.hasEnrolledFingerprints() ->
29 fingerInterface?.failed(context.getString(R.string.finger_print_hint), context.getString(R.string.finger_print_not_configured))
30
31 !keyguardManager.isKeyguardSecure ->
32 fingerInterface?.failed(context.getString(R.string.finger_print_hint), context.getString(R.string.finger_print_not_available_without_keyguard))
33
34 else ->
35 fingerManager.authenticate(cryptoObject, cancellationSignal, 0, this, null)
36 }
37 else
38 fingerInterface?.failed(context.getString(R.string.finger_print_hint), context.getString(R.string.finger_print_has_not_permission))
39 }
40
41
42 fun cancelAuth() {
43 cancellationSignal.cancel()
44 }
45
46
47 private val grantedPermission: Boolean =
48 ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED
49
50 private fun generateKeys() {
51 try {
52 keyStore = KeyStore.getInstance("AndroidKeyStore")
53
54 keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
55
56 keyGenerator?.init(KeyGenParameterSpec.Builder(KEY_NAME,
57 KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
58 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
59 .setUserAuthenticationRequired(true)
60 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
61 .build())
62 keyGenerator?.generateKey()
63
64 } catch (exc: Exception) {
65 exc.printStackTrace()
66 }
67
68
69 }
70
71 private fun initCipher() {
72 try {
73 cipher = Cipher.getInstance(
74 KeyProperties.KEY_ALGORITHM_AES + "/"
75 + KeyProperties.BLOCK_MODE_CBC + "/"
76 + KeyProperties.ENCRYPTION_PADDING_PKCS7)
77 keyStore?.load(null)
78 val key = keyStore?.getKey(KEY_NAME, null) as SecretKey
79 cipher?.init(Cipher.ENCRYPT_MODE, key)
80 } catch (e: Exception) {
81 throw RuntimeException(e)
82 }
83 }
84
85
86 override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
87 super.onAuthenticationError(errorCode, errString)
88 Timber.i("error in fingerAuth code:$errorCode message:$errString")
89 fingerInterface?.failed(context.getString(R.string.finger_print_error), parseError(errString), errorCode)
90 }
91
92 override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult) {
93 super.onAuthenticationSucceeded(result)
94 Timber.i("success in fingerAuth result:$result")
95 fingerInterface?.isSuccessful("MyKey")
96 }
97
98 override fun onAuthenticationHelp(helpCode: Int, helpString: CharSequence?) {
99 super.onAuthenticationHelp(helpCode, helpString)
100 Timber.i("help in fingerAuth code:$helpCode message:$helpString")
101 fingerInterface?.failed(context.getString(R.string.finger_print_help), context.getString(R.string.take_your_finger_on_place), errorCode = helpCode)
102 }
103
104 override fun onAuthenticationFailed() {
105 super.onAuthenticationFailed()
106 Timber.i("failed in fingerAuth")
107 fingerInterface?.failed(context.getString(R.string.finger_print_failed), context.getString(R.string.finger_print_detection_failed))
108 }
109}