1# Generating a Key (ArkTS) 2 3 4This topic walks you through on how to randomly generate a DH 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## How to Develop 10 111. Set the alias (**keyAlias**) of the key to generate. 12 - The key alias cannot exceed 128 bytes. 13 - 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. 14 152. Initialize the key property set. Encapsulate key properties in [HuksParam](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksparam) and use a **HuksParam** array to assign values to the **properties** field of [HuksOptions](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksoptions). 16 The key property set must contain [HuksKeyAlg](../../reference/apis-universal-keystore-kit/js-apis-huks.md#hukskeyalg), [HuksKeySize](../../reference/apis-universal-keystore-kit/js-apis-huks.md#hukskeysize), and [HuksKeyPurpose](../../reference/apis-universal-keystore-kit/js-apis-huks.md#hukskeypurpose). That is, **TAG**, **HUKS_TAG_ALGORITHM**, **HUKS_TAG_PURPOSE**, and **HUKS_TAG_KEY_SIZE** are mandatory. 17 183. Use [huks.generateKeyItem](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksgeneratekeyitem9) to generate a key based on the key alias and key properties specified. 19 20> **NOTE**<br> 21> 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. 22 23 24```ts 25/* Generate a DH key. */ 26import { huks } from '@kit.UniversalKeystoreKit'; 27 28/* 1. Set the key alias. */ 29let keyAlias = 'dh_key'; 30/* 2. Initialize the key property set. */ 31let properties1: Array<huks.HuksParam> = [ 32 { 33 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 34 value: huks.HuksKeyAlg.HUKS_ALG_DH 35 }, 36 { 37 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 38 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE 39 }, 40 { 41 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 42 value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_2048 43 } 44]; 45let huksOptions: huks.HuksOptions = { 46 properties: properties1, 47 inData: new Uint8Array(new Array()) 48} 49 50/* 3. Generate a key. */ 51function generateKeyItem(keyAlias: string, huksOptions: huks.HuksOptions) { 52 return new Promise<void>((resolve, reject) => { 53 try { 54 huks.generateKeyItem(keyAlias, huksOptions, (error, data) => { 55 if (error) { 56 reject(error); 57 } else { 58 resolve(data); 59 } 60 }); 61 } catch (error) { 62 throw (error as Error); 63 } 64 }); 65} 66 67async function publicGenKeyFunc(keyAlias: string, huksOptions: huks.HuksOptions) { 68 console.info(`enter promise generateKeyItem`); 69 try { 70 await generateKeyItem(keyAlias, huksOptions) 71 .then((data) => { 72 console.info(`promise: generateKeyItem success, data = ${JSON.stringify(data)}`); 73 }) 74 .catch((error: Error) => { 75 console.error(`promise: generateKeyItem failed, ${JSON.stringify(error)}`); 76 }); 77 } catch (error) { 78 console.error(`promise: generateKeyItem input arg invalid, ` + JSON.stringify(error)); 79 } 80} 81 82async function TestGenKey() { 83 await publicGenKeyFunc(keyAlias, huksOptions); 84} 85``` 86