关闭数字盾服务
场景介绍
当用户不再使用数字盾时,可以通过密码认证主动发起关闭数字盾的操作;若用户忘记密码或连续密码认证失败次数达到最大限制导致数字盾密码锁定,盾应用将在重新激活数字盾时,无需进行密码认证直接关闭最初激活的数字盾,并通过设置数字盾密码重新创建新的数字盾密码。
约束与限制
本功能目前仅在手机设备支持。
业务流程

当不需要密码认证进行关闭数字盾申请时,则无需和Universal Keystore Kit交互,使用随机生成的challenge完成关闭数字盾操作。
接口说明
接口及使用方法请参见API参考。
| 接口名 | 描述 |
|---|---|
| disableTrustedAuthentication(challenge: Uint8Array, needAuth: boolean, authID: bigint, label: TUILable): Promise<AuthToken> | 关闭数字盾服务 |
关闭数字盾服务界面介绍
如图为需要进行密码认证的方式关闭数字盾服务时对应的TUI界面示例。

开发步骤
密码认证方式关闭数字盾服务
-
导入huks 、trustedAuthentication 和相关依赖模块。
import { resourceManager } from '@kit.LocalizationKit'import { huks } from '@kit.UniversalKeystoreKit';import { BusinessError } from '@kit.BasicServicesKit';import { trustedAuthentication } from '@kit.DeviceSecurityKit';import { cryptoFramework } from '@kit.CryptoArchitectureKit';import { hilog } from '@kit.PerformanceAnalysisKit';import { common } from '@kit.AbilityKit'; -
关闭数字盾前,需从服务器获取当前账号在设置数字盾密码时获取的authID。
-
参考密钥管理服务提供的签名/验签指导,初始化签名会话。
-
调用关闭数字盾服务接口,发起数字盾服务关闭申请。
// 关闭数字盾服务async function DisablePwd(challenge: Uint8Array, context: common.UIAbilityContext):Promise<trustedAuthentication.AuthToken> {try {const authID: bigint = 1687413472599354502n;//实际填充为从服务器获取到的账号对应的authID值const resourceMgr: resourceManager.ResourceManager = context.resourceManager;const fileData : Uint8Array = await resourceMgr.getRawFileContent('test_logo_rgba.png'); //实际使用时请替换为应用要在TUI界面展示的logo图片名称const buffer = fileData.buffer;const label:trustedAuthentication.TUILable = {image: buffer as ArrayBuffer,title: "关闭数字盾",}const authToken = await trustedAuthentication.disableTrustedAuthentication(challenge, true, authID, label);return authToken;} catch (err) {hilog.error(0x0000, 'testTag', `Failed to disableTrustedAuthentication, code:${err.code}, message:${err.message}`);throw new Error('Close trusted authentication failed:' + (err as BusinessError).message);}}const rand = cryptoFramework.createRandom();const len: number = 32;const challenge: Uint8Array = rand?.generateRandomSync(len)?.data;//实际使用时请替换为通过UniversalKeystoreKit初始化会话获取的challengelet context = this.getUIContext().getHostContext() as common.UIAbilityContext;const authToken: trustedAuthentication.AuthToken = await DisablePwd(challenge, context); -
参考密钥管理服务提供的签名/验签指导, 对通过关闭数字盾获取到的authToken数据进行签名,并结束会话。
无需密码认证方式关闭数字盾服务
使用cryptoFramework生成的32字节随机数作为challenge,直接调用关闭数字盾服务接口即可,生成的authToken信息未经过密码认证,不可进行签名校验。
// 关闭数字盾服务
async function DisablePwd(challenge: Uint8Array):Promise<trustedAuthentication.AuthToken> {
try {
const authID: bigint = 1687413472599354502n;//实际填充为从服务器获取到的账号对应的authID值
let emptyBuffer = new ArrayBuffer(0);
const label:trustedAuthentication.TUILable = {
image: emptyBuffer,
title: "",
}
const authToken = await trustedAuthentication.disableTrustedAuthentication(challenge, false, authID, label);
return authToken;
} catch (err) {
hilog.error(0x0000, 'testTag', `Failed to disableTrustedAuthentication, code:${err.code}, message:${err.message}`);
throw new Error('Close trusted authentication failed:' + (err as BusinessError).message);
}
}
const rand = cryptoFramework.createRandom();
const len: number = 32;
const challenge: Uint8Array = rand?.generateRandomSync(len)?.data;//此处使用的challenge为通过cryptoFramework生成的32字节随机数即可
const authToken: trustedAuthentication.AuthToken = await DisablePwd(challenge);