1# Generating a Key (C/C++) 2 3 4This topic walks you through on how to randomly generate an ECC key. For details about the scenarios and supported algorithms, see [Supported Algorithms](huks-key-generation-overview.md#supported-algorithms). 5 6> **NOTE**<br> 7> Key aliases must not contain sensitive information, such as personal data. 8 9## Add the dynamic library in the CMake script. 10```txt 11 target_link_libraries(entry PUBLIC libhuks_ndk.z.so) 12``` 13 14## How to Develop 15 161. Set the alias (**keyAlias**) of the key to generate. 17 - The key alias cannot exceed 128 bytes. 18 - For the keys generated for different services, HUKS isolates the storage paths based on the service identity information to prevent conflicts caused by the same key alias. 19 202. Initialize the key property set. Construct the key property set **paramSet** using [OH_Huks_InitParamSet](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_initparamset), [OH_Huks_AddParams](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_addparams), and [OH_Huks_BuildParamSet](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_buildparamset). 21 ** paramSet** must contain [OH_Huks_KeyAlg](../../reference/apis-universal-keystore-kit/_huks_type_api.md#oh_huks_keyalg), [OH_Huks_KeySize](../../reference/apis-universal-keystore-kit/_huks_type_api.md#oh_huks_keysize), and [OH_Huks_KeyPurpose](../../reference/apis-universal-keystore-kit/_huks_type_api.md#oh_huks_keypurpose). 22 233. Use [OH_Huks_GenerateKeyItem](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_generatekeyitem) to generate a key based on the key alias and key properties specified. 24 25> **NOTE**<br> 26> If the service uses the same key alias to call the HUKS API to generate a key again, HUKS will generate a new key and overwrite the historical key file. 27 28```c++ 29/* Generate an ECC key. */ 30#include "huks/native_huks_api.h" 31#include "huks/native_huks_param.h" 32#include <string.h> 33OH_Huks_Result InitParamSet( 34 struct OH_Huks_ParamSet **paramSet, 35 const struct OH_Huks_Param *params, 36 uint32_t paramCount) 37{ 38 OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet); 39 if (ret.errorCode != OH_HUKS_SUCCESS) { 40 return ret; 41 } 42 ret = OH_Huks_AddParams(*paramSet, params, paramCount); 43 if (ret.errorCode != OH_HUKS_SUCCESS) { 44 OH_Huks_FreeParamSet(paramSet); 45 return ret; 46 } 47 ret = OH_Huks_BuildParamSet(paramSet); 48 if (ret.errorCode != OH_HUKS_SUCCESS) { 49 OH_Huks_FreeParamSet(paramSet); 50 return ret; 51 } 52 return ret; 53} 54struct OH_Huks_Param g_testGenerateKeyParam[] = { 55 { 56 .tag = OH_HUKS_TAG_ALGORITHM, 57 .uint32Param = OH_HUKS_ALG_ECC 58 }, { 59 .tag = OH_HUKS_TAG_PURPOSE, 60 .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE 61 }, { 62 .tag = OH_HUKS_TAG_KEY_SIZE, 63 .uint32Param = OH_HUKS_ECC_KEY_SIZE_256 64 }, { 65 .tag = OH_HUKS_TAG_DIGEST, 66 .uint32Param = OH_HUKS_DIGEST_NONE 67 } 68}; 69static napi_value GenerateKey(napi_env env, napi_callback_info info) 70{ 71 /* 1. Set the key alias. */ 72 const char *alias = "test_generate"; 73 struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias }; 74 struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr; 75 struct OH_Huks_Result ohResult; 76 do { 77 /* 2. Initialize the key property set. */ 78 ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam, 79 sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param)); 80 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 81 break; 82 } 83 /* 3. Generate a key. */ 84 ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr); 85 } while (0); 86 OH_Huks_FreeParamSet(&testGenerateKeyParamSet); 87 napi_value ret; 88 napi_create_int32(env, ohResult.errorCode, &ret); 89 return ret; 90} 91``` 92