• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7## How to Develop
8
91. Set the alias (**keyAlias**) of the key to generate.
10   - The key alias cannot exceed 64 bytes.
11   - 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.
12
132. 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).
14   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.
15
163. 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.
17
18> **NOTE**<br>
19> 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.
20
21
22```ts
23/* Generate a DH key. */
24import huks from '@ohos.security.huks';
25import { BusinessError } from '@ohos.base';
26class HuksProperties {
27    tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM;
28    value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | huks.HuksKeyDigest = huks.HuksKeyAlg.HUKS_ALG_ECC;
29}
30/* 1. Set the key alias. */
31let keyAlias = 'dh_key';
32/* 2. Initialize the key property set. */
33let properties1: HuksProperties[] = [
34    {
35        tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
36        value: huks.HuksKeyAlg.HUKS_ALG_DH
37    },
38    {
39        tag: huks.HuksTag.HUKS_TAG_PURPOSE,
40        value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
41    },
42    {
43        tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
44        value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_2048
45    },
46    {
47        tag: huks.HuksTag.HUKS_TAG_DIGEST,
48        value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
49    }
50];
51let huksOptions: huks.HuksOptions = {
52    properties: properties1,
53    inData: new Uint8Array(new Array())
54}
55/* 3. Generate a key. */
56function generateKeyItem(keyAlias: string, huksOptions: huks.HuksOptions){
57    return new Promise<void>((resolve, reject) => {
58        try {
59            huks.generateKeyItem(keyAlias, huksOptions, (error, data) => {
60                if (error) {
61                    reject(error);
62                } else {
63                    resolve(data);
64                }
65            });
66        } catch (error) {
67            throw (error as Error);
68        }
69    });
70}
71async function publicGenKeyFunc(keyAlias: string, huksOptions: huks.HuksOptions) {
72    console.info(`enter promise generateKeyItem`);
73    try {
74        await generateKeyItem(keyAlias, huksOptions)
75        .then((data) => {
76            console.info(`promise: generateKeyItem success, data = ${JSON.stringify(data)}`);
77        })
78        .catch((error: BusinessError) => {
79            console.error(`promise: generateKeyItem failed` + error);
80        });
81    } catch (error) {
82        console.error(`promise: generateKeyItem input arg invalid` + error);
83    }
84}
85async function TestGenKey() {
86    await publicGenKeyFunc(keyAlias, huksOptions);
87}
88```
89