• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HUKS Development
2
3## When to Use
4
5 OpenHarmony Universal KeyStore (HUKS) provides KeyStore (KS) capabilities for applications, including key management and key cryptography operations. HUKS also provides APIs for applications to import or generate keys.
6
7
8## Available APIs
9
10| API                                                      | Description       |
11| ------------------------------------------------------------ | ---------------- |
12| generateKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback<HuksResult>) : void| Generates a key. This method uses an asynchronous callback to return the result.        |
13| generateKey(keyAlias: string, options: HuksOptions) : Promise<HuksResult>| Generates a key. This method uses a promise to return the result.        |
14| exportKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback<HuksResult>) : void| Exports the public key. This method uses an asynchronous callback to return the result.        |
15| exportKey(keyAlias: string, options: HuksOptions) : Promise<HuksResult>| Exports the public key. This method uses a promise to return the result.        |
16| isKeyExist(keyAlias: string, options: HuksOptions, callback: AsyncCallback<boolean>) : void | Checks whether a key exists. This method uses an asynchronous callback to return the result.|
17| isKeyExist(keyAlias: string, options: HuksOptions) : Promise<boolean> | Checks whether a key exists. This method uses a promise to return the result.|
18| deleteKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback<HuksResult>) : void| Deletes a key. This method uses an asynchronous callback to return the result.        |
19| deleteKey(keyAlias: string, options: HuksOptions) : Promise<HuksResult>| Deletes a key. This method uses a promise to return the result.        |
20
21## How to Develop
22
231. Import the HUKS module.
24
25   ```js
26   import huks from '@ohos.security.huks'
27   ```
28
292. Call **generateKey()** to generate a key.
30
31   **keyAlias** indicates the alias of the key generated. **options** indicates the parameters used for generating the key. Set **options** based on the actual situation.
32
33   The value returned indicates whether the key is successfully generated.
34
35   ```js
36   var alias = 'testAlias';
37   var properties = new Array();
38   properties[0] = {
39     tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
40     value: huks.HuksKeyAlg.HUKS_ALG_ECC
41   };
42   properties[1] = {
43     tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
44     value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_224
45   };
46   properties[2] = {
47     tag: huks.HuksTag.HUKS_TAG_PURPOSE,
48     value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
49   };
50   properties[3] = {
51     tag: huks.HuksTag.HUKS_TAG_DIGEST,
52     value: huks.HuksKeyDigest.HUKS_DIGEST_NONE
53   };
54   var options = {
55     properties: properties
56   }
57   var resultA = huks.generateKey(alias, options);
58   ```
59
603. Call **Init()** to initialize the key.
61
62   **Alias** indicates the alias of the key to initialize, and **options** indicates the parameters used for initializing the key. Set **options** based on the actual situation.
63
64   The value returned indicates whether the **Init** operation is successful.
65
66   ```js
67   var alias = 'test001'
68   var properties = new Array();
69   properties[0] = {
70     tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
71     value: huks.HuksKeyAlg.HUKS_ALG_DH
72   };
73   properties[1] = {
74     tag: huks.HuksTag.HUKS_TAG_PURPOSE,
75     value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
76   };
77   properties[2] = {
78     tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
79     value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_4096
80   };
81   var options = {
82     properties: properties
83   };
84   huks.init(alias, options, function(err, data) {
85       if (err.code !== 0) {
86           console.log("test init err information: " + JSON.stringify(err));
87       } else {
88           console.log(`test init data: ${JSON.stringify(data)}`);
89       }
90   })
91   ```
92
934. Call **update()** to update the key.
94
95   **handle** indicates the session ID for updating the key. **options** indicates the parameters used for updating the key. Set **options** based on the actual situation.
96
97   The value returned indicates whether the **Update** operation is successful.
98
99   ```js
100   var properties = new Array();
101   properties[0] = {
102     tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
103     value: huks.HuksKeyAlg.HUKS_ALG_DH
104   };
105   properties[1] = {
106     tag: huks.HuksTag.HUKS_TAG_PURPOSE,
107     value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
108   };
109   properties[2] = {
110     tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
111     value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_4096
112   };
113   var options = {
114     properties: properties
115   };
116   var result = huks.update(handle, options)
117   ```
118
1195. Call **finish()** to complete the operation.
120
121   **handle** indicates the session ID of the **Finish** operation. **options** indicates the parameters used for this operation. Set **options** based on the actual situation.
122
123   The value returned indicates whether the **Finish** operation is successful.
124
125   ```js
126   var properties = new Array();
127   properties[0] = {
128     tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
129     value: huks.HuksKeyAlg.HUKS_ALG_DH
130   };
131   properties[1] = {
132     tag: huks.HuksTag.HUKS_TAG_PURPOSE,
133     value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
134   };
135   properties[2] = {
136     tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
137     value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_4096
138   };
139   var options = {
140     properties: properties
141   };
142   var result = huks.finish(handle, options)
143   ```
144