• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 查询密钥是否存在(ArkTS)
2
3
4HUKS提供了接口供应用查询指定密钥是否存在。
5
6
7## 开发步骤
8
91. 指定密钥别名keyAlias,密钥别名最大长度为64字节。
10
112. 初始化密钥属性集。用于查询时指定密钥的属性TAG,比如查询的密钥范围(全量/单个),当查询单个时,TAG字段可传空。
12
133. 调用接口[isKeyItemExist](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksiskeyitemexist9),查询密钥是否存在。
14
15```ts
16import huks from '@ohos.security.huks';
17/* 1.确定密钥别名 */
18let keyAlias = 'test_key';
19let isKeyExist: Boolean;
20/* 2.构造空对象 */
21let huksOptions:huks.HuksOptions = {
22  properties: []
23}
24try {
25  /* 3.判断密钥是否存在 */
26  huks.isKeyItemExist(keyAlias, huksOptions, (error, data) => {
27    if (error) {
28      console.error(`callback: isKeyItemExist failed` + error);
29    } else {
30      if (data !== null && data.valueOf() !== null) {
31        isKeyExist = data.valueOf();
32        console.info(`callback: isKeyItemExist success, isKeyExist = ${isKeyExist}`);
33      }
34    }
35  });
36} catch (error) {
37  console.error(`callback: isKeyItemExist input arg invalid` + error);
38}
39```
40