• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Exporting 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 export the public key of a persistently stored asymmetric key. Currently, HUKS supports export of the ECC, RSA, Ed25519, X25519, and SM2 public keys.
11>**NOTE**<br>
12> <!--RP1-->Mini-system devices<!--RP1End--> support export of only the RSA public keys.
13
14## How to Develop
15
161. Specify the key alias. For details about the naming rules, see [Key Generation Overview and Algorithm Specifications](huks-key-generation-overview.md).
17
182. Use [exportKeyItem](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksexportkeyitem9) to export the key based on the specified **keyAlias** and **options**. **options** is a reserved parameter and is left empty currently.
19
203. In the [HuksReturnResult](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksreturnresult9) object returned, the public key is in the **outData** field in the DER format defined in X.509. For details about the format, see [Public Key Material Format](huks-concepts.md#public-key-material-format).
21
22```ts
23import { huks } from '@kit.UniversalKeystoreKit';
24/* 1. Set the key alias. */
25let keyAlias = 'keyAlias';
26/* Leave options empty. */
27let emptyOptions: huks.HuksOptions = {
28  properties: []
29};
30/* 2. Set key properties. */
31let properties1: 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([])
48}
49/* 3. Generate a key. */
50function generateKeyItem(keyAlias: string, huksOptions: huks.HuksOptions) {
51  return new Promise<void>((resolve, reject) => {
52    try {
53      huks.generateKeyItem(keyAlias, huksOptions, (error, data) => {
54        if (error) {
55          reject(error);
56        } else {
57          resolve(data);
58        }
59      });
60    } catch (error) {
61      throw (error as Error);
62    }
63  });
64}
65async function publicGenKeyFunc(keyAlias: string, huksOptions: huks.HuksOptions): Promise<string> {
66  try {
67    await generateKeyItem(keyAlias, huksOptions)
68      .then((data) => {
69        console.info(`promise: generateKeyItem success, data = ${JSON.stringify(data)}`);
70      })
71      .catch((error: Error) => {
72        console.error(`promise: generateKeyItem failed, ${JSON.stringify(error)}`);
73      });
74    return 'Success';
75  } catch (error) {
76    console.error(`promise: generateKeyItem input arg invalid, ${JSON.stringify(error)}`);
77    return 'Failed';
78  }
79}
80async function testGenKey(): Promise<string> {
81  let ret = await publicGenKeyFunc(keyAlias, huksOptions);
82  return ret;
83}
84/* Export a key. */
85function exportKeyItem(keyAlias: string, emptyOptions: huks.HuksOptions) {
86  return new Promise<huks.HuksReturnResult>((resolve, reject) => {
87    try {
88      huks.exportKeyItem(keyAlias, emptyOptions, (error, data) => {
89        if (error) {
90          reject(error);
91        } else {
92          resolve(data);
93        }
94      });
95    } catch (error) {
96      throw (error as Error);
97    }
98  });
99}
100async function check(): Promise<string> {
101  try {
102    /* 1. Generate a key. */
103    let genResult = await testGenKey();
104    /* 2. Export the key. */
105    if (genResult === 'Success') {
106      let data = await exportKeyItem(keyAlias, emptyOptions);
107      console.info(`callback: exportKeyItem success, data = ${JSON.stringify(data)}`);
108    } else {
109      console.error('Key generation failed, skipping export');
110      return 'Failed';
111    }
112    return 'Success';
113  } catch (error) {
114    console.error(`callback: exportKeyItem input arg invalid, ${JSON.stringify(error)}`);
115    return 'Failed';
116  }
117}
118```
119