1# 查询密钥是否存在(C/C++)
2
3
4HUKS提供了接口供应用查询指定密钥是否存在。
5
6## 在CMake脚本中链接相关动态库
7```txt
8   target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
9```
10
11## 开发步骤
12
131. 构造对应参数。
14   - 指定密钥别名keyAlias,密钥别名最大长度为128字节。
15   - 查询密钥需要的[属性TAG](../../reference/apis-universal-keystore-kit/_huks_type_api.md#oh_huks_tag)(默认传空)。
16
172. 调用接口[OH_Huks_IsKeyItemExist](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_iskeyitemexist),查询密钥是否存在。
18
19```c++
20#include "huks/native_huks_api.h"
21#include "huks/native_huks_param.h"
22#include <string.h>
23static napi_value IsKeyExist(napi_env env, napi_callback_info info)
24{
25    /* 1.获取密钥别名 */
26    struct OH_Huks_Blob keyAlias = {
27        (uint32_t)strlen("test_key"),
28        (uint8_t *)"test_key"
29    };
30
31    /* 2.调用OH_Huks_IsKeyItemExist判断密钥是否存在  */
32    struct OH_Huks_Result ohResult = OH_Huks_IsKeyItemExist(&keyAlias, nullptr);
33    // OH_HUKS_SUCCESS表示存在, OH_HUKS_ERR_CODE_ITEM_NOT_EXIST表示不存在
34
35    napi_value ret;
36    napi_create_int32(env, ohResult.errorCode, &ret);
37    return ret;
38}
39```
40