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