1# Deleting a Key (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 10To ensure data security, delete the key that is no longer required. 11 12## Add the dynamic library in the CMake script. 13```txt 14target_link_libraries(entry PUBLIC libhuks_ndk.z.so) 15``` 16 17## How to Develop 18 19For example, delete a 256-bit HKDF key. 20 211. Specify the key alias. For details about the naming rules, see [Key Generation Overview and Algorithm Specifications](huks-key-generation-overview.md). 22 232. Use [OH_Huks_DeleteKeyItem](../../reference/apis-universal-keystore-kit/capi-native-huks-api-h.md#oh_huks_deletekeyitem) to delete the key. 24 25```c++ 26#include "huks/native_huks_api.h" 27#include "huks/native_huks_param.h" 28#include "napi/native_api.h" 29#include <cstring> 30 31/* Generate an ECC key. */ 32OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params, 33 uint32_t paramCount) 34{ 35 OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet); 36 if (ret.errorCode != OH_HUKS_SUCCESS) { 37 return ret; 38 } 39 ret = OH_Huks_AddParams(*paramSet, params, paramCount); 40 if (ret.errorCode != OH_HUKS_SUCCESS) { 41 OH_Huks_FreeParamSet(paramSet); 42 return ret; 43 } 44 ret = OH_Huks_BuildParamSet(paramSet); 45 if (ret.errorCode != OH_HUKS_SUCCESS) { 46 OH_Huks_FreeParamSet(paramSet); 47 return ret; 48 } 49 return ret; 50} 51 52struct OH_Huks_Param g_testGenerateKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC}, 53 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE}, 54 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256}, 55 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}}; 56 57static OH_Huks_Result GenerateKeyHelper(const char *alias) 58{ 59 struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias}; 60 struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr; 61 struct OH_Huks_Result ohResult; 62 do { 63 /* 1. Initialize the key property set. */ 64 ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam, 65 sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param)); 66 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 67 break; 68 } 69 /* 2. Generate a key. */ 70 ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr); 71 } while (0); 72 OH_Huks_FreeParamSet(&testGenerateKeyParamSet); 73 return ohResult; 74} 75 76static napi_value DeleteKey(napi_env env, napi_callback_info info) 77{ 78 /* 1. Obtain the key alias. */ 79 const char *alias = "test_key"; 80 struct OH_Huks_Blob keyAlias = { 81 (uint32_t)strlen("test_key"), 82 (uint8_t *)"test_key" 83 }; 84 85 /* Generate a key. */ 86 OH_Huks_Result genResult = GenerateKeyHelper(alias); 87 if (genResult.errorCode != OH_HUKS_SUCCESS) { 88 napi_value ret; 89 napi_create_int32(env, genResult.errorCode, &ret); 90 return ret; 91 } 92 93 /* 2. Call OH_Huks_DeleteKeyItem to delete the key. */ 94 struct OH_Huks_Result ohResult = OH_Huks_DeleteKeyItem(&keyAlias, nullptr); 95 96 napi_value ret; 97 napi_create_int32(env, ohResult.errorCode, &ret); 98 return ret; 99} 100``` 101