• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 查询密钥是否存在(C/C++)
2
3<!--Kit: Universal Keystore Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @wutiantian-gitee-->
6<!--Designer: @HighLowWorld-->
7<!--Tester: @wxy1234564846-->
8<!--Adviser: @zengyawen-->
9
10HUKS提供了接口供应用查询指定密钥是否存在。
11
12## 在CMake脚本中链接相关动态库
13```txt
14target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
15```
16## 开发步骤
17
181. 指定密钥别名,密钥别名命名规范参考[密钥生成介绍及算法规格](huks-key-generation-overview.md)。
19
202. 初始化密钥属性集。用于查询时指定[密钥的属性TAG](../../reference/apis-universal-keystore-kit/capi-native-huks-type-h.md#oh_huks_tag)(默认传空)。
21
223. 调用接口[OH_Huks_IsKeyItemExist](../../reference/apis-universal-keystore-kit/capi-native-huks-api-h.md#oh_huks_iskeyitemexist),查询密钥是否存在。
23
24```c++
25#include "huks/native_huks_api.h"
26#include "huks/native_huks_param.h"
27#include "napi/native_api.h"
28#include <cstring>
29
30/* 以下以生成ECC密钥为例 */
31OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params,
32                            uint32_t paramCount)
33{
34    OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
35    if (ret.errorCode != OH_HUKS_SUCCESS) {
36        return ret;
37    }
38    ret = OH_Huks_AddParams(*paramSet, params, paramCount);
39    if (ret.errorCode != OH_HUKS_SUCCESS) {
40        OH_Huks_FreeParamSet(paramSet);
41        return ret;
42    }
43    ret = OH_Huks_BuildParamSet(paramSet);
44    if (ret.errorCode != OH_HUKS_SUCCESS) {
45        OH_Huks_FreeParamSet(paramSet);
46        return ret;
47    }
48    return ret;
49}
50
51struct OH_Huks_Param g_testGenerateKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC},
52                                                 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE},
53                                                 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256},
54                                                 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}};
55
56static OH_Huks_Result GenerateKeyHelper(const char *alias)
57{
58    struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias};
59    struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr;
60    struct OH_Huks_Result ohResult;
61    do {
62        /* 1.初始化密钥属性集 */
63        ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam,
64                                sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param));
65        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
66            break;
67        }
68        /* 2.生成密钥 */
69        ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr);
70    } while (0);
71    OH_Huks_FreeParamSet(&testGenerateKeyParamSet);
72    return ohResult;
73}
74
75static napi_value IsKeyExist(napi_env env, napi_callback_info info)
76{
77    /* 1.获取密钥别名 */
78    const char *alias = "test_key";
79    struct OH_Huks_Blob keyAlias = {
80        (uint32_t)strlen(alias),
81        (uint8_t *)alias
82    };
83
84    /* 生成密钥 */
85    OH_Huks_Result genResult = GenerateKeyHelper(alias);
86    if (genResult.errorCode != OH_HUKS_SUCCESS) {
87        napi_value ret;
88        napi_create_int32(env, genResult.errorCode, &ret);
89        return ret;
90    }
91
92    /* 2.调用OH_Huks_IsKeyItemExist判断密钥是否存在  */
93    struct OH_Huks_Result ohResult = OH_Huks_IsKeyItemExist(&keyAlias, nullptr);
94
95    napi_value ret;
96    napi_create_int32(env, ohResult.errorCode, &ret);
97    return ret;
98}
99```
100