• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Generating a Key (ArkTS)
2
3<!--Kit: Universal Keystore Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @wutiantian-gitee-->
6<!--Designer: @HighLowWorld-->
7<!--Tester: @wxy1234564846-->
8<!--Adviser: @zengyawen-->
9
10This topic walks you through on how to randomly generate a key with the DH algorithm. For details about the scenarios and supported algorithms, see [Supported Algorithms](huks-key-generation-overview.md#supported-algorithms).
11
12> **NOTE**
13> Key aliases must not contain sensitive information, such as personal data.
14
15## How to Develop
16
171. Set the alias (**keyAlias**) of the key to generate.
18   - The key alias can contain a maximum of 128 bytes and cannot contain sensitive information, such as personal data.
19   - 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.
20
212. Initialize the key property set.
22   - 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).
23   - 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.
24
25   > **NOTE**
26   >
27   > A key can have only one purpose, and the purpose specified during key generation must match the key purpose during usage. Otherwise, an exception occurs.
28
293. Use [generateKeyItem](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksgeneratekeyitem9) to generate a key based on the key alias and key properties specified.
30
31> **NOTE**
32> 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.
33
34```ts
35/* Generate a DH key. */
36import { huks } from '@kit.UniversalKeystoreKit';
37
38/* 1. Set the key alias. */
39let keyAlias = 'dh_key';
40/* 2. Initialize the key property set. */
41let properties1: Array<huks.HuksParam> = [
42  {
43    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
44    value: huks.HuksKeyAlg.HUKS_ALG_DH
45  },
46  {
47    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
48    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
49  },
50  {
51    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
52    value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_2048
53  }
54];
55let huksOptions: huks.HuksOptions = {
56  properties: properties1,
57  inData: new Uint8Array(new Array())
58}
59
60/* 3. Generate a key. */
61function generateKeyItem(keyAlias: string, huksOptions: huks.HuksOptions) {
62  return new Promise<void>((resolve, reject) => {
63    try {
64      huks.generateKeyItem(keyAlias, huksOptions, (error, data) => {
65        if (error) {
66          reject(error);
67        } else {
68          resolve(data);
69        }
70      });
71    } catch (error) {
72      throw (error as Error);
73    }
74  });
75}
76
77async function publicGenKeyFunc(keyAlias: string, huksOptions: huks.HuksOptions) {
78  console.info(`enter promise generateKeyItem`);
79  try {
80    await generateKeyItem(keyAlias, huksOptions)
81      .then((data) => {
82        console.info(`promise: generateKeyItem success, data = ${JSON.stringify(data)}`);
83      })
84      .catch((error: Error) => {
85        console.error(`promise: generateKeyItem failed, ${JSON.stringify(error)}`);
86      });
87  } catch (error) {
88    console.error(`promise: generateKeyItem input arg invalid, ` + JSON.stringify(error));
89  }
90}
91
92async function TestGenKey() {
93  await publicGenKeyFunc(keyAlias, huksOptions);
94}
95```
96