指纹登录的Android和flutter实现
# 11001.指纹登录的Android和flutter实现
# 产品交互逻辑
此处实现的是本地加密保存账号密码从而实现的一键登录功能.暂不讨论需要后台参与的验签类实现.
如果之前的是手机+验证码登录,则这种纯本地方式无法实现指纹登录功能.
# 设置页面
有打开关闭指纹登录的开关.
默认关闭
点击打开后,无需再次验证用户名密码,下次进入即可使用指纹自动登录
(实现应该是: 之前就使用对称加密保存了用户名密码到本地,使用指纹登录的话,不过是把加密保存的方式改成需要生物验证的非对称加密)
# 登录页面
如果打开了指纹登录,且之前有保存登录数据,那么弹出指纹验证框,验证ok后,自动调用登录逻辑
如果打开了指纹登录,之前没有保存登录数据,那么在本次输入,登录成功后,自动静默加密保存登录账号密码信息到本地.
# flutter:
https://pub.dev/packages/biometric_storage
biometric_storage: ^5.0.0+4
1
这个库不行,使用的是对称加密,不仅取密码需要验证指纹,存密码也需要验证,无法满足需要
# Usage
You basically only need 4 methods.
- Check whether biometric authentication is supported by the device
final response = await BiometricStorage().canAuthenticate()
if (response != CanAuthenticateResponse.success) {
// panic..
}
1
2
3
4
2
3
4
- Create the access object
final store = BiometricStorage().getStorage('mystorage');
1
- Read data
final data = await storageFile.read();
1
- Write data
final myNewData = 'Hello World';
await storageFile.write(myNewData);
1
2
2
# Android:
https://github.com/hss01248/utilcodeEnhance/blob/master/biometric/src/main/java/com/hss01248/biometric/BiometricHelper.java
api "com.github.hss01248.utilcodeEnhance:biometric:1.4.2"
api "com.github.hss01248.utilcodeEnhance:cipher:1.4.2"
1
2
3
2
3
使用ras公钥加密保存密码: 无需验证指纹:
byte[] encryptedData = RsaCipherUtil.encryptByPublicKeyWithUserVerify("bio-rsaCipher", "123456".getBytes());
1
//后续encryptedData可base64后,存到sharedprefences中.
账号可明文或对称加密存到sharedprefences中.
使用rsa私钥解析密码:
RsaCipherUtil.decryptByPrivateKeyWithUserVerify("bio-rsaCipher", encryptedData, false,
new MyCommonCallback3<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
LogUtils.i("bio-rsaCipher-解密后: "+new String(bytes));
}
});
1
2
3
4
5
6
7
2
3
4
5
6
7
对称加密保存到sp,可直接使用Android官方的EncryptedSharedPreferences
封装好的方法为: EnSpUtil.xxx
编辑 (opens new window)
上次更新: 2024/01/25, 11:21:52