• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Obtaining Key Properties (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 describes how to obtain properties of a key. Before the operation, ensure that the key exists in HUKS.
11>**NOTE**<br>
12> <!--RP1-->The mini-system devices<!--RP1End--> do not allow for obtaining key properties.
13
14## How to Develop
15
161. Set the key alias (**keyAlias**), which cannot exceed 128 bytes.
17
182. Use [getKeyItemProperties](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksgetkeyitemproperties9) to obtain the properties of the key based on **keyAlias** and **options**. **options** is a reserved parameter and is left empty currently.
19
203. You can find the key properties in the **properties** field in the [HuksReturnResult](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksreturnresult9) object.
21
22```ts
23import { huks } from '@kit.UniversalKeystoreKit';
24/* 1. Set the key alias. */
25let keyAlias = 'keyAlias';
26/* 2. Set key properties. */
27let emptyOptions: huks.HuksOptions = {
28  properties: []
29};
30let properties1: huks.HuksParam[] = [
31  {
32    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
33    value: huks.HuksKeyAlg.HUKS_ALG_DH
34  },
35  {
36    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
37    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
38  },
39  {
40    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
41    value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_2048
42  }
43];
44let huksOptions: huks.HuksOptions = {
45  properties: properties1,
46  inData: new Uint8Array([])
47}
48/* 3. Generate a key. */
49function generateKeyItem(keyAlias: string, huksOptions: huks.HuksOptions) {
50  return new Promise<void>((resolve, reject) => {
51    try {
52      huks.generateKeyItem(keyAlias, huksOptions, (error, data) => {
53        if (error) {
54          reject(error);
55        } else {
56          resolve(data);
57        }
58      });
59    } catch (error) {
60      throw (error as Error);
61    }
62  });
63}
64async function publicGenKeyFunc(keyAlias: string, huksOptions: huks.HuksOptions): Promise<string> {
65  console.info(`enter promise generateKeyItem`);
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/* Obtain key properties. */
85function getKeyItemProperties(keyAlias: string, emptyOptions: huks.HuksOptions) {
86  return new Promise<huks.HuksReturnResult>((resolve, reject) => {
87    try {
88      huks.getKeyItemProperties(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. Obtain key properties. */
105    if (genResult === 'Success') {
106      let data = await getKeyItemProperties(keyAlias, emptyOptions);
107      console.info(`callback: getKeyItemProperties success, data = ${JSON.stringify(data)}`);
108    } else {
109      console.error('Key generation failed, skipping get properties');
110      return 'Failed';
111    }
112    return 'Success';
113  } catch (error) {
114    console.error(`callback: getKeyItemProperties input arg invalid, ${JSON.stringify(error)}`);
115    return 'Failed';
116  }
117}
118```
119