After activating the digital shield, users can change the digital shield password after successful authentication using the old password.
In versions earlier than 6.1.1(24), this function is supported only on mobile phones. In 6.1.1(24) and later versions, this function is supported on PCs/2-in-1 devices and tablets with the TUI capability. You can call the checkConfirmUITextFormat API to check whether a device has the TUI capability. When an unsupported device calls the service APIs of the Digital Shield service, error code 1019100016 is returned.
For details about the APIs and their usage, see API Reference.
| API | Description |
|---|---|
| modifyTrustedAuthenticationPwd(challenge: Uint8Array, pwdInfo: PasswordInfo, authID: bigint, label: TUILable): Promise<AuthToken> | Changes the digital shield password. |
Import the huks module, trustedAuthentication module, and dependent modules.
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'; Before changing the password, retrieve the authID value obtained during digital shield password setup from the server for the current account.
Initialize the signature session. For details, please refer to Signing and Signature Verification of Universal Keystore Kit.
Call the password change API of the digital shield service to initiate a digital shield password change request.
async ModifyPwd(challenge: Uint8Array, assetName: string): Promise<trustedAuthentication.AuthToken> {
try {
const passwordInfo: trustedAuthentication.PasswordInfo = {
pwdType: trustedAuthentication.PasswordType.PASSWORD_TYPE_DIGITAL,
pwdMaxLength: 10,
pwdMinLength: 6,
maxAuthFailCount: 6
};
let resArray: Uint8Array = await AssetUtils.QueryDataFromAssetStore(assetName);
let credentialID: bigint = CryptoUtils.uint8ArrayToBigInt(resArray); // In actual use, replace the value with the credentialID of the account obtained from the server.
const context = AppStorage.get('context') as Context;
const buffer: ArrayBuffer = await CryptoUtils.ImportImage(); // Obtain the app logo image to be displayed on the TUI.
const label: trustedAuthentication.TUILable = {
image: buffer,
title: context.resourceManager.getStringSync($r('app.string.ModifyShield').id)
}
const authInfo =
await trustedAuthentication.modifyTrustedAuthenticationPwd(challenge, passwordInfo, credentialID, label);
hilog.info(0x0000, 'testTag', 'Modify Shield Success: ', authInfo.authToken);
return authInfo;
} catch (error) {
hilog.error(0x0000, 'testTag', 'Modify Shield Fail: ', error);
throw new Error('Modify Shield Fail: ' + (error as BusinessError).message);
}
} Sign the authToken obtained through password changing by referring to Signing and Signature Verification of Universal Keystore Kit. Then end the session.