2
私は自分のアプリにパスワードと指紋認証を作成したいと思っています。私はパスワードの部分を行う方法を知っているが、私の質問は指紋認証を作成する方法ですか?アンドロイドで指紋認証を作成する方法は?
ので、私はandroidHiveでチュートリアルを経てこのコードを書かれている:
- 私は指紋の後にテントを設定するにはどうすればよい:今
public class LogUpInActivity extends Activity { // sign up statue private SharedPreferences StatuSharePreference; private SharedPreferences.Editor editor_state; public boolean state_signUp, default_state, state_check; private Stage mStage; // sign up with password private View signUpContent; private EditText signUp_pasword_editText, signUp_conf_password_editText, signIn_password_editText; public static SharedPreferences save_password_sharePreference; public static SharedPreferences.Editor editor_password; public static String logIn_password = null; // sign in with password public View signIn_passwordContent; public int times_of_signIn = 5; // sign in with finger print private View signIn_fingerPrintContent; private TextView msg_fingerPrint; private KeyStore keyStore; private Cipher cipher; private static final String KEY_NAME = "mohrOmum"; // sign in after unlocking public boolean sign_in_again; private Button cancelBTN, stateBTN; private TextView msg_state_conf; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_container); TextView who = (TextView) findViewById(R.id.who); who.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/mehr.ttf")); TextView signInPassTitle = (TextView) findViewById(R.id.password_description); signInPassTitle.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/nazanin.ttf")); TextView signUpPassTitle = (TextView) findViewById(R.id.signUp_title); signUpPassTitle.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/nazanin.ttf")); StatuSharePreference = getSharedPreferences("signUp", Context.MODE_PRIVATE); editor_state = StatuSharePreference.edit(); signUpContent = findViewById(R.id.signup_content); signUp_pasword_editText = (EditText) findViewById(R.id.pass); signUp_conf_password_editText = (EditText) findViewById(R.id.conf_pass); signIn_password_editText = (EditText) findViewById(R.id.password); msg_state_conf = (TextView) findViewById(R.id.notConfiration_msg); save_password_sharePreference = getSharedPreferences("password", Context.MODE_PRIVATE); editor_password = save_password_sharePreference.edit(); signIn_passwordContent = findViewById(R.id.signIn_password_container); signIn_fingerPrintContent = findViewById(R.id.fingerprint_container); msg_fingerPrint = (TextView) findViewById(R.id.fingerprint_description); // Initializing both Android Keyguard Manager and Fingerprint Manager KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE); // check if sign up is done or not if (!default_state) { state_signUp = false; default_state = true; } else { state_signUp = true; } state_check = StatuSharePreference.getBoolean("signUp", state_signUp); if (!state_check) { // register user password signIn_passwordContent.setVisibility(View.GONE); signUpContent.setVisibility(View.VISIBLE); mStage = Stage.SIGNUP; } else { signUpContent.setVisibility(View.GONE); signIn_passwordContent.setVisibility(View.VISIBLE); // Check whether the device has a Fingerprint sensor. if (!fingerprintManager.isHardwareDetected()) { signIn_fingerPrintContent.setVisibility(View.GONE); } else { signIn_fingerPrintContent.setVisibility(View.VISIBLE); // Checks whether fingerprint permission is set on manifest if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { msg_fingerPrint.setVisibility(View.VISIBLE); msg_fingerPrint.setText(R.string.fingerPrint_not_avable); msg_fingerPrint.setTextColor(getResources().getColor(R.color.red_color)); } else { // Check whether at least one fingerprint is registered if (!fingerprintManager.hasEnrolledFingerprints()) { msg_fingerPrint.setVisibility(View.VISIBLE); msg_fingerPrint.setText(R.string.fingerPrint_not_register); msg_fingerPrint.setTextColor(getResources().getColor(R.color.red_color)); } else { // Checks whether lock screen security is enabled or not if (!keyguardManager.isKeyguardSecure()) { msg_fingerPrint.setVisibility(View.VISIBLE); msg_fingerPrint.setText(R.string.fingerPrint_not_register); msg_fingerPrint.setTextColor(getResources().getColor(R.color.red_color)); } else { generateKey(); if (cipherInit()) { FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher); FingerprintHandler helper = new FingerprintHandler(this); helper.startAuth(fingerprintManager, cryptoObject); } } } } } mStage = Stage.SIGNIN_PASSWORD; } cancelBTN = (Button) findViewById(R.id.cancel_button); cancelBTN.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); logIn_password = save_password_sharePreference.getString("password", logIn_password); stateBTN = (Button) findViewById(R.id.second_dialog_button); stateBTN.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.M) @Override public void onClick(View view) { switch (mStage) { case SIGNUP: //get user password and register it registerPassword(); break; /*case SIGNIN_FINGERPRINT:// open sign in with password signInWithPassword(); break;*/ case SIGNIN_PASSWORD://checking passsword entry verifyPassword(); break; } } }); } // on create /** * get user password from Edit text , check its lengh , confirm it **/ public void registerPassword() { String passString = signUp_pasword_editText.getText().toString(); String confPassString = signUp_conf_password_editText.getText().toString(); if ((passString.equals(confPassString)) && (passString.length() == 12) && (confPassString.length() == 12)) { logIn_password = passString; editor_password.putString("password", logIn_password).commit(); Toast.makeText(this, logIn_password, Toast.LENGTH_LONG).show(); msg_state_conf.setVisibility(View.VISIBLE); msg_state_conf.setText(R.string.msg_conf); msg_state_conf.setTextColor(getResources().getColor(R.color.green_color)); editor_state.putBoolean("signUp", true).commit(); //state_signUp = true; //mina! ***checking fingerPrint sensor*** /* + : finger primt authorization ** - : sign in with password */ mStage = Stage.SIGNIN_PASSWORD; signUpContent.setVisibility(View.GONE); signIn_passwordContent.setVisibility(View.VISIBLE); } else { msg_state_conf.setVisibility(View.VISIBLE); msg_state_conf.setText(R.string.msg_not_conf); msg_state_conf.setTextColor(getResources().getColor(R.color.red_color)); //not confirm editor_state.putBoolean("signUp", false).commit(); } } /** * Checks whether the current entered password is correct, and dismisses the the dialog and * let's the activity know about the result. */ private void verifyPassword() { logIn_password = save_password_sharePreference.getString("signUp", logIn_password); Toast.makeText(this, logIn_password, Toast.LENGTH_LONG).show(); if (signIn_password_editText.getText().toString().equals(logIn_password)) { startActivity(new Intent(LogUpInActivity.this, MainActivity.class)); LogUpInActivity.this.finish(); } else { --times_of_signIn; Toast.makeText(this, times_of_signIn + "تعداد ورود باقیمانده:", Toast.LENGTH_LONG).show(); signIn_password_editText.setText(null); if (times_of_signIn == 0) { Toast.makeText(this, R.string.restart_app, Toast.LENGTH_LONG).show(); MainActivity.sqlDB.delete(DataBaseClass.BANK_TABLE_NAME, null, null); logIn_password = null; editor_password.putString("password", logIn_password).commit(); state_signUp = false; editor_state.putBoolean("signUp", state_signUp).commit(); signIn_passwordContent.setVisibility(View.GONE); signUpContent.setVisibility(View.VISIBLE); mStage = Stage.SIGNUP; } } } /* sign in with finger print */ @TargetApi(Build.VERSION_CODES.M) protected void generateKey() { try { keyStore = KeyStore.getInstance("AndroidKeyStore"); } catch (Exception e) { e.printStackTrace(); } KeyGenerator keyGenerator; try { keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); //startActivity(new Intent(LogUpInActivity.this,MainActivity.class)); } catch (NoSuchAlgorithmException | NoSuchProviderException e) { throw new RuntimeException("Failed to get KeyGenerator instance", e); } try { 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 (NoSuchAlgorithmException | InvalidAlgorithmParameterException | IOException e) { throw new RuntimeException(e); } catch (java.security.cert.CertificateException e) { e.printStackTrace(); } } @TargetApi(Build.VERSION_CODES.M) public boolean cipherInit() { 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 (IOException e) { e.printStackTrace(); } catch (java.security.cert.CertificateException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } return true; } public void onFinish(){ editor_password.putString("password",logIn_password).commit(); } /** * Enumeration to indicate which authentication method the user is trying to authenticate with. */ public enum Stage { SIGNUP, SIGNIN_FINGERPRINT, SIGNIN_PASSWORD }
すべてこの後、私は次の質問を持っています承認?
- どのように私の時間が制限された指紋認証のための(
verifyPassword()
法など)
を案内してくださいすることができます。