Ukey PIN Authentication (ArkTS)
From API version 22, huksExternalCrypto provides the PIN authentication API. An ecosystem app calls the certificate HAP UI to display a certificate list. After a user selects a certificate, the browser obtains resourceId based on the selected certificate, and then open the resource for PIN authentication. For details about the scenarios, see Ukey PIN Authentication Overview and Specifications.
How to Develop
-
Construct parameters, among which HUKS_EXT_CRYPTO_TAG_UID and HUKS_EXT_CRYPTO_TAG_UKEY_PIN are mandatory.
-
Call authUkeyPin to verify the PIN.
Development Cases
import { BusinessError } from '@kit.BasicServicesKit';
import { huksExternalCrypto } from '@kit.UniversalKeystoreKit';
function StringToUint8Array(str: string) {
let arr: number[] = [];
for (let i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i));
}
return new Uint8Array(arr);
}
// The UID is obtained by the caller.
let uid: number = 3511;
async function authUkeyPin(): Promise<void> {
try {
/* 1. Assume that the opened resources are as follows: */
const testResourceId = JSON.stringify({
providerName: "testProviderName",
bundleName: "com.example.cryptoapplication",
abilityName: "CryptoExtension",
index: {
key: "testKey"
} as ESObject
});
/* 2. Construct parameters. */
const pin = "123456";
const extProperties: Array<huksExternalCrypto.HuksExternalCryptoParam> = [
{
tag: huksExternalCrypto.HuksExternalCryptoTag.HUKS_EXT_CRYPTO_TAG_UID,
value: uid
}, {
tag: huksExternalCrypto.HuksExternalCryptoTag.HUKS_EXT_CRYPTO_TAG_UKEY_PIN,
value: StringToUint8Array(pin)
}
];
/* 3. Verify the PIN. */
await huksExternalCrypto.authUkeyPin(testResourceId, extProperties)
.then(() => {
console.info(`promise: getUkeyPinAuthState success`);
}).catch((error: BusinessError) => {
console.error(`promise: getUkeyPinAuthState failed, errCode : ${error.code}, errMsg : ${error.message}`);
});
} catch (error) {
console.error(`promise: getUkeyPinAuthState input arg invalid`);
}
}
async function TestAuthUkeyPin() {
await authUkeyPin();
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 huks-key-import-overview
openharmony 鸿蒙 huks-signing-signature-verification-arkts
openharmony 鸿蒙 huks-hmac-arkts
openharmony 鸿蒙 huks-key-agreement-overview
openharmony 鸿蒙 huks-as-user-sys
openharmony 鸿蒙 huks-delete-key-ndk