• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 查询密钥是否存在(C/C++)
2
3
4HUKS提供了接口供应用查询指定密钥是否存在。
5
6
7## 开发步骤
8
91. 构造对应参数。
10   - 指定密钥别名keyAlias,密钥别名最大长度为64字节。
11   - 查询密钥需要的属性TAG(默认传空)。
12
132. 调用接口[OH_Huks_IsKeyItemExist](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_iskeyitemexist),查询密钥是否存在。
14
15```c++
16#include "huks/native_huks_api.h"
17#include "huks/native_huks_param.h"
18#include <string.h>
19static napi_value IsKeyExist(napi_env env, napi_callback_info info)
20{
21    /* 1.获取密钥别名 */
22    struct OH_Huks_Blob keyAlias = {
23        (uint32_t)strlen("test_key"),
24        (uint8_t *)"test_key"
25    };
26
27    /* 2.调用OH_Huks_IsKeyItemExist判断密钥是否存在  */
28    struct OH_Huks_Result ohResult = OH_Huks_IsKeyItemExist(&keyAlias, nullptr);
29    // OH_HUKS_SUCCESS表示存在, OH_HUKS_ERR_CODE_ITEM_NOT_EXIST表示不存在
30
31    napi_value ret;
32    napi_create_int32(env, ohResult.errorCode, &ret);
33    return ret;
34}
35```
36