HarmonyOS-鸿蒙app开发 —基于java安全生物特征识别开发指导

HarmonyOS-鸿蒙app开发 —基于java安全生物特征识别开发指导

场景介绍

当前生物特征识别支持2D人脸识别、3D人脸识别,可应用于设备解锁、应用登录、支付等身份认证场景。

接口说明

BiometricAuthentication类提供了生物认证的相关方法,包括检测认证能力、认证和取消认证等,用户可以通过人脸等生物特征信息进行认证操作。在执行认证前,需要检查设备是否支持该认证能力,具体指认证类型、安全级别和是否本地认证。如果不支持,需要考虑使用其他认证能力。

表1 生物特征识别开放能力列表:

接口名

功能描述

getInstance(Ability ability)

获取BiometricAuthentication的单例对象。

checkAuthenticationAvailability(AuthType type, SecureLevel level, boolean isLocalAuth)

检测设备是否具有生物认证能力。

execAuthenticationAction(AuthType type, SecureLevel level, boolean isLocalAuth,boolean isAppAuthDialog, SystemAuthDialogInfo information)

调用者使用该方法进行生物认证。

getAuthenticationTips()

获取生物认证过程中的提示信息。

cancelAuthenticationAction()

取消生物认证操作。

setSecureObjectSignature(Signature sign)

设置需要关联认证结果的Signature对象,在进行认证操作后,如果认证成功则Signature对象被授权可以使用。设置前Signature对象需要正确初始化,且配置为认证成功才能使用。

getSecureObjectSignature()

在认证成功后,可通过该方法获取已授权的Signature对象。如果未设置过Signature对象,则返回null。

setSecureObjectCipher(Cipher cipher)

设置需要关联认证结果的Cipher对象,在进行认证操作后,如果认证成功则Cipher对象被授权可以使用。设置前Cipher对象需要正确初始化,且配置为认证成功才能使用。

getSecureObjectCipher()

在认证成功后,可通过该方法获取已授权的Cipher对象。如果未设置过Cipher对象,则返回null。

setSecureObjectMac(Mac mac)

设置需要关联认证结果的Mac对象,在进行认证操作后,如果认证成功则Mac对象被授权可以使用。设置前Mac对象需要正确初始化,且配置为认证成功才能使用。

getSecureObjectMac()

在认证成功后,可通过该方法获取已授权的Mac对象。如果未设置过Mac对象,则返回null。

开发步骤

开发前请完成以下准备工作:

  1. 在应用配置权限文件中,增加ohos.permission.ACCESS_BIOMETRIC的权限声明。
  2. 在使用生物特征识别认证能力的代码文件中增加import ohos.biometrics.authentication.BiometricAuthentication。

开发过程:

  1. 获取BiometricAuthentication的单例对象,代码示例如下:

BiometricAuthentication biometricAuthentication = BiometricAuthentication.getInstance(MainAbility.mAbility);

检测设备是否具有生物认证能力:

2D人脸识别建议使用SECURE_LEVEL_S2,3D人脸识别建议使用SECURE_LEVEL_S3。代码示例如下:

int retChkAuthAvb = biometricAuthentication.checkAuthenticationAvailability(
BiometricAuthentication.AuthType.AUTH_TYPE_BIOMETRIC_FACE_ONLY,
BiometricAuthentication.SecureLevel.SECURE_LEVEL_S2, true);

(可选)设置需要关联认证结果的Signature对象或Cipher对象或Mac对象,代码示例如下:

// 定义一个Signature对象sign;
biometricAuthentication.setSecureObjectSignature(sign);

// 定义一个Cipher对象cipher;
biometricAuthentication.setSecureObjectCipher(cipher);

// 定义一个Mac对象mac;
biometricAuthentication.setSecureObjectMac(mac);

在新线程里面执行认证操作,避免阻塞其他操作,代码示例如下:

new Thread(new Runnable() {
@Override
public void run() {
int retExcAuthretExcAuth = biometricAuthentication.execAuthenticationAction(
BiometricAuthentication.AuthType.AUTH_TYPE_BIOMETRIC_FACE_ONLY,
BiometricAuthentication.SecureLevel.SECURE_LEVEL_S2, true, false, null);
}
}).start();

获得认证过程中的提示信息,代码示例如下:

AuthenticationTips tips = biometricAuthentication.getAuthenticationTips();

(可选)认证成功后获取已设置的Signature对象或Cipher对象或Mac对象,代码示例如下:

Signature sign = biometricAuthentication.getSecureObjectSignature();

Cipher cipher = biometricAuthentication.getSecureObjectCipher();

Mac mac = biometricAuthentication.getSecureObjectMac();

认证过程中取消认证,代码示例如下:

int ret = biometricAuthentication.cancelAuthenticationAction();

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x