js-apis-huks.md 128.5 KB
Newer Older
V
Vincentchenhao 已提交
1 2
# 通用密钥库系统

S
shuyi 已提交
3 4 5
> **说明**
>
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
V
Vincentchenhao 已提交
6

S
shuyi 已提交
7
向应用提供密钥库能力,包括密钥管理及密钥的密码学操作等功能。
V
Vincentchenhao 已提交
8 9 10 11 12 13 14 15
HUKS所管理的密钥可以由应用导入或者由应用调用HUKS接口生成。

## 导入模块

```js
import huks from '@ohos.security.huks'
```

C
CheungVane 已提交
16
## HuksParam
S
shuyi 已提交
17

18
调用接口使用的options中的properties数组中的param。
V
Vincentchenhao 已提交
19

20
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
21

22 23 24 25
| 参数名 | 类型                                | 必填 | 说明         |
| ------ | ----------------------------------- | ---- | ------------ |
| tag    | [HuksTag](#hukstag)                 | 是   | 标签。       |
| value  | boolean\|number\|bigint\|Uint8Array | 是   | 标签对应值。 |
Z
zhangcheng 已提交
26

C
CheungVane 已提交
27
## HuksOptions
Z
zhangcheng 已提交
28

29
调用接口使用的options。
Z
zhangcheng 已提交
30

31
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
32

33 34 35 36
| 参数名     | 类型              | 必填 | 说明                     |
| ---------- | ----------------- | ---- | ------------------------ |
| properties | Array\<[HuksParam](#huksparam)> | 否   | 属性,用于存HuksParam的数组。 |
| inData     | Uint8Array        | 否   | 输入数据。               |
V
Vincentchenhao 已提交
37

C
CheungVane 已提交
38
## HuksHandle<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
39

40
huks Handle结构体。
V
Vincentchenhao 已提交
41

42
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
43

44 45 46 47 48
| 参数名     | 类型             | 必填 | 说明     |
| ---------- | ---------------- | ---- | -------- |
| errorCode  | number           | 是   | 表示错误码。 |
| handle    | number       | 是 | 表示handle值。 |
| token | Uint8Array | 否 | 表示[init](#huksinit)操作之后获取到的challenge信息。 |
V
Vincentchenhao 已提交
49

C
CheungVane 已提交
50
## HuksSessionHandle<sup>9+</sup>
V
Vincentchenhao 已提交
51

52
huks Handle结构体。
V
Vincentchenhao 已提交
53

54
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
55

56 57 58 59
| 参数名    | 类型       | 必填 | 说明                                                 |
| --------- | ---------- | ---- | ---------------------------------------------------- |
| handle    | number     | 是   | 表示handle值。                                       |
| challenge | Uint8Array | 否   | 表示[init](#huksinit)操作之后获取到的challenge信息。 |
V
Vincentchenhao 已提交
60

C
CheungVane 已提交
61
## HuksResult<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
62

63
调用接口返回的result。
V
Vincentchenhao 已提交
64

65
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
66

V
Vincentchenhao 已提交
67 68


69 70 71 72 73 74
| 参数名     | 类型                            | 必填 | 说明             |
| ---------- | ------------------------------- | ---- | ---------------- |
| errorCode  | number                          | 是   | 表示错误码。     |
| outData    | Uint8Array                      | 否   | 表示输出数据。   |
| properties | Array\<[HuksParam](#huksparam)> | 否   | 表示属性信息。   |
| certChains | Array\<string>                  | 否   | 表示证书链数据。 |
V
Vincentchenhao 已提交
75

C
CheungVane 已提交
76
## HuksReturnResult<sup>9+</sup>
S
shuyi 已提交
77

78
调用接口返回的result。
V
Vincentchenhao 已提交
79

80
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
81 82


S
shuyi 已提交
83

84 85 86 87 88
| 参数名     | 类型                            | 必填 | 说明             |
| ---------- | ------------------------------- | ---- | ---------------- |
| outData    | Uint8Array                      | 否   | 表示输出数据。   |
| properties | Array\<[HuksParam](#huksparam)> | 否   | 表示属性信息。   |
| certChains | Array\<string>                  | 否   | 表示证书链数据。 |
V
Vincentchenhao 已提交
89

C
CheungVane 已提交
90
## huks.generateKey<sup>(deprecated)</sup>
S
shuyi 已提交
91

92
generateKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void
V
Vincentchenhao 已提交
93

94
生成密钥,使用Callback回调异步返回结果。
V
Vincentchenhao 已提交
95

96
>  **说明:** 从API Version 9开始废弃,建议使用[huks.generateKeyItem<sup>9+</sup>](#huksgeneratekeyitem9)替代。
V
Vincentchenhao 已提交
97

98
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
99

100
**参数:** 
S
shuyi 已提交
101

102 103 104 105
| 参数名   | 类型                                      | 必填 | 说明                                                         |
| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| keyAlias | string                                    | 是   | 别名。                                                       |
| options  | [HuksOptions](#huksoptions)               | 是   | 用于存放生成key所需TAG。                                     |
C
CheungVane 已提交
106
| callback | AsyncCallback\<[HuksResult](#huksresult)> | 是   | 回调函数。返回HUKS_SUCCESS时表示接口使用成功,其余结果请参考HuksResult进行错误码查询。 |
V
Vincentchenhao 已提交
107

108
**示例:**
V
Vincentchenhao 已提交
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
```js
/* 以生成RSA512密钥为例 */
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
  tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
  value: huks.HuksKeyAlg.HUKS_ALG_RSA
};
properties[1] = {
  tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
  value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_512
};
properties[2] = {
  tag: huks.HuksTag.HUKS_TAG_PURPOSE,
  value:
huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT |
huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
};
properties[3] = {
  tag: huks.HuksTag.HUKS_TAG_PADDING,
  value: huks.HuksKeyPadding.HUKS_PADDING_OAEP
};
properties[4] = {
  tag: huks.HuksTag.HUKS_TAG_DIGEST,
  value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
};
var options = {
  properties: properties
};
huks.generateKey(keyAlias, options, function (err, data){}); 
```
S
shuyi 已提交
141

C
CheungVane 已提交
142
## huks.generateKey<sup>(deprecated)</sup>
S
shuyi 已提交
143

144
generateKey(keyAlias: string, options: HuksOptions) : Promise\<HuksResult>
V
Vincentchenhao 已提交
145

146
生成密钥,使用Promise方式异步返回结果。
H
haixiangw 已提交
147

148
>  **说明:** 从API Version 9开始废弃,建议使用[huks.generateKeyItem<sup>9+</sup>](#huksgeneratekeyitem9-1)替代。
H
haixiangw 已提交
149

150
**系统能力**:SystemCapability.Security.Huks
H
haixiangw 已提交
151

152
**参数:** 
H
haixiangw 已提交
153

154 155 156 157
| 参数名   | 类型                        | 必填 | 说明                     |
| -------- | --------------------------- | ---- | ------------------------ |
| keyAlias | string                      | 是   | 密钥别名。               |
| options  | [HuksOptions](#huksoptions) | 是   | 用于存放生成key所需TAG。 |
H
haixiangw 已提交
158

C
CheungVane 已提交
159
**返回值**
H
haixiangw 已提交
160

161 162
| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
C
CheungVane 已提交
163
| Promise\<[HuksResult](#huksresult)> | Promise对象。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 |
164

165
**示例:**
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
```js
/* 以生成ECC256密钥为例 */
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
  tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
  value: huks.HuksKeyAlg.HUKS_ALG_ECC
};
properties[1] = {
  tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
  value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_256
};
properties[2] = {
  tag: huks.HuksTag.HUKS_TAG_PURPOSE,
  value:
huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_SIGN |
huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY
};
properties[3] = {
  tag: huks.HuksTag.HUKS_TAG_DIGEST,
  value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
};
var options = {
  properties: properties
};
var result = huks.generateKey(keyAlias, options);
```
194

C
CheungVane 已提交
195
## huks.generateKeyItem<sup>9+</sup>
196

197
generateKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void
198

199
生成密钥,使用Callback回调异步返回结果。
200 201 202

**系统能力**:SystemCapability.Security.Huks

203
**参数:** 
204

205 206 207 208
| 参数名   | 类型                        | 必填 | 说明                                          |
| -------- | --------------------------- | ---- | --------------------------------------------- |
| keyAlias | string                      | 是   | 别名。                                        |
| options  | [HuksOptions](#huksoptions) | 是   | 用于存放生成key所需TAG。                      |
C
CheungVane 已提交
209
| callback | AsyncCallback\<void>        | 是   | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 |
210

211
**示例:**
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
```js
/* 以生成ECC256密钥为例 */
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS_ALG_ECC
};
properties[1] = {
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
    value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_256
};
properties[2] = {
    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
    value:
    huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_SIGN |
    huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY
};
properties[3] = {
    tag: huks.HuksTag.HUKS_TAG_DIGEST,
    value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
};
var options = {
    properties: properties
};
try {
    huks.generateKeyItem(keyAlias, options, function (error, data) {
        if (error) {
            console.error(`callback: generateKeyItem failed, code: ${error.code}, msg: ${error.message}`);
        } else {
            console.info(`callback: generateKeyItem key success`);
        }
    });
} catch (error) {
    console.error(`callback: generateKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```
250

C
CheungVane 已提交
251
## huks.generateKeyItem<sup>9+</sup>
252

253
generateKeyItem(keyAlias: string, options: HuksOptions) : Promise\<void>
254

255
生成密钥,使用Promise方式异步返回结果。
256 257 258

**系统能力**:SystemCapability.Security.Huks

259
**参数:** 
260

261 262 263 264
| 参数名   | 类型                        | 必填 | 说明                     |
| -------- | --------------------------- | ---- | ------------------------ |
| keyAlias | string                      | 是   | 密钥别名。               |
| options  | [HuksOptions](#huksoptions) | 是   | 用于存放生成key所需TAG。 |
265

266
**示例:**
267

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
```js
/* 以生成ECC256密钥为例 */
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS_ALG_ECC
};
properties[1] = {
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
    value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_256
};
properties[2] = {
    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
    value:
    huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_SIGN |
    huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY
};
properties[3] = {
    tag: huks.HuksTag.HUKS_TAG_DIGEST,
    value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
};
var options = {
    properties: properties
};
try {
    huks.generateKeyItem(keyAlias, options)
        .then((data) => {
            console.info(`promise: generateKeyItem success`);
        })
        .catch(error => {
            console.error(`promise: generateKeyItem failed, code: ${error.code}, msg: ${error.message}`);
        });
} catch (error) {
    console.error(`promise: generateKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```
V
Vincentchenhao 已提交
305

C
CheungVane 已提交
306
## huks.deleteKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
307

308
deleteKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void
V
Vincentchenhao 已提交
309

310
删除密钥,使用Callback回调异步返回结果。
S
shuyi 已提交
311

312
>  **说明:** 从API Version 9开始废弃,建议使用[huks.deleteKeyItem<sup>9+</sup>](#huksdeletekeyitem9)替代。
Z
zhangcheng 已提交
313

314
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
315

316
**参数:**
V
Vincentchenhao 已提交
317

318 319 320 321
| 参数名   | 类型                                      | 必填 | 说明                                               |
| -------- | ----------------------------------------- | ---- | -------------------------------------------------- |
| keyAlias | string                                    | 是   | 密钥别名,应为生成key时传入的别名。                |
| options  | [HuksOptions](#huksoptions)               | 是   | 空对象(此处传空即可)。                           |
C
CheungVane 已提交
322
| callback | AsyncCallback\<[HuksResult](#huksresult)> | 是   | 回调函数。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 |
V
Vincentchenhao 已提交
323 324 325 326

**示例:**

```js
327
/* 此处options选择emptyOptions传空 */
S
shuyi 已提交
328
var keyAlias = 'keyAlias';
329 330
var emptyOptions = {
  properties: []
V
Vincentchenhao 已提交
331
};
332
huks.deleteKey(keyAlias, emptyOptions, function (err, data) {});
V
Vincentchenhao 已提交
333 334
```

C
CheungVane 已提交
335
## huks.deleteKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
336

337
deleteKey(keyAlias: string, options: HuksOptions) : Promise\<HuksResult>
V
Vincentchenhao 已提交
338

339
删除密钥,使用Promise方式异步返回结果。
S
shuyi 已提交
340

341
>  **说明:** 从API Version 9开始废弃,建议使用[huks.deleteKeyItem<sup>9+</sup>](#huksdeletekeyitem9-1)替代。
Z
zhangcheng 已提交
342

343
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
344

345
**参数:**
V
Vincentchenhao 已提交
346

347 348 349 350
| 参数名   | 类型        | 必填 | 说明                                                  |
| -------- | ----------- | ---- | ----------------------------------------------------- |
| keyAlias | string      | 是   | 密钥别名,应为生成key时传入的别名。 |
| options | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。 |
V
Vincentchenhao 已提交
351

352
**返回值:**
V
Vincentchenhao 已提交
353

S
shuyi 已提交
354 355
| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
C
CheungVane 已提交
356
| Promise\<[HuksResult](#huksresult)> | Promise对象。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 |
V
Vincentchenhao 已提交
357 358 359 360

**示例:**

```js
361
/* 此处options选择emptyOptions传空 */
S
shuyi 已提交
362
var keyAlias = 'keyAlias';
363 364
var emptyOptions = {
  properties: []
V
Vincentchenhao 已提交
365
};
366
var result = huks.deleteKey(keyAlias, emptyOptions);
V
Vincentchenhao 已提交
367 368
```

C
CheungVane 已提交
369
## huks.deleteKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
370

371
deleteKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void
Z
zhangcheng 已提交
372

373
删除密钥,使用Callback回调异步返回结果。
Z
zhangcheng 已提交
374 375 376

**系统能力**:SystemCapability.Security.Huks

377
**参数:**
Z
zhangcheng 已提交
378 379 380

| 参数名   | 类型                        | 必填 | 说明                                          |
| -------- | --------------------------- | ---- | --------------------------------------------- |
381 382
| keyAlias | string                      | 是   | 密钥别名,应为生成key时传入的别名。           |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。                      |
C
CheungVane 已提交
383
| callback | AsyncCallback\<void>        | 是   | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 |
Z
zhangcheng 已提交
384 385 386 387

**示例:**

```js
388
/* 此处options选择emptyOptions传空 */
Z
zhangcheng 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
var keyAlias = 'keyAlias';
var emptyOptions = {
    properties: []
};
try {
    huks.deleteKeyItem(keyAlias, emptyOptions, function (error, data) {
        if (error) {
            console.error(`callback: deleteKeyItem failed, code: ${error.code}, msg: ${error.message}`);
        } else {
            console.info(`callback: deleteKeyItem key success`);
        }
    });
} catch (error) {
    console.error(`callback: deleteKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```

C
CheungVane 已提交
406
## huks.deleteKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441

deleteKeyItem(keyAlias: string, options: HuksOptions) : Promise\<void>

删除密钥,使用Promise方式异步返回结果。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                        | 必填 | 说明                                |
| -------- | --------------------------- | ---- | ----------------------------------- |
| keyAlias | string                      | 是   | 密钥别名,应为生成key时传入的别名。 |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。            |

**示例:**

```js
/* 此处options选择emptyOptions传空 */
var keyAlias = 'keyAlias';
var emptyOptions = {
    properties: []
};
try {
    huks.deleteKeyItem(keyAlias, emptyOptions)
        .then ((data) => {
            console.info(`promise: deleteKeyItem key success`);
        })
        .catch(error => {
            console.error(`promise: deleteKeyItem failed, code: ${error.code}, msg: ${error.message}`);
        });
} catch (error) {
    console.error(`promise: deleteKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```

C
CheungVane 已提交
442
## huks.getSdkVersion
V
Vincentchenhao 已提交
443 444 445 446 447

getSdkVersion(options: HuksOptions) : string

获取当前系统sdk版本。

448
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
449

V
Vincentchenhao 已提交
450 451 452 453
**参数:**

| 参数名  | 类型       | 必填 | 说明                      |
| ------- | ---------- | ---- | ------------------------- |
S
shuyi 已提交
454
| options | [HuksOptions](#huksoptions) | 是   | 空对象,用于存放sdk版本。 |
V
Vincentchenhao 已提交
455 456 457

**返回值:**

S
shuyi 已提交
458 459 460
| 类型   | 说明          |
| ------ | ------------- |
| string | 返回sdk版本。 |
V
Vincentchenhao 已提交
461 462 463 464

**示例:**

```js
S
shuyi 已提交
465
/* 此处options选择emptyOptions传空 */
V
Vincentchenhao 已提交
466 467 468
var emptyOptions = {
  properties: []
};
S
shuyi 已提交
469
var result = huks.getSdkVersion(emptyOptions);
V
Vincentchenhao 已提交
470 471
```

C
CheungVane 已提交
472
## huks.importKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
473

Z
zengyawen 已提交
474
importKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void
V
Vincentchenhao 已提交
475

H
haixiangw 已提交
476
导入明文密钥,使用Callback方式回调异步返回结果 。
S
shuyi 已提交
477

Z
zhangcheng 已提交
478 479
>  **说明:** 从API Version 9开始废弃,建议使用[huks.importKeyItem<sup>9+</sup>](#huksimportkeyitem9)替代。

480
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
481 482 483 484 485

**参数:**

| 参数名   | 类型                     | 必填 | 说明                                              |
| -------- | ------------------------ | ---- | ------------------------------------------------- |
V
Vincentchenhao 已提交
486
| keyAlias | string                   | 是   | 密钥别名。 |
S
shuyi 已提交
487
| options  | [HuksOptions](#huksoptions) | 是   | 用于导入时所需TAG和需要导入的密钥。 |
C
CheungVane 已提交
488
| callback | AsyncCallback\<[HuksResult](#huksresult)> | 是   | 回调函数。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 |
V
Vincentchenhao 已提交
489 490 491 492

**示例:**

```js
S
shuyi 已提交
493 494 495 496 497 498 499 500 501
/* 以导入AES256密钥为例 */
var plainTextSize32 = makeRandomArr(32);
function makeRandomArr(size) {
    var arr = new Uint8Array(size);
    for (var i = 0; i < size; i++) {
        arr[i] = Math.floor(Math.random() * 10);
    }
    return arr;
};
V
Vincentchenhao 已提交
502 503 504 505
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
  tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
S
shuyi 已提交
506
  value: huks.HuksKeyAlg.HUKS_ALG_AES
V
Vincentchenhao 已提交
507 508 509
};
properties[1] = {
  tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
S
shuyi 已提交
510
  value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256
V
Vincentchenhao 已提交
511 512 513
};
properties[2] = {
  tag: huks.HuksTag.HUKS_TAG_PURPOSE,
S
shuyi 已提交
514 515
  value:
huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
V
Vincentchenhao 已提交
516 517 518
};
properties[3] = {
  tag: huks.HuksTag.HUKS_TAG_PADDING,
S
shuyi 已提交
519
  value:huks.HuksKeyPadding.HUKS_PADDING_PKCS7
V
Vincentchenhao 已提交
520 521
};
properties[4] = {
S
shuyi 已提交
522 523
  tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
  value: huks.HuksCipherMode.HUKS_MODE_ECB
V
Vincentchenhao 已提交
524 525 526
};
var options = {
  properties: properties,
S
shuyi 已提交
527
  inData: plainTextSize32
V
Vincentchenhao 已提交
528 529 530 531
};
huks.importKey(keyAlias, options, function (err, data){});
```

C
CheungVane 已提交
532
## huks.importKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
533

Z
zengyawen 已提交
534
importKey(keyAlias: string, options: HuksOptions) : Promise\<HuksResult>
V
Vincentchenhao 已提交
535

H
haixiangw 已提交
536
导入明文密钥,使用Promise方式异步返回结果。
S
shuyi 已提交
537

Z
zhangcheng 已提交
538 539
>  **说明:** 从API Version 9开始废弃,建议使用[huks.importKeyItem<sup>9+</sup>](#huksimportkeyitem9-1)替代。

540
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
541 542 543 544 545

**参数:**

| 参数名   | 类型        | 必填 | 说明                                 |
| -------- | ----------- | ---- | ------------------------------------ |
V
Vincentchenhao 已提交
546
| keyAlias | string      | 是   | 密钥别名。 |
S
shuyi 已提交
547
| options  | [HuksOptions](#huksoptions) | 是   | 用于导入时所需TAG和需要导入的密钥。 |
V
Vincentchenhao 已提交
548 549 550

**返回值:**

S
shuyi 已提交
551 552
| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
C
CheungVane 已提交
553
| Promise\<[HuksResult](#huksresult)> | Promise对象。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 |
V
Vincentchenhao 已提交
554 555 556 557

**示例:**

```js
S
shuyi 已提交
558 559 560 561 562 563 564 565 566 567 568 569
/* 以导入AES128为例 */
var plainTextSize32 = makeRandomArr(32);

function makeRandomArr(size) {
    var arr = new Uint8Array(size);
    for (var i = 0; i < size; i++) {
        arr[i] = Math.floor(Math.random() * 10);
    }
    return arr;
};

/*第一步:生成密钥*/
V
Vincentchenhao 已提交
570 571 572 573
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
  tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
S
shuyi 已提交
574
  value: huks.HuksKeyAlg.HUKS_ALG_AES
V
Vincentchenhao 已提交
575 576 577
};
properties[1] = {
  tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
S
shuyi 已提交
578
  value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128
V
Vincentchenhao 已提交
579 580 581
};
properties[2] = {
  tag: huks.HuksTag.HUKS_TAG_PURPOSE,
S
shuyi 已提交
582
  value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
V
Vincentchenhao 已提交
583 584 585
};
properties[3] = {
  tag: huks.HuksTag.HUKS_TAG_PADDING,
S
shuyi 已提交
586
  value:huks.HuksKeyPadding.HUKS_PADDING_PKCS7
V
Vincentchenhao 已提交
587 588
};
properties[4] = {
S
shuyi 已提交
589 590
  tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
  value: huks.HuksCipherMode.HUKS_MODE_ECB
V
Vincentchenhao 已提交
591
};
S
shuyi 已提交
592
var huksoptions = {
V
Vincentchenhao 已提交
593
  properties: properties,
S
shuyi 已提交
594
  inData: plainTextSize32
V
Vincentchenhao 已提交
595
};
S
shuyi 已提交
596
var result = huks.importKey(keyAlias, huksoptions);
V
Vincentchenhao 已提交
597 598
```

C
CheungVane 已提交
599
## huks.importKeyItem<sup>9+</sup>
C
CheungVane 已提交
600

Z
zhangcheng 已提交
601
importKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void
C
CheungVane 已提交
602

Z
zhangcheng 已提交
603
导入明文密钥,使用Callback方式回调异步返回结果 。
C
CheungVane 已提交
604 605 606 607 608

**系统能力**:SystemCapability.Security.Huks

**参数:**

Z
zhangcheng 已提交
609 610 611 612
| 参数名   | 类型                        | 必填 | 说明                                          |
| -------- | --------------------------- | ---- | --------------------------------------------- |
| keyAlias | string                      | 是   | 密钥别名。                                    |
| options  | [HuksOptions](#huksoptions) | 是   | 用于导入时所需TAG和需要导入的密钥。           |
C
CheungVane 已提交
613
| callback | AsyncCallback\<void>        | 是   | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 |
C
CheungVane 已提交
614 615 616 617

**示例:**

```js
Z
zhangcheng 已提交
618 619 620 621 622 623 624 625 626 627 628 629
/* 以导入AES256密钥为例 */
var plainTextSize32 = makeRandomArr(32);
function makeRandomArr(size) {
    var arr = new Uint8Array(size);
    for (var i = 0; i < size; i++) {
        arr[i] = Math.floor(Math.random() * 10);
    }
    return arr;
};
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
630
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
Z
zhangcheng 已提交
631 632 633
    value: huks.HuksKeyAlg.HUKS_ALG_AES
};
properties[1] = {
634
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
Z
zhangcheng 已提交
635 636 637
    value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256
};
properties[2] = {
638
    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
Z
zhangcheng 已提交
639 640 641 642
    value:
    huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
};
properties[3] = {
643
    tag: huks.HuksTag.HUKS_TAG_PADDING,
Z
zhangcheng 已提交
644 645 646
    value:huks.HuksKeyPadding.HUKS_PADDING_PKCS7
};
properties[4] = {
647 648
    tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
    value: huks.HuksCipherMode.HUKS_MODE_ECB
Z
zhangcheng 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
};
var options = {
    properties: properties,
    inData: plainTextSize32
};
try {
    huks.importKeyItem(keyAlias, options, function (error, data) {
        if (error) {
            console.error(`callback: importKeyItem failed, code: ${error.code}, msg: ${error.message}`);
        } else {
            console.info(`callback: importKeyItem success`);
        }
    });
} catch (error) {
    console.error(`callback: importKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
C
CheungVane 已提交
664 665 666
}
```

C
CheungVane 已提交
667
## huks.importKeyItem<sup>9+</sup>
C
CheungVane 已提交
668

Z
zhangcheng 已提交
669
importKeyItem(keyAlias: string, options: HuksOptions) : Promise\<void>
C
CheungVane 已提交
670

Z
zhangcheng 已提交
671
导入明文密钥,使用Promise方式异步返回结果。
C
CheungVane 已提交
672 673 674 675 676

**系统能力**:SystemCapability.Security.Huks

**参数:**

Z
zhangcheng 已提交
677 678 679 680
| 参数名   | 类型                        | 必填 | 说明                                |
| -------- | --------------------------- | ---- | ----------------------------------- |
| keyAlias | string                      | 是   | 密钥别名。                          |
| options  | [HuksOptions](#huksoptions) | 是   | 用于导入时所需TAG和需要导入的密钥。 |
C
CheungVane 已提交
681

C
CheungVane 已提交
682 683 684
**示例:**

```js
Z
zhangcheng 已提交
685 686
/* 以导入AES128为例 */
var plainTextSize32 = makeRandomArr(32);
C
CheungVane 已提交
687

Z
zhangcheng 已提交
688 689 690 691 692 693 694
function makeRandomArr(size) {
    var arr = new Uint8Array(size);
    for (var i = 0; i < size; i++) {
        arr[i] = Math.floor(Math.random() * 10);
    }
    return arr;
};
C
CheungVane 已提交
695

Z
zhangcheng 已提交
696 697 698 699
/*第一步:生成密钥*/
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
700
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
Z
zhangcheng 已提交
701 702 703 704 705 706 707
    value: huks.HuksKeyAlg.HUKS_ALG_AES
};
properties[1] = {
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
    value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128
};
properties[2] = {
708
    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
Z
zhangcheng 已提交
709 710 711
    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
};
properties[3] = {
712
    tag: huks.HuksTag.HUKS_TAG_PADDING,
Z
zhangcheng 已提交
713 714 715
    value:huks.HuksKeyPadding.HUKS_PADDING_PKCS7
};
properties[4] = {
716 717
    tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
    value: huks.HuksCipherMode.HUKS_MODE_ECB
Z
zhangcheng 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
};
var huksoptions = {
    properties: properties,
    inData: plainTextSize32
};
try {
    huks.importKeyItem(keyAlias, huksoptions)
        .then ((data) => {
            console.info(`promise: importKeyItem success`);
        })
        .catch(error => {
            console.error(`promise: importKeyItem failed, code: ${error.code}, msg: ${error.message}`);
        });
} catch (error) {
    console.error(`promise: importKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```
735

C
CheungVane 已提交
736
## huks.attestKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749

attestKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void

获取密钥证书,使用Callback方式回调异步返回结果 。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                                                 | 必填 | 说明                                          |
| -------- | ---------------------------------------------------- | ---- | --------------------------------------------- |
| keyAlias | string                                               | 是   | 密钥别名,存放待获取证书密钥的别名。          |
| options  | [HuksOptions](#huksoptions)                          | 是   | 用于获取证书时指定所需参数与数据。            |
C
CheungVane 已提交
750
| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult)> | 是   | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 |
Z
zhangcheng 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

**示例:**

```js
let securityLevel = stringToUint8Array('sec_level');
let challenge = stringToUint8Array('challenge_data');
let versionInfo = stringToUint8Array('version_info');
let keyAliasString = "key attest";

function stringToUint8Array(str) {
    var arr = [];
    for (var i = 0, j = str.length; i < j; ++i) {
        arr.push(str.charCodeAt(i));
    }
    var tmpUint8Array = new Uint8Array(arr);
    return tmpUint8Array;
}

async function generateKey(alias) {
    let properties = new Array();
    properties[0] = {
        tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
        value: huks.HuksKeyAlg.HUKS_ALG_RSA
    };
    properties[1] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_STORAGE_FLAG,
        value: huks.HuksKeyStorageType.HUKS_STORAGE_PERSISTENT
    };
    properties[2] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
        value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048
    };
    properties[3] = {
        tag: huks.HuksTag.HUKS_TAG_PURPOSE,
        value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY
    };
    properties[4] = {
        tag: huks.HuksTag.HUKS_TAG_DIGEST,
        value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
    };
    properties[5] = {
        tag: huks.HuksTag.HUKS_TAG_PADDING,
        value: huks.HuksKeyPadding.HUKS_PADDING_PSS
    };
    properties[6] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_GENERATE_TYPE,
        value: huks.HuksKeyGenerateType.HUKS_KEY_GENERATE_TYPE_DEFAULT
    };
    properties[7] = {
        tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
        value: huks.HuksCipherMode.HUKS_MODE_ECB
    };
    let options = {
        properties: properties
    };

    try {
        huks.generateKeyItem(alias, options, function (error, data) {
            if (error) {
                console.error(`callback: generateKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            } else {
                console.info(`callback: generateKeyItem success`);
            }
        });
    } catch (error) {
        console.error(`callback: generateKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
818 819
}

C
CheungVane 已提交
820
async function attestKey() {
Z
zhangcheng 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
    let aliasString = keyAliasString;
    let aliasUint8 = stringToUint8Array(aliasString);
    let properties = new Array();
    properties[0] = {
        tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_SEC_LEVEL_INFO,
        value: securityLevel
    };
    properties[1] = {
        tag: huks.HuksTag.HUKS_TAG_ATTESTATION_CHALLENGE,
        value: challenge
    };
    properties[2] = {
        tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_VERSION_INFO,
        value: versionInfo
    };
    properties[3] = {
        tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_ALIAS,
        value: aliasUint8
    };
    let options = {
        properties: properties
    };
    await generateKey(aliasString);
    try {
        huks.attestKeyItem(aliasString, options, function (error, data) {
            if (error) {
                console.error(`callback: attestKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            } else {
                console.info(`callback: attestKeyItem success`);
            }
        });
    } catch (error) {
        console.error(`callback: attestKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
C
CheungVane 已提交
855 856 857
}
```

C
CheungVane 已提交
858
## huks.attestKeyItem<sup>9+</sup>
H
haixiangw 已提交
859

Z
zhangcheng 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
attestKeyItem(keyAlias: string, options: HuksOptions) : Promise\<HuksReturnResult>

获取密钥证书,使用Promise方式异步返回结果 。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                        | 必填 | 说明                                 |
| -------- | --------------------------- | ---- | ------------------------------------ |
| keyAlias | string                      | 是   | 密钥别名,存放待获取证书密钥的别名。 |
| options  | [HuksOptions](#huksoptions) | 是   | 用于获取证书时指定所需参数与数据。   |

**返回值:**

| 类型                                           | 说明                                          |
| ---------------------------------------------- | --------------------------------------------- |
C
CheungVane 已提交
877
| Promise<[HuksReturnResult](#huksreturnresult)> | Promise对象。不返回err值时表示接口使用成功,其他时为错误。 |
Z
zhangcheng 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984

**示例:**

```js
let securityLevel = stringToUint8Array('sec_level');
let challenge = stringToUint8Array('challenge_data');
let versionInfo = stringToUint8Array('version_info');
let keyAliasString = "key attest";

function stringToUint8Array(str) {
    var arr = [];
    for (var i = 0, j = str.length; i < j; ++i) {
        arr.push(str.charCodeAt(i));
    }
    var tmpUint8Array = new Uint8Array(arr);
    return tmpUint8Array;
}

async function generateKey(alias) {
    let properties = new Array();
    properties[0] = {
        tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
        value: huks.HuksKeyAlg.HUKS_ALG_RSA
    };
    properties[1] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_STORAGE_FLAG,
        value: huks.HuksKeyStorageType.HUKS_STORAGE_PERSISTENT
    };
    properties[2] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
        value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048
    };
    properties[3] = {
        tag: huks.HuksTag.HUKS_TAG_PURPOSE,
        value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY
    };
    properties[4] = {
        tag: huks.HuksTag.HUKS_TAG_DIGEST,
        value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
    };
    properties[5] = {
        tag: huks.HuksTag.HUKS_TAG_PADDING,
        value: huks.HuksKeyPadding.HUKS_PADDING_PSS
    };
    properties[6] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_GENERATE_TYPE,
        value: huks.HuksKeyGenerateType.HUKS_KEY_GENERATE_TYPE_DEFAULT
    };
    properties[7] = {
        tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
        value: huks.HuksCipherMode.HUKS_MODE_ECB
    };
    let options = {
        properties: properties
    };

    try {
        await huks.generateKeyItem(alias, options)
            .then((data) => {
                console.info(`promise: generateKeyItem success`);
            })
            .catch(error => {
                console.error(`promise: generateKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`promise: generateKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}

async function attestKey() {
    let aliasString = keyAliasString;
    let aliasUint8 = stringToUint8Array(aliasString);
    let properties = new Array();
    properties[0] = {
        tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_SEC_LEVEL_INFO,
        value: securityLevel
    };
    properties[1] = {
        tag: huks.HuksTag.HUKS_TAG_ATTESTATION_CHALLENGE,
        value: challenge
    };
    properties[2] = {
        tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_VERSION_INFO,
        value: versionInfo
    };
    properties[3] = {
        tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_ALIAS,
        value: aliasUint8
    };
    let options = {
        properties: properties
    };
    await generateKey(aliasString);
    try {
        await huks.attestKeyItem(aliasString, options)
            .then ((data) => {
                console.info(`promise: attestKeyItem success`);
            })
            .catch(error => {
                console.error(`promise: attestKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`promise: attestKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}
```

C
CheungVane 已提交
985
## huks.importWrappedKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
986 987

importWrappedKeyItem(keyAlias: string, wrappingKeyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void
H
haixiangw 已提交
988 989 990

导入加密密钥,使用Callback方式回调异步返回结果 。

991
**系统能力**:SystemCapability.Security.Huks
H
haixiangw 已提交
992 993 994

**参数:**

Z
zhangcheng 已提交
995 996 997 998 999
| 参数名           | 类型                        | 必填 | 说明                                          |
| ---------------- | --------------------------- | ---- | --------------------------------------------- |
| keyAlias         | string                      | 是   | 密钥别名,存放待导入密钥的别名。              |
| wrappingKeyAlias | string                      | 是   | 密钥别名,对应密钥用于解密加密的密钥数据。    |
| options          | [HuksOptions](#huksoptions) | 是   | 用于导入时所需TAG和需要导入的加密的密钥数据。 |
C
CheungVane 已提交
1000
| callback         | AsyncCallback\<void>        | 是   | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 |
H
haixiangw 已提交
1001 1002 1003 1004

**示例:**

```js
Z
zhangcheng 已提交
1005 1006
import huks from '@ohos.security.huks';

H
haixiangw 已提交
1007 1008 1009 1010 1011
var exportWrappingKey;
var alias1 = "importAlias";
var alias2 = "wrappingKeyAlias";

async function TestGenFunc(alias, options) {
Z
zhangcheng 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
    try {
        await genKey(alias, options)
            .then((data) => {
                console.info(`callback: generateKeyItem success`);
            })
            .catch(error => {
                console.error(`callback: generateKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`callback: generateKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
H
haixiangw 已提交
1023 1024 1025
}

function genKey(alias, options) {
Z
zhangcheng 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    return new Promise((resolve, reject) => {
        try {
            huks.generateKeyItem(alias, options, function (error, data) {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        } catch (error) {
            throw(error);
        }
H
haixiangw 已提交
1038 1039 1040 1041
    });
}

async function TestExportFunc(alias, options) {
Z
zhangcheng 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    try {
        await exportKey(alias, options)
            .then ((data) => {
                console.info(`callback: exportKeyItem success, data = ${JSON.stringify(data)}`);
                exportWrappingKey = data.outData;
            })
            .catch(error => {
                console.error(`callback: exportKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`callback: exportKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
H
haixiangw 已提交
1054 1055
}

Z
zhangcheng 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
function exportKey(alias, options) : Promise<huks.HuksReturnResult> {
    return new Promise((resolve, reject) => {
        try {
            huks.exportKeyItem(alias, options, function (error, data) {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        } catch (error) {
            throw(error);
        }
H
haixiangw 已提交
1069 1070 1071 1072
    });
}

async function TestImportWrappedFunc(alias, wrappingAlias, options) {
Z
zhangcheng 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
    try {
        await importWrappedKey(alias, wrappingAlias, options)
            .then ((data) => {
                console.info(`callback: importWrappedKeyItem success`);
            })
            .catch(error => {
                console.error(`callback: importWrappedKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`callback: importWrappedKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
H
haixiangw 已提交
1084 1085 1086
}

function importWrappedKey(alias, wrappingAlias, options) {
Z
zhangcheng 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    return new Promise((resolve, reject) => {
        try {
            huks.importWrappedKeyItem(alias, wrappingAlias, options, function (error, data) {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        } catch (error) {
            throw(error);
        }
H
haixiangw 已提交
1099 1100 1101 1102
    });
}

async function TestImportWrappedKeyFunc(
Z
zhangcheng 已提交
1103 1104 1105 1106
        alias,
        wrappingAlias,
        genOptions,
        importOptions
H
haixiangw 已提交
1107 1108 1109 1110 1111 1112 1113 1114
) {
    await TestGenFunc(wrappingAlias, genOptions);
    await TestExportFunc(wrappingAlias, genOptions);

    /* 以下操作不需要调用HUKS接口,此处不给出具体实现。
     * 假设待导入的密钥为keyA
     * 1.生成ECC公私钥keyB,公钥为keyB_pub, 私钥为keyB_pri
     * 2.使用keyB_pri和wrappingAlias密钥中获取的公钥进行密钥协商,协商出共享密钥share_key
1115 1116
     * 3.随机生成密钥kek,用于加密keyA,采用AES-GCM加密,加密过程中需要记录:nonce1、aad1、加密后的密文keyA_enc、加密后的tag1。
     * 4.使用share_key加密kek,采用AES-GCM加密,加密过程中需要记录:nonce2、aad2、加密后的密文kek_enc、加密后的tag2。
H
haixiangw 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
     * 5.拼接importOptions.inData字段,满足以下格式:
     * keyB_pub的长度(4字节) + keyB_pub的数据 + aad2的长度(4字节) + aad2的数据 +
     * nonce2的长度(4字节)   + nonce2的数据   + tag2的长度(4字节) + tag2的数据 +
     * kek_enc的长度(4字节)  + kek_enc的数据  + aad1的长度(4字节) + aad1的数据 +
     * nonce1的长度(4字节)   + nonce1的数据   + tag1的长度(4字节) + tag1的数据 +
     * keyA长度占用的内存长度(4字节)  + keyA的长度     + keyA_enc的长度(4字节) + keyA_enc的数据
     */
    var inputKey = new Uint8Array([0x02, 0x00, 0x00, 0x00]);
    importOptions.inData = inputKey;
    await TestImportWrappedFunc(alias, wrappingAlias, importOptions);
}

function makeGenerateOptions() {
Z
zhangcheng 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    var properties = new Array();
    properties[0] = {
        tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
        value: huks.HuksKeyAlg.HUKS_ALG_ECC
    };
    properties[1] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
        value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_256
    };
    properties[2] = {
        tag: huks.HuksTag.HUKS_TAG_PURPOSE,
        value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_UNWRAP
    };
    properties[3] = {
        tag: huks.HuksTag.HUKS_TAG_DIGEST,
        value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
    };
    properties[4] = {
        tag: huks.HuksTag.HUKS_TAG_IMPORT_KEY_TYPE,
        value: huks.HuksImportKeyType.HUKS_KEY_TYPE_KEY_PAIR,
    };
    var options = {
        properties: properties
    };
    return options;
H
haixiangw 已提交
1155 1156 1157
};

function makeImportOptions() {
Z
zhangcheng 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    var properties = new Array();
    properties[0] = {
        tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
        value: huks.HuksKeyAlg.HUKS_ALG_AES
    };
    properties[1] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
        value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256
    };
    properties[2] = {
        tag: huks.HuksTag.HUKS_TAG_PURPOSE,
        value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
    };
    properties[3] = {
        tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
        value: huks.HuksCipherMode.HUKS_MODE_CBC
    };
    properties[4] = {
        tag: huks.HuksTag.HUKS_TAG_PADDING,
        value: huks.HuksKeyPadding.HUKS_PADDING_NONE
    };
    properties[5] = {
        tag: huks.HuksTag.HUKS_TAG_UNWRAP_ALGORITHM_SUITE,
        value: huks.HuksUnwrapSuite.HUKS_UNWRAP_SUITE_ECDH_AES_256_GCM_NOPADDING
    };
    var options = {
        properties: properties
    };
    return options;
H
haixiangw 已提交
1187 1188 1189
};

function huksImportWrappedKey() {
Z
zhangcheng 已提交
1190 1191 1192 1193 1194 1195 1196 1197
    var genOptions = makeGenerateOptions();
    var importOptions = makeImportOptions();
    TestImportWrappedKeyFunc(
        alias1,
        alias2,
        genOptions,
        importOptions
    );
H
haixiangw 已提交
1198 1199 1200
}
```

C
CheungVane 已提交
1201
## huks.importWrappedKeyItem<sup>9+</sup>
H
haixiangw 已提交
1202

Z
zhangcheng 已提交
1203
importWrappedKeyItem(keyAlias: string, wrappingKeyAlias: string, options: HuksOptions) : Promise\<void>
H
haixiangw 已提交
1204 1205 1206

导入加密密钥,使用Promise方式异步返回结果。

1207
**系统能力**:SystemCapability.Security.Huks
H
haixiangw 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221

**参数:**

| 参数名           | 类型                        | 必填 | 说明                                          |
| ---------------- | --------------------------- | ---- | --------------------------------------------- |
| keyAlias         | string                      | 是   | 密钥别名,存放待导入密钥的别名。              |
| wrappingKeyAlias | string                      | 是   | 密钥别名,对应密钥用于解密加密的密钥数据。    |
| options          | [HuksOptions](#huksoptions) | 是   | 用于导入时所需TAG和需要导入的加密的密钥数据。 |

**示例:**

```js
/* 处理流程与callback类似,主要差异点为如下函数: */
async function TestImportWrappedFunc(alias, wrappingAlias, options) {
Z
zhangcheng 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
    try {
        await huks.importWrappedKeyItem(alias, wrappingAlias, options)
            .then ((data) => {
                console.info(`promise: importWrappedKeyItem success`);
            })
            .catch(error => {
                console.error(`promise: importWrappedKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`promise: importWrappedKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
H
haixiangw 已提交
1233 1234 1235
}
```

C
CheungVane 已提交
1236
## huks.exportKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1237

Z
zengyawen 已提交
1238
exportKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void
V
Vincentchenhao 已提交
1239

S
shuyi 已提交
1240
导出密钥,使用Callback方式回调异步返回的结果。
S
shuyi 已提交
1241

Z
zhangcheng 已提交
1242 1243
>  **说明:** 从API Version 9开始废弃,建议使用[huks.exportKeyItem<sup>9+</sup>](#huksexportkeyitem9)替代。

1244
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1245 1246 1247

**参数:**

Z
zengyawen 已提交
1248 1249
| 参数名   | 类型                                      | 必填 | 说明                                                         |
| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
S
shuyi 已提交
1250 1251
| keyAlias | string                                    | 是   | 密钥别名,应与所用密钥生成时使用的别名相同。                 |
| options  | [HuksOptions](#huksoptions)               | 是   | 空对象(此处传空即可)。                                     |
C
CheungVane 已提交
1252
| callback | AsyncCallback\<[HuksResult](#huksresult)> | 是   | 回调函数。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。outData:返回从密钥中导出的公钥。 |
V
Vincentchenhao 已提交
1253 1254 1255 1256

**示例:**

```js
S
shuyi 已提交
1257
/* 此处options选择emptyOptions来传空 */
V
Vincentchenhao 已提交
1258 1259 1260 1261 1262 1263 1264
var keyAlias = 'keyAlias';
var emptyOptions = {
  properties: []
};
huks.exportKey(keyAlias, emptyOptions, function (err, data){});
```

C
CheungVane 已提交
1265
## huks.exportKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1266

Z
zengyawen 已提交
1267
exportKey(keyAlias: string, options: HuksOptions) : Promise\<HuksResult>
V
Vincentchenhao 已提交
1268

S
shuyi 已提交
1269
导出密钥,使用Promise方式回调异步返回的结果。
S
shuyi 已提交
1270

Z
zhangcheng 已提交
1271 1272
>  **说明:** 从API Version 9开始废弃,建议使用[huks.exportKeyItem<sup>9+</sup>](#huksexportkeyitem9-1))替代。 

1273
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1274 1275 1276 1277 1278

**参数:**

| 参数名   | 类型        | 必填 | 说明                                                         |
| -------- | ----------- | ---- | ------------------------------------------------------------ |
S
shuyi 已提交
1279 1280
| keyAlias | string      | 是   | 密钥别名,应与所用密钥生成时使用的别名相同。 |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。 |
V
Vincentchenhao 已提交
1281 1282 1283 1284 1285

**返回值:**

| 类型                                | 说明                                                         |
| ----------------------------------- | ------------------------------------------------------------ |
C
CheungVane 已提交
1286
| Promise\<[HuksResult](#huksresult)> | Promise对象。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。outData:返回从密钥中导出的公钥。 |
V
Vincentchenhao 已提交
1287 1288 1289 1290

**示例:**

```js
S
shuyi 已提交
1291
/* 此处options选择emptyOptions来传空 */
V
Vincentchenhao 已提交
1292 1293 1294 1295
var keyAlias = 'keyAlias';
var emptyOptions = {
  properties: []
};
S
shuyi 已提交
1296
var result = huks.exportKey(keyAlias, emptyOptions);
V
Vincentchenhao 已提交
1297 1298
```

C
CheungVane 已提交
1299
## huks.exportKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312

exportKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void

导出密钥,使用Callback方式回调异步返回的结果。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                                                 | 必填 | 说明                                                         |
| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| keyAlias | string                                               | 是   | 密钥别名,应与所用密钥生成时使用的别名相同。                 |
| options  | [HuksOptions](#huksoptions)                          | 是   | 空对象(此处传空即可)。                                     |
C
CheungVane 已提交
1313
| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult)> | 是   | 回调函数。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。outData:返回从密钥中导出的公钥。 |
Z
zhangcheng 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335

**示例:**

```js
/* 此处options选择emptyOptions来传空 */
var keyAlias = 'keyAlias';
var emptyOptions = {
    properties: []
};
try {
    huks.exportKeyItem(keyAlias, emptyOptions, function (error, data) {
        if (error) {
            console.error(`callback: exportKeyItem failed, code: ${error.code}, msg: ${error.message}`);
        } else {
            console.info(`callback: exportKeyItem success, data = ${JSON.stringify(data)}`);
        }
    });
} catch (error) {
    console.error(`callback: exportKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```

C
CheungVane 已提交
1336
## huks.exportKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354

exportKeyItem(keyAlias: string, options: HuksOptions) : Promise\<HuksReturnResult>

导出密钥,使用Promise方式回调异步返回的结果。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                        | 必填 | 说明                                         |
| -------- | --------------------------- | ---- | -------------------------------------------- |
| keyAlias | string                      | 是   | 密钥别名,应与所用密钥生成时使用的别名相同。 |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。                     |

**返回值:**

| 类型                                           | 说明                                                         |
| ---------------------------------------------- | ------------------------------------------------------------ |
C
CheungVane 已提交
1355
| Promise<[HuksReturnResult](#huksreturnresult)> | Promise对象。不返回err值时表示接口使用成功,其他时为错误。outData:返回从密钥中导出的公钥。 |
Z
zhangcheng 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377

**示例:**

```js
/* 此处options选择emptyOptions来传空 */
var keyAlias = 'keyAlias';
var emptyOptions = {
    properties: []
};
try {
    huks.exportKeyItem(keyAlias, emptyOptions)
        .then ((data) => {
            console.info(`promise: exportKeyItem success, data = ${JSON.stringify(data)}`);
        })
        .catch(error => {
            console.error(`promise: exportKeyItem failed, code: ${error.code}, msg: ${error.message}`);
        });
} catch (error) {
    console.error(`promise: exportKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```

C
CheungVane 已提交
1378
## huks.getKeyProperties<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1379

Z
zengyawen 已提交
1380
getKeyProperties(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void
V
Vincentchenhao 已提交
1381

S
shuyi 已提交
1382 1383
获取密钥属性,使用Callback回调异步返回结果。

Z
zhangcheng 已提交
1384 1385
>  **说明:** 从API Version 9开始废弃,建议使用[huks.getKeyItemProperties<sup>9+</sup>](#huksgetkeyitemproperties9)替代。

1386
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1387 1388 1389 1390 1391

**参数:**

| 参数名   | 类型                                      | 必填 | 说明                                                         |
| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
S
shuyi 已提交
1392 1393
| keyAlias | string                                    | 是   | 密钥别名,应与所用密钥生成时使用的别名相同。                 |
| options  | [HuksOptions](#huksoptions)               | 是   | 空对象(此处传空即可)。                                     |
C
CheungVane 已提交
1394
| callback | AsyncCallback\<[HuksResult](#huksresult)> | 是   | 回调函数。errorCode:返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 |
V
Vincentchenhao 已提交
1395 1396 1397 1398

**示例:**

```js
S
shuyi 已提交
1399
/* 此处options选择emptyOptions来传空 */
V
Vincentchenhao 已提交
1400 1401 1402 1403 1404 1405 1406
var keyAlias = 'keyAlias';
var emptyOptions = {
  properties: []
};
huks.getKeyProperties(keyAlias, emptyOptions, function (err, data){});
```

C
CheungVane 已提交
1407
## huks.getKeyProperties<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1408

Z
zengyawen 已提交
1409
getKeyProperties(keyAlias: string, options: HuksOptions) : Promise\<HuksResult>
V
Vincentchenhao 已提交
1410

S
shuyi 已提交
1411 1412
获取密钥属性,使用Promise回调异步返回结果。

Z
zhangcheng 已提交
1413 1414
>  **说明:** 从API Version 9开始废弃,建议使用[huks.getKeyItemProperties<sup>9+</sup>](#huksgetkeyitemproperties9-1)替代。

1415
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1416 1417 1418 1419 1420

**参数:**

| 参数名   | 类型        | 必填 | 说明                                                         |
| -------- | ----------- | ---- | ------------------------------------------------------------ |
S
shuyi 已提交
1421 1422
| keyAlias | string      | 是   | 密钥别名,应与所用密钥生成时使用的别名相同。 |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。 |
V
Vincentchenhao 已提交
1423 1424 1425 1426 1427

**返回值:**

| 类型               | 说明                                                         |
| ------------------ | ------------------------------------------------------------ |
C
CheungVane 已提交
1428
| Promise\<[HuksResult](#huksoptions)> | Promise对象。errorCode:返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。properties:返回值为生成密钥时所需参数。 |
V
Vincentchenhao 已提交
1429 1430 1431 1432

**示例:**

```js
S
shuyi 已提交
1433
/* 此处options选择emptyOptions来传空 */
V
Vincentchenhao 已提交
1434 1435 1436 1437
var keyAlias = 'keyAlias';
var emptyOptions = {
  properties: []
};
S
shuyi 已提交
1438
var result = huks.getKeyProperties(keyAlias, emptyOptions);
V
Vincentchenhao 已提交
1439 1440
```

C
CheungVane 已提交
1441
## huks.getKeyItemProperties<sup>9+</sup>
Z
zhangcheng 已提交
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454

getKeyItemProperties(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void

获取密钥属性,使用Callback回调异步返回结果。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                                                 | 必填 | 说明                                                         |
| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| keyAlias | string                                               | 是   | 密钥别名,应与所用密钥生成时使用的别名相同。                 |
| options  | [HuksOptions](#huksoptions)                          | 是   | 空对象(此处传空即可)。                                     |
C
CheungVane 已提交
1455
| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult)> | 是   | 回调函数。errorCode:返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 |
Z
zhangcheng 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

**示例:**

```js
/* 此处options选择emptyOptions来传空 */
var keyAlias = 'keyAlias';
var emptyOptions = {
    properties: []
};
try {
    huks.getKeyItemProperties(keyAlias, emptyOptions, function (error, data) {
        if (error) {
            console.error(`callback: getKeyItemProperties failed, code: ${error.code}, msg: ${error.message}`);
        } else {
            console.info(`callback: getKeyItemProperties success, data = ${JSON.stringify(data)}`);
        }
    });
} catch (error) {
    console.error(`callback: getKeyItemProperties input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```

C
CheungVane 已提交
1478
## huks.getKeyItemProperties<sup>9+</sup>
Z
zhangcheng 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496

getKeyItemProperties(keyAlias: string, options: HuksOptions) : Promise\<HuksReturnResult>

获取密钥属性,使用Promise回调异步返回结果。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                        | 必填 | 说明                                         |
| -------- | --------------------------- | ---- | -------------------------------------------- |
| keyAlias | string                      | 是   | 密钥别名,应与所用密钥生成时使用的别名相同。 |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。                     |

**返回值:**

| 类型                                            | 说明                                                         |
| ----------------------------------------------- | ------------------------------------------------------------ |
C
CheungVane 已提交
1497
| Promise\<[HuksReturnResult](#huksreturnresult)> | Promise对象。不返回err值时表示接口使用成功,其他时为错误。properties:返回值为生成密钥时所需参数。 |
Z
zhangcheng 已提交
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519

**示例:**

```js
/* 此处options选择emptyOptions来传空 */
var keyAlias = 'keyAlias';
var emptyOptions = {
    properties: []
};
try {
    huks.getKeyItemProperties(keyAlias, emptyOptions)
        .then ((data) => {
            console.info(`promise: getKeyItemProperties success, data = ${JSON.stringify(data)}`);
        })
        .catch(error => {
            console.error(`promise: getKeyItemProperties failed, code: ${error.code}, msg: ${error.message}`);
        });
} catch (error) {
    console.error(`promise: getKeyItemProperties input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```

C
CheungVane 已提交
1520
## huks.isKeyExist<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1521

Z
zengyawen 已提交
1522
isKeyExist(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<boolean>) : void
V
Vincentchenhao 已提交
1523

S
shuyi 已提交
1524 1525
判断密钥是否存在,使用Callback回调异步返回结果 。

C
CheungVane 已提交
1526
>  **说明:** 从API Version 9开始废弃,建议使用[huks.isKeyItemExist<sup>9+</sup>](#huksiskeyitemexist9)替代。
1527

1528
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1529 1530 1531 1532 1533

**参数:**

| 参数名   | 类型                   | 必填 | 说明                                  |
| -------- | ---------------------- | ---- | ------------------------------------- |
S
shuyi 已提交
1534 1535
| keyAlias | string                 | 是   | 所需查找的密钥的别名。 |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。 |
C
CheungVane 已提交
1536
| callback | AsyncCallback\<boolean> | 是   | 回调函数。FALSE代表密钥不存在,TRUE代表密钥存在。 |
V
Vincentchenhao 已提交
1537 1538 1539 1540

**示例:**

```js
S
shuyi 已提交
1541
/* 此处options选择emptyOptions来传空 */
V
Vincentchenhao 已提交
1542 1543 1544 1545 1546 1547 1548
var keyAlias = 'keyAlias';
var emptyOptions = {
  properties: []
};
huks.isKeyExist(keyAlias, emptyOptions, function (err, data){});
```

C
CheungVane 已提交
1549
## huks.isKeyExist<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1550

Z
zengyawen 已提交
1551
isKeyExist(keyAlias: string, options: HuksOptions) : Promise\<boolean>
V
Vincentchenhao 已提交
1552

S
shuyi 已提交
1553 1554
判断密钥是否存在,使用Promise回调异步返回结果 。

C
CheungVane 已提交
1555
>  **说明:** 从API Version 9开始废弃,建议使用[huks.isKeyItemExist<sup>9+</sup>](#huksiskeyitemexist9-1)替代。
1556

1557
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1558 1559 1560 1561 1562

**参数:**

| 参数名   | 类型        | 必填 | 说明                             |
| -------- | ----------- | ---- | -------------------------------- |
S
shuyi 已提交
1563 1564
| keyAlias | string      | 是   | 所需查找的密钥的别名。 |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。 |
V
Vincentchenhao 已提交
1565 1566 1567

**返回值:**

S
shuyi 已提交
1568 1569
| 类型              | 说明                                    |
| ----------------- | --------------------------------------- |
C
CheungVane 已提交
1570
| Promise\<boolean> | Promise对象。FALSE代表密钥不存在,TRUE代表密钥存在。 |
V
Vincentchenhao 已提交
1571 1572 1573 1574

**示例:**

```js
S
shuyi 已提交
1575
/* 此处options选择emptyOptions来传空 */
V
Vincentchenhao 已提交
1576 1577 1578 1579
var keyAlias = 'keyAlias';
var emptyOptions = {
  properties: []
};
S
shuyi 已提交
1580
var result = huks.isKeyExist(keyAlias, emptyOptions);
V
Vincentchenhao 已提交
1581 1582
```

C
CheungVane 已提交
1583
## huks.isKeyItemExist<sup>9+</sup>
Z
zhangcheng 已提交
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596

isKeyItemExist(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<boolean>) : void

判断密钥是否存在,使用Callback回调异步返回结果 。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                        | 必填 | 说明                                    |
| -------- | --------------------------- | ---- | --------------------------------------- |
| keyAlias | string                      | 是   | 所需查找的密钥的别名。                  |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。                |
C
CheungVane 已提交
1597
| callback | AsyncCallback\<boolean>     | 是   | 回调函数。FALSE代表密钥不存在,TRUE代表密钥存在。 |
Z
zhangcheng 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619

**示例:**

```js
/* 此处options选择emptyOptions来传空 */
var keyAlias = 'keyAlias';
var emptyOptions = {
    properties: []
};
try {
    huks.isKeyItemExist(keyAlias, emptyOptions, function (error, data) {
        if (error) {
            console.info(`callback: isKeyItemExist success, data = ${JSON.stringify(data)}`);
        } else {
            console.error(`callback: isKeyItemExist failed, code: ${error.code}, msg: ${error.message}`);
        }
    });
} catch (error) {
    console.error(`promise: isKeyItemExist input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```

C
CheungVane 已提交
1620
## huks.isKeyItemExist<sup>9+</sup>
V
Vincentchenhao 已提交
1621

Z
zhangcheng 已提交
1622
isKeyItemExist(keyAlias: string, options: HuksOptions) : Promise\<boolean>
S
shuyi 已提交
1623

Z
zhangcheng 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
判断密钥是否存在,使用Promise回调异步返回结果 。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                        | 必填 | 说明                     |
| -------- | --------------------------- | ---- | ------------------------ |
| keyAlias | string                      | 是   | 所需查找的密钥的别名。   |
| options  | [HuksOptions](#huksoptions) | 是   | 空对象(此处传空即可)。 |

**返回值:**

| 类型              | 说明                                    |
| ----------------- | --------------------------------------- |
C
CheungVane 已提交
1639
| Promise\<boolean> | Promise对象。FALSE代表密钥不存在,TRUE代表密钥存在。 |
Z
zhangcheng 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661

**示例:**

```js
/* 此处options选择emptyOptions来传空 */
var keyAlias = 'keyAlias';
var emptyOptions = {
    properties: []
};
try {
    huks.isKeyItemExist(keyAlias, emptyOptions)
        .then ((data) => {
            console.info(`promise: isKeyItemExist success, data = ${JSON.stringify(data)}`);
        })
        .catch(error => {
            console.error(`promise: isKeyItemExist failed, code: ${error.code}, msg: ${error.message}`);
        });
} catch (error) {
    console.error(`promise: isKeyItemExist input arg invalid, code: ${error.code}, msg: ${error.message}`);
}
```

C
CheungVane 已提交
1662
## huks.init<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1663

Z
zengyawen 已提交
1664
init(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksHandle>) : void
V
Vincentchenhao 已提交
1665

1666
init操作密钥接口,使用Callback回调异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。
S
shuyi 已提交
1667

Z
zhangcheng 已提交
1668 1669
>  **说明:** 从API Version 9开始废弃,建议使用[huks.initSession<sup>9+</sup>](#huksinitsession9-1)替代。

1670
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1671 1672 1673 1674 1675

**参数:**

| 参数名   | 类型                   | 必填 | 说明                                  |
| -------- | ---------------------- | ---- | ------------------------------------- |
S
shuyi 已提交
1676 1677
| keyAlias | string                 | 是   | Init操作密钥的别名。 |
| options  | [HuksOptions](#huksoptions) | 是   | Init操作的参数集合。 |
C
CheungVane 已提交
1678
| callback | AsyncCallback\<[HuksHandle](#hukshandle)> | 是   | 回调函数。将Init操作操作返回的handle添加到密钥管理系统的回调。 |
V
Vincentchenhao 已提交
1679 1680


C
CheungVane 已提交
1681
## huks.init<sup>(deprecated)</sup>
Z
zengyawen 已提交
1682 1683

init(keyAlias: string, options: HuksOptions) : Promise\<HuksHandle>
V
Vincentchenhao 已提交
1684

1685
init操作密钥接口,使用Promise方式异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。
V
Vincentchenhao 已提交
1686

Z
zhangcheng 已提交
1687 1688
>  **说明:** 从API Version 9开始废弃,建议使用[huks.initSession<sup>9+</sup>](#huksinitsession9-1)替代。

1689
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
1690

V
Vincentchenhao 已提交
1691 1692 1693 1694
**参数:**

| 参数名   | 类型                   | 必填 | 说明                                  |
| -------- | ---------------------- | ---- | ------------------------------------- |
S
shuyi 已提交
1695 1696
| keyAlias | string                 | 是   | Init操作密钥的别名。 |
| options  | [HuksOptions](#huksoptions) | 是   | Init参数集合。 |
C
CheungVane 已提交
1697 1698 1699 1700 1701 1702

**返回值**

| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
| Promise\<[HuksHandle](#hukshandle)> | Promise对象。将Init操作返回的handle添加到密钥管理系统的回调。 |
V
Vincentchenhao 已提交
1703

C
CheungVane 已提交
1704
## huks.initSession<sup>9+</sup>
Z
zhangcheng 已提交
1705 1706 1707

initSession(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksSessionHandle>) : void

1708
initSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。
Z
zhangcheng 已提交
1709

1710
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
1711 1712 1713 1714 1715 1716 1717

**参数:**

| 参数名   | 类型                                                    | 必填 | 说明                                                 |
| -------- | ------------------------------------------------------- | ---- | ---------------------------------------------------- |
| keyAlias | string                                                  | 是   | Init操作密钥的别名。                                 |
| options  | [HuksOptions](#huksoptions)                             | 是   | Init操作的参数集合。                                 |
C
CheungVane 已提交
1718
| callback | AsyncCallback\<[HuksSessionHandle](#hukssessionhandle)> | 是   | 回调函数。将Init操作操作返回的handle添加到密钥管理系统的回调。 |
Z
zhangcheng 已提交
1719 1720


C
CheungVane 已提交
1721
## huks.initSession<sup>9+</sup>
Z
zhangcheng 已提交
1722 1723 1724

initSession(keyAlias: string, options: HuksOptions) : Promise\<HuksSessionHandle>

1725
initSession操作密钥接口,使用Promise方式异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。
Z
zhangcheng 已提交
1726

1727
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
1728 1729 1730 1731 1732 1733 1734

**参数:**

| 参数名   | 类型                                              | 必填 | 说明                                             |
| -------- | ------------------------------------------------- | ---- | ------------------------------------------------ |
| keyAlias | string                                            | 是   | Init操作密钥的别名。                             |
| options  | [HuksOptions](#huksoptions)                       | 是   | Init参数集合。                                   |
C
CheungVane 已提交
1735 1736 1737 1738 1739 1740

**返回值**

| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
| Promise\<[HuksSessionHandle](#hukssessionhandle)> | Promise对象。将Init操作返回的handle添加到密钥管理系统的回调。 |
Z
zhangcheng 已提交
1741

C
CheungVane 已提交
1742
## huks.update<sup>(deprecated)</sup>
Z
zhangcheng 已提交
1743 1744 1745

update(handle: number, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void

1746
update操作密钥接口,使用Callback回调异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。
Z
zhangcheng 已提交
1747

1748 1749
>  **说明:** 从API Version 9开始废弃,建议使用[huks.updateSession<sup>9+</sup>](#huksupdatesession9)替代。

Z
zhangcheng 已提交
1750 1751 1752 1753 1754 1755 1756 1757
**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                                      | 必填 | 说明                                         |
| -------- | ----------------------------------------- | ---- | -------------------------------------------- |
| handle   | number                                    | 是   | Update操作的handle。                         |
| options  | [HuksOptions](#huksoptions)               | 是   | Update的参数集合。                           |
C
CheungVane 已提交
1758
| callback | AsyncCallback\<[HuksResult](#huksresult)> | 是   | 回调函数。将Update操作的结果添加到密钥管理系统的回调。 |
V
Vincentchenhao 已提交
1759

C
CheungVane 已提交
1760
## huks.update<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1761

Z
zhangcheng 已提交
1762
update(handle: number, options: HuksOptions,  token: Uint8Array,  callback: AsyncCallback\<HuksResult>) : void
V
Vincentchenhao 已提交
1763

1764
update操作密钥接口,使用Callback回调异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。
S
shuyi 已提交
1765

1766
>  **说明:** 从API Version 9开始废弃,建议使用[huks.updateSession<sup>9+</sup>](#huksupdatesession9-1)替代。
V
Vincentchenhao 已提交
1767

1768
**系统能力**: SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1769

1770
**参数:**
V
Vincentchenhao 已提交
1771

1772 1773 1774 1775 1776
| 参数名   | 类型                                      | 必填 | 说明                                         |
| -------- | ----------------------------------------- | ---- | -------------------------------------------- |
| handle   | number                                    | 是   | Update操作的handle。                         |
| token    | Uint8Array                                | 否   | Update操作的token。                          |
| options  | [HuksOptions](#huksoptions)               | 是   | Update操作的参数集合。                       |
C
CheungVane 已提交
1777
| callback | AsyncCallback\<[HuksResult](#huksresult)> | 是   | 回调函数。将Update操作的结果添加到密钥管理系统的回调。 |
V
Vincentchenhao 已提交
1778

C
CheungVane 已提交
1779
## huks.update<sup>(deprecated)</sup>
Z
zengyawen 已提交
1780

Z
zhangcheng 已提交
1781
update(handle: number, options: HuksOptions, token?: Uint8Array) : Promise\<HuksResult>
V
Vincentchenhao 已提交
1782

1783
update操作密钥接口,使用Promise方式异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。
V
Vincentchenhao 已提交
1784

1785
>  **说明:** 从API Version 9开始废弃,建议使用[huks.updateSession<sup>9+</sup>](#huksupdatesession9-2)替代。
1786 1787

**系统能力**: SystemCapability.Security.Huks
S
shuyi 已提交
1788

V
Vincentchenhao 已提交
1789 1790
**参数:**

1791 1792 1793 1794 1795
| 参数名  | 类型                                | 必填 | 说明                                         |
| ------- | ----------------------------------- | ---- | -------------------------------------------- |
| handle  | number                              | 是   | Update操作的handle。                         |
| token   | Uint8Array                          | 否   | Update操作的token。                          |
| options | [HuksOptions](#huksoptions)         | 是   | Update操作的参数集合。                       |
C
CheungVane 已提交
1796 1797 1798 1799 1800 1801

**返回值**

| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
| Promise\<[HuksResult](#huksresult)> | Promise对象。将Update操作的结果添加到密钥管理系统的回调。 |
1802

C
CheungVane 已提交
1803
## huks.updateSession<sup>9+</sup>
1804

Z
zhangcheng 已提交
1805
updateSession(handle: number, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void
1806

1807
updateSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。
1808 1809 1810 1811

**系统能力**:SystemCapability.Security.Huks

**参数:**
V
Vincentchenhao 已提交
1812

Z
zhangcheng 已提交
1813 1814 1815 1816
| 参数名   | 类型                                                 | 必填 | 说明                                         |
| -------- | ---------------------------------------------------- | ---- | -------------------------------------------- |
| handle   | number                                               | 是   | Update操作的handle。                         |
| options  | [HuksOptions](#huksoptions)                          | 是   | Update的参数集合。                           |
C
CheungVane 已提交
1817
| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult)> | 是   | 回调函数。将Update操作的结果添加到密钥管理系统的回调。 |
1818 1819


C
CheungVane 已提交
1820
## huks.updateSession<sup>9+</sup>
1821

Z
zhangcheng 已提交
1822
updateSession(handle: number, options: HuksOptions, token: Uint8Array, callback: AsyncCallback\<HuksReturnResult>) : void
1823

1824
updateSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。
1825 1826 1827 1828 1829

**系统能力**:SystemCapability.Security.Huks

**参数:**

Z
zhangcheng 已提交
1830 1831 1832 1833 1834
| 参数名   | 类型                                                 | 必填 | 说明                                         |
| -------- | ---------------------------------------------------- | ---- | -------------------------------------------- |
| handle   | number                                               | 是   | Update操作的handle。                         |
| options  | [HuksOptions](#huksoptions)                          | 是   | Update操作的参数集合。                       |
| token    | Uint8Array                                           | 是   | Update操作的token。                          |
C
CheungVane 已提交
1835
| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult)> | 是   | 回调函数。将Update操作的结果添加到密钥管理系统的回调。 |
1836

C
CheungVane 已提交
1837
## huks.updateSession<sup>9+</sup>
1838

Z
zhangcheng 已提交
1839
updateSession(handle: number, options: HuksOptions, token?: Uint8Array) : Promise\<HuksReturnResult>
1840

1841
uupdateSession操作密钥接口,使用Promise方式异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。
1842 1843 1844 1845 1846

**系统能力**:SystemCapability.Security.Huks

**参数:**

Z
zhangcheng 已提交
1847 1848 1849 1850 1851
| 参数名  | 类型                                           | 必填 | 说明                                         |
| ------- | ---------------------------------------------- | ---- | -------------------------------------------- |
| handle  | number                                         | 是   | Update操作的handle。                         |
| options | [HuksOptions](#huksoptions)                    | 是   | Update操作的参数集合。                       |
| token   | Uint8Array                                     | 否   | Update操作的token。                          |
C
CheungVane 已提交
1852 1853 1854 1855 1856 1857

**返回值**

| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
| Promise<[HuksReturnResult](#huksreturnresult)> | Promise对象。将Update操作的结果添加到密钥管理系统的回调。 |
V
Vincentchenhao 已提交
1858

C
CheungVane 已提交
1859
## huks.finish<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1860

Z
zengyawen 已提交
1861
finish(handle: number, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void
V
Vincentchenhao 已提交
1862

1863
finish操作密钥接口,使用Callback回调异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。
S
shuyi 已提交
1864

Z
zhangcheng 已提交
1865 1866
>  **说明:** 从API Version 9开始废弃,建议使用[huks.finishSession<sup>9+</sup>](#huksfinishsession9)替代。

1867
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1868 1869 1870 1871 1872

**参数:**

| 参数名   | 类型                   | 必填 | 说明                                  |
| -------- | ---------------------- | ---- | ------------------------------------- |
S
shuyi 已提交
1873 1874
| handle | number           | 是   | Finish操作的handle。 |
| options  | [HuksOptions](#huksoptions) | 是   | Finish的参数集合。 |
C
CheungVane 已提交
1875
| callback | AsyncCallback\<[HuksResult](#huksresult)> | 是 | 回调函数。将Finish操作的结果添加到密钥管理系统的回调。 |
V
Vincentchenhao 已提交
1876 1877


C
CheungVane 已提交
1878
## huks.finish<sup>(deprecated)</sup>
Z
zengyawen 已提交
1879 1880

finish(handle: number, options: HuksOptions) : Promise\<HuksResult>
V
Vincentchenhao 已提交
1881

1882
finish操作密钥接口,使用Promise方式异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。
V
Vincentchenhao 已提交
1883

Z
zhangcheng 已提交
1884 1885
>  **说明:** 从API Version 9开始废弃,建议使用[huks.updateSession<sup>9+</sup>](#huksfinishsession9-1)替代。

1886
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
1887

V
Vincentchenhao 已提交
1888 1889 1890 1891
**参数:**

| 参数名   | 类型                   | 必填 | 说明                                  |
| -------- | ---------------------- | ---- | ------------------------------------- |
S
shuyi 已提交
1892 1893
| handle | number           | 是   | Finish操作的handle。 |
| options  | [HuksOptions](#huksoptions) | 是   | Finish操作的参数集合。 |
C
CheungVane 已提交
1894 1895 1896 1897 1898 1899

**返回值**

| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
| Promise\<[HuksResult](#huksresult)> | Promise对象,用于获取异步返回结果。 |
V
Vincentchenhao 已提交
1900

C
CheungVane 已提交
1901
## huks.finishSession<sup>9+</sup>
1902

Z
zhangcheng 已提交
1903
finishSession(handle: number, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void
1904

1905
finishSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。
1906 1907 1908 1909 1910

**系统能力**:SystemCapability.Security.Huks

**参数:**

Z
zhangcheng 已提交
1911 1912 1913 1914 1915
| 参数名   | 类型                                                 | 必填 | 说明                                         |
| -------- | ---------------------------------------------------- | ---- | -------------------------------------------- |
| handle   | number                                               | 是   | Finish操作的handle。                         |
| options  | [HuksOptions](#huksoptions)                          | 是   | Finish的参数集合。                           |
| token    | Uint8Array                                           | 是   | Finish操作的token。                          |
C
CheungVane 已提交
1916
| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult)> | 是   | 回调函数。将Finish操作的结果添加到密钥管理系统的回调。 |
1917

C
CheungVane 已提交
1918
## huks.finishSession<sup>9+</sup>
1919

Z
zhangcheng 已提交
1920
finishSession(handle: number, options: HuksOptions, token: Uint8Array, callback: AsyncCallback\<HuksReturnResult>) : void
1921

1922
finishSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。
1923

Z
zhangcheng 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932
**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                                                  | 必填 | 说明                                         |
| -------- | ----------------------------------------------------- | ---- | -------------------------------------------- |
| handle   | number                                                | 是   | Finish操作的handle。                         |
| options  | [HuksOptions](#huksoptions)                           | 是   | Finish的参数集合。                           |
| token    | Uint8Array                                            | 是   | Finish操作的token。                          |
C
CheungVane 已提交
1933
| callback | AsyncCallback\<[HuksReturnResult](#huksreturnresult)> | 是   | 回调函数。将Finish操作的结果添加到密钥管理系统的回调。 |
Z
zhangcheng 已提交
1934 1935


C
CheungVane 已提交
1936
## huks.finishSession<sup>9+</sup>
Z
zhangcheng 已提交
1937 1938 1939

finishSession(handle: number, options: HuksOptions, token?: Uint8Array) : Promise\<HuksReturnResult>

1940
finishSession操作密钥接口,使用Promise方式异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。
1941 1942 1943 1944 1945

**系统能力**:SystemCapability.Security.Huks

**参数:**

Z
zhangcheng 已提交
1946 1947 1948 1949 1950
| 参数名  | 类型                                            | 必填 | 说明                                |
| ------- | ----------------------------------------------- | ---- | ----------------------------------- |
| handle  | number                                          | 是   | Finish操作的handle。                |
| options | [HuksOptions](#huksoptions)                     | 是   | Finish操作的参数集合。              |
| token   | Uint8Array                                      | 否   | Finish操作的token。                 |
C
CheungVane 已提交
1951 1952 1953 1954 1955 1956

**返回值**

| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
| Promise\<[HuksReturnResult](#huksreturnresult)> | Promise对象,用于获取异步返回结果。 |
V
Vincentchenhao 已提交
1957

C
CheungVane 已提交
1958
## huks.abort<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1959

Z
zengyawen 已提交
1960
abort(handle: number, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void
V
Vincentchenhao 已提交
1961

1962
abort操作密钥接口,使用Callback回调异步返回结果。
S
shuyi 已提交
1963

Z
zhangcheng 已提交
1964 1965
>  **说明:** 从API Version 9开始废弃,建议使用[huks.abortSession<sup>9+</sup>](#huksabortsession9)替代。

1966
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1967 1968 1969 1970 1971

**参数:**

| 参数名   | 类型                   | 必填 | 说明                                  |
| -------- | ---------------------- | ---- | ------------------------------------- |
S
shuyi 已提交
1972 1973
| handle | number           | 是   | Abort操作的handle。 |
| options  | [HuksOptions](#huksoptions) | 是   | Abort操作的参数集合。 |
C
CheungVane 已提交
1974
| callback | AsyncCallback\<[HuksResult](#huksresult)> | 是 | 回调函数。将Abort操作的结果添加到密钥管理系统的回调。 |
V
Vincentchenhao 已提交
1975 1976 1977 1978

**示例:**

```js
S
shuyi 已提交
1979 1980 1981 1982 1983
/* huks.init, huks.update, huks.finish为三段式接口,需要一起使用,当huks.init和huks.update
 * 以及huks.finish操作中的任一阶段发生错误时,都需要调用huks.abort来终止密钥的使用。
 *
 * 以下以RSA1024密钥的callback操作使用为例
 */
S
shuyi 已提交
1984
var keyalias = "HuksDemoRSA";
S
shuyi 已提交
1985 1986 1987 1988 1989
var properties = new Array();
var options = {
  properties: properties,
  inData: new Uint8Array(0)
};
S
shuyi 已提交
1990
var handle;
S
shuyi 已提交
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
var resultMessage = "";
async function generateKey() {
  properties[0] = {
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS_ALG_RSA
  };
  properties[1] = {
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
    value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024
  };
  properties[2] = {
    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT
  };
  properties[3] = {
    tag: huks.HuksTag.HUKS_TAG_PADDING,
    value: huks.HuksKeyPadding.HUKS_PADDING_OAEP
  };
  properties[4] = {
    tag: huks.HuksTag.HUKS_TAG_DIGEST,
    value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
  };
S
shuyi 已提交
2013
  huks.generateKey(keyalias, options);
S
shuyi 已提交
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
}
function stringToUint8Array(str) {
  var arr = [];
  for (var i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  var tmpUint8Array = new Uint8Array(arr);
  return tmpUint8Array;
}
async function huksInit() {
S
shuyi 已提交
2024
  await huks.init(keyalias, options).then((data) => {
S
shuyi 已提交
2025
    console.log(`test init data: ${JSON.stringify(data)}`);
S
shuyi 已提交
2026
    handle = data.handle;
S
shuyi 已提交
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
  }).catch((err) => {
    console.log("test init err information: " + JSON.stringify(err))
  })
}
async function huksUpdate() {
    options.inData = stringToUint8Array("huksHmacTest");
    await huks.update(handle, options).then((data) => {
      if (data.errorCode === 0) {
        resultMessage += "update success!";
      } else {
        resultMessage += "update fail!";
      }
    });
    console.log(resultMessage);
}
function huksFinish() {
  options.inData = stringToUint8Array("HuksDemoHMAC");
  huks.finish(handle, options).then((data) => {
    if (data.errorCode === 0) {
      resultMessage = "finish success!";
    } else {
      resultMessage = "finish fail errorCode: " + data.errorCode;
    }
  }).catch((err) => {
    resultMessage = "finish fail, catch errorMessage:" + JSON.stringify(err)
  });
  console.log(resultMessage);
}
async function huksAbort() {
  huks.abort(handle, options).then((data) => {
    if (data.errorCode === 0) {
      resultMessage = "abort success!";
    } else {
      resultMessage = "abort fail errorCode: " + data.errorCode;
    }
  }).catch((err) => {
    resultMessage = "abort fail, catch errorMessage:" + JSON.stringify(err)
  });
  console.log(resultMessage);
}
V
Vincentchenhao 已提交
2067 2068
```

C
CheungVane 已提交
2069
## huks.abort<sup>(deprecated)</sup>
Z
zengyawen 已提交
2070 2071

abort(handle: number, options: HuksOptions) : Promise\<HuksResult>;
V
Vincentchenhao 已提交
2072

S
shuyi 已提交
2073
abort操作密钥接口,使用Promise方式异步返回结果。
V
Vincentchenhao 已提交
2074

Z
zhangcheng 已提交
2075 2076
>  **说明:** 从API Version 9开始废弃,建议使用[huks.abortSession<sup>9+</sup>](#huksabortsession9-1)替代。

2077
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
2078

V
Vincentchenhao 已提交
2079 2080 2081 2082
**参数:**

| 参数名   | 类型                   | 必填 | 说明                                  |
| -------- | ---------------------- | ---- | ------------------------------------- |
S
shuyi 已提交
2083 2084
| handle | number           | 是   | Abort操作的handle。 |
| options  | [HuksOptions](#huksoptions) | 是   | Abort操作的参数集合。 |
C
CheungVane 已提交
2085 2086 2087 2088 2089 2090

**返回值**

| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
| Promise\<[HuksResult](#huksresult)> | Promise对象。将Abort操作的结果添加到密钥管理系统的回调。 |
V
Vincentchenhao 已提交
2091 2092 2093 2094

**示例:**

```js
S
shuyi 已提交
2095 2096 2097 2098 2099
/* huks.init, huks.update, huks.finish为三段式接口,需要一起使用,当huks.init和huks.update
 * 以及huks.finish操作中的任一阶段发生错误时,都需要调用huks.abort来终止密钥的使用。
 *
 * 以下以RSA1024密钥的promise操作使用为例
 */
S
shuyi 已提交
2100
var keyalias = "HuksDemoRSA";
S
shuyi 已提交
2101 2102 2103 2104 2105
var properties = new Array();
var options = {
  properties: properties,
  inData: new Uint8Array(0)
};
S
shuyi 已提交
2106
var handle;
S
shuyi 已提交
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
var resultMessage = "";
function stringToUint8Array(str) {
  var arr = [];
  for (var i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  var tmpUint8Array = new Uint8Array(arr);
  return tmpUint8Array;
}

async function generateKey() {
  properties[0] = {
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
    value: huks.HuksKeyAlg.HUKS_ALG_RSA
  };
  properties[1] = {
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
    value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024
  };
  properties[2] = {
    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT
  };
  properties[3] = {
    tag: huks.HuksTag.HUKS_TAG_PADDING,
    value: huks.HuksKeyPadding.HUKS_PADDING_OAEP
  };
  properties[4] = {
    tag: huks.HuksTag.HUKS_TAG_DIGEST,
    value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
  };
S
shuyi 已提交
2138
  huks.generateKey(keyalias, options, function (err, data) { });
S
shuyi 已提交
2139 2140 2141
}
async function huksInit() {
  return new Promise((resolve, reject) => {
S
shuyi 已提交
2142
    huks.init(keyalias, options, async function (err, data) {
S
shuyi 已提交
2143
      if (data.errorCode === 0) {
S
shuyi 已提交
2144 2145
        resultMessage = "init success!"
        handle = data.handle;
S
shuyi 已提交
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
      } else {
        resultMessage = "init fail errorCode: " + data.errorCode
      }
    });
  });
}

async function huksUpdate() {
    options.inData = stringToUint8Array("huksHmacTest");
    new Promise((resolve, reject) => {
      huks.update(handle, options, function (err, data) {
        if (data.errorCode === 0) {
          resultMessage += "update success!";
        } else {
          resultMessage += "update fail!";
S
shuyi 已提交
2161
        }
S
shuyi 已提交
2162 2163 2164
      });
    });
    console.log(resultMessage);
S
shuyi 已提交
2165

S
shuyi 已提交
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
}

async function huksFinish() {
  options.inData = stringToUint8Array("0");
  new Promise((resolve, reject) => {
    huks.finish(handle, options, function (err, data) {
      if (data.errorCode === 0) {
        resultMessage = "finish success!";
      } else {
        resultMessage =  "finish fail errorCode: " + data.errorCode;
      }
    });
  });
}

function huksAbort() {
  new Promise((resolve, reject) => {
    huks.abort(handle, options, function (err, data) {
      console.log(`Huks_Demo hmac huksAbort1 data ${JSON.stringify(data)}`);
      console.log(`Huks_Demo hmac huksAbort1 err ${JSON.stringify(err)}`);
    });
  });
}
Z
zhangcheng 已提交
2189 2190
```

C
CheungVane 已提交
2191
## huks.abortSession<sup>9+</sup>
Z
zhangcheng 已提交
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204

abortSession(handle: number, options: HuksOptions, callback: AsyncCallback\<void>) : void

abort操作密钥接口,使用Callback回调异步返回结果 。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名   | 类型                        | 必填 | 说明                                        |
| -------- | --------------------------- | ---- | ------------------------------------------- |
| handle   | number                      | 是   | Abort操作的handle。                         |
| options  | [HuksOptions](#huksoptions) | 是   | Abort操作的参数集合。                       |
C
CheungVane 已提交
2205
| callback | AsyncCallback\<void>        | 是   | 回调函数。将Abort操作的结果添加到密钥管理系统的回调。 |
Z
zhangcheng 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335

**示例:**

```js
/* huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用,当
 * huks.initSession和huks.updateSession
 * 以及huks.finishSession操作中的任一阶段发生错误时,
 * 都需要调用huks.abortSession来终止密钥的使用。
 *
 * 以下以RSA1024密钥的callback功能使用为例
 */
function stringToUint8Array(str) {
    var arr = [];
    for (var i = 0, j = str.length; i < j; ++i) {
        arr.push(str.charCodeAt(i));
    }
    var tmpUint8Array = new Uint8Array(arr);
    return tmpUint8Array;
}

var keyAlias = "HuksDemoRSA";
var properties = new Array();
var options = {
    properties: properties,
    inData: new Uint8Array(0)
};
var handle;
async function generateKey() {
    properties[0] = {
        tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
        value: huks.HuksKeyAlg.HUKS_ALG_RSA
    };
    properties[1] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
        value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024
    };
    properties[2] = {
        tag: huks.HuksTag.HUKS_TAG_PURPOSE,
        value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT
    };
    properties[3] = {
        tag: huks.HuksTag.HUKS_TAG_PADDING,
        value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5
    };
    properties[4] = {
        tag: huks.HuksTag.HUKS_TAG_DIGEST,
        value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
    };
    properties[5] = {
        tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
        value: huks.HuksCipherMode.HUKS_MODE_ECB,
    }

    try {
        await huks.generateKeyItem(keyAlias, options, function (error, data) {
            if (error) {
                console.error(`callback: generateKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            } else {
                console.info(`callback: generateKeyItem success`);
            }
        });
    } catch (error) {
        console.error(`callback: generateKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}

async function huksInit() {
    console.log('enter huksInit');
    try {
        huks.initSession(keyAlias, options, function (error, data) {
            if (error) {
                console.error(`callback: initSession failed, code: ${error.code}, msg: ${error.message}`);
            } else {
                console.info(`callback: initSession success, data = ${JSON.stringify(data)}`);
                handle = data.handle;
            }
        });
    } catch (error) {
        console.error(`callback: initSession input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}

async function huksUpdate() {
    console.log('enter huksUpdate');
    options.inData = stringToUint8Array("huksHmacTest");
    try {
        huks.updateSession(handle, options, function (error, data) {
            if (error) {
                console.error(`callback: updateSession failed, code: ${error.code}, msg: ${error.message}`);
            } else {
                console.info(`callback: updateSession success, data = ${JSON.stringify(data)}`);
            }
        });
    } catch (error) {
        console.error(`callback: updateSession input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}

async function huksFinish() {
    console.log('enter huksFinish');
    options.inData = new Uint8Array(0);
    try {
        huks.finishSession(handle, options, function (error, data) {
            if (error) {
                console.error(`callback: finishSession failed, code: ${error.code}, msg: ${error.message}`);
            } else {
                console.info(`callback: finishSession success, data = ${JSON.stringify(data)}`);
            }
        });
    } catch (error) {
        console.error(`callback: finishSession input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}

async function huksAbort() {
    console.log('enter huksAbort');
    try {
        huks.abortSession(handle, options, function (error, data) {
            if (error) {
                console.error(`callback: abortSession failed, code: ${error.code}, msg: ${error.message}`);
            } else {
                console.info(`callback: abortSession success`);
            }
        });
    } catch (error) {
        console.error(`callback: abortSession input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}
```

C
CheungVane 已提交
2336
## huks.abortSession<sup>9+</sup>
Z
zhangcheng 已提交
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349

abortSession(handle: number, options: HuksOptions) : Promise\<void>;

abort操作密钥接口,使用Promise方式异步返回结果。

**系统能力**:SystemCapability.Security.Huks

**参数:**

| 参数名  | 类型                        | 必填 | 说明                                        |
| ------- | --------------------------- | ---- | ------------------------------------------- |
| handle  | number                      | 是   | Abort操作的handle。                         |
| options | [HuksOptions](#huksoptions) | 是   | Abort操作的参数集合。                       |
C
CheungVane 已提交
2350 2351 2352 2353 2354 2355

**返回值**

| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
| Promise\<void>             | Promise对象。将Abort操作的结果添加到密钥管理系统的回调。 |
Z
zhangcheng 已提交
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408

**示例:**

```js
/* huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用,当
 * huks.initSession和huks.updateSession
 * 以及huks.finishSession操作中的任一阶段发生错误时,
 * 都需要调用huks.abortSession来终止密钥的使用。
 *
 * 以下以RSA1024密钥的callback功能使用为例
 */
function stringToUint8Array(str) {
    var arr = [];
    for (var i = 0, j = str.length; i < j; ++i) {
        arr.push(str.charCodeAt(i));
    }
    var tmpUint8Array = new Uint8Array(arr);
    return tmpUint8Array;
}

var keyAlias = "HuksDemoRSA";
var properties = new Array();
var options = {
    properties: properties,
    inData: new Uint8Array(0)
};
var handle;
async function generateKey() {
    properties[0] = {
        tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
        value: huks.HuksKeyAlg.HUKS_ALG_RSA
    };
    properties[1] = {
        tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
        value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024
    };
    properties[2] = {
        tag: huks.HuksTag.HUKS_TAG_PURPOSE,
        value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT
    };
    properties[3] = {
        tag: huks.HuksTag.HUKS_TAG_PADDING,
        value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5
    };
    properties[4] = {
        tag: huks.HuksTag.HUKS_TAG_DIGEST,
        value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
    };
    properties[5] = {
        tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
        value: huks.HuksCipherMode.HUKS_MODE_ECB,
    }

2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
    try {
        await huks.generateKeyItem(keyAlias, options)
            .then((data) => {
                console.info(`promise: generateKeyItem success`);
            })
            .catch(error => {
                console.error(`promise: generateKeyItem failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`promise: generateKeyItem input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}

async function huksInit() {
    console.log('enter huksInit');
    try {
        await huks.initSession(keyAlias, options)
            .then ((data) => {
                console.info(`promise: initSession success, data = ${JSON.stringify(data)}`);
                    handle = data.handle;
            })
            .catch(error => {
                console.error(`promise: initSession key failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`promise: initSession input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}

async function huksUpdate() {
    console.log('enter huksUpdate');
    options.inData = stringToUint8Array("huksHmacTest");
    try {
        await huks.updateSession(handle, options)
            .then ((data) => {
                console.info(`promise: updateSession success, data = ${JSON.stringify(data)}`);
            })
            .catch(error => {
                console.error(`promise: updateSession failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`promise: updateSession input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}

async function huksFinish() {
    console.log('enter huksFinish');
    options.inData = new Uint8Array(0);
    try {
        await huks.finishSession(handle, options)
            .then ((data) => {
                console.info(`promise: finishSession success, data = ${JSON.stringify(data)}`);
            })
            .catch(error => {
                console.error(`promise: finishSession failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`promise: finishSession input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}

async function huksAbort() {
    console.log('enter huksAbort');
    try {
        await huks.abortSession(keyAlias, options)
            .then ((data) => {
                console.info(`promise: abortSession success`);
            })
            .catch(error => {
                console.error(`promise: abortSession failed, code: ${error.code}, msg: ${error.message}`);
            });
    } catch (error) {
        console.error(`promise: abortSession input arg invalid, code: ${error.code}, msg: ${error.message}`);
    }
}
```

C
CheungVane 已提交
2486
## HuksErrorCode<sup>(deprecated)</sup>
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568

表示错误码的枚举。

**系统能力**:SystemCapability.Security.Huks

| 名称                       | 值    | 说明 |
| -------------------------- | ----- | ---- |
| HUKS_SUCCESS | 0     |表示成功。|
| HUKS_FAILURE | -1    |表示失败。|
| HUKS_ERROR_BAD_STATE | -2    |表示错误的状态。|
| HUKS_ERROR_INVALID_ARGUMENT | -3    |表示无效的数据。|
| HUKS_ERROR_NOT_SUPPORTED | -4    |表示不支持。|
| HUKS_ERROR_NO_PERMISSION | -5    |表示没有许可。|
| HUKS_ERROR_INSUFFICIENT_DATA | -6    |表示数据不足。|
| HUKS_ERROR_BUFFER_TOO_SMALL | -7    |表示缓冲区太小。|
| HUKS_ERROR_INSUFFICIENT_MEMORY | -8    |表示内存不足。|
| HUKS_ERROR_COMMUNICATION_FAILURE | -9    |表示通讯失败。|
| HUKS_ERROR_STORAGE_FAILURE | -10   |表示存储故障。|
| HUKS_ERROR_HARDWARE_FAILURE | -11   |表示硬件故障。|
| HUKS_ERROR_ALREADY_EXISTS | -12   |表示已经存在。|
| HUKS_ERROR_NOT_EXIST | -13   |表示不存在。|
| HUKS_ERROR_NULL_POINTER | -14   |表示空指针。|
| HUKS_ERROR_FILE_SIZE_FAIL | -15   |表示文件大小失败。|
| HUKS_ERROR_READ_FILE_FAIL | -16   |表示读取文件失败。|
| HUKS_ERROR_INVALID_PUBLIC_KEY | -17   |表示无效的公钥。|
| HUKS_ERROR_INVALID_PRIVATE_KEY | -18   |表示无效的私钥。|
| HUKS_ERROR_INVALID_KEY_INFO | -19   |表示无效的密钥信息。|
| HUKS_ERROR_HASH_NOT_EQUAL | -20   |表示哈希不相等。|
| HUKS_ERROR_MALLOC_FAIL | -21   |表示MALLOC 失败。|
| HUKS_ERROR_WRITE_FILE_FAIL | -22   |表示写文件失败。|
| HUKS_ERROR_REMOVE_FILE_FAIL | -23   |表示删除文件失败。|
| HUKS_ERROR_OPEN_FILE_FAIL | -24   |表示打开文件失败。|
| HUKS_ERROR_CLOSE_FILE_FAIL | -25   |表示关闭文件失败。|
| HUKS_ERROR_MAKE_DIR_FAIL | -26   |表示创建目录失败。|
| HUKS_ERROR_INVALID_KEY_FILE | -27   |表示无效的密钥文件。|
| HUKS_ERROR_IPC_MSG_FAIL | -28   |表示IPC 信息失败。|
| HUKS_ERROR_REQUEST_OVERFLOWS | -29   |表示请求溢出。|
| HUKS_ERROR_PARAM_NOT_EXIST | -30   |表示参数不存在。|
| HUKS_ERROR_CRYPTO_ENGINE_ERROR | -31   |表示CRYPTO ENGINE错误。|
| HUKS_ERROR_COMMUNICATION_TIMEOUT | -32   |表示通讯超时。|
| HUKS_ERROR_IPC_INIT_FAIL | -33   |表示IPC 初始化失败。|
| HUKS_ERROR_IPC_DLOPEN_FAIL | -34   |表示IPC DLOPEN 失败。|
| HUKS_ERROR_EFUSE_READ_FAIL | -35   |表示EFUSE 读取失败。|
| HUKS_ERROR_NEW_ROOT_KEY_MATERIAL_EXIST | -36   |表示存在新的根密钥材料。|
| HUKS_ERROR_UPDATE_ROOT_KEY_MATERIAL_FAIL | -37   |表示更新根密钥材料失败。|
| HUKS_ERROR_VERIFICATION_FAILED | -38   |表示验证证书链失败。|
| HUKS_ERROR_GET_USERIAM_SECINFO_FAILED<sup>9+</sup> | -40 |表示获取当前用户安全属性信息失败。|
| HUKS_ERROR_GET_USERIAM_AUTHINFO_FAILED<sup>9+</sup> | -41 |表示获取当前用户认证信息失败。|
| HUKS_ERROR_USER_AUTH_TYPE_NOT_SUPPORT<sup>9+</sup> | -42 |表示不支持当前用户认证类型的访问控制。|
| HUKS_ERROR_KEY_AUTH_FAILED<sup>9+</sup> | -43 |表示安全访问控制认证失败。|
| HUKS_ERROR_DEVICE_NO_CREDENTIAL<sup>9+</sup> | -44 |表示设备当前未录入凭据。|
| HUKS_ERROR_CHECK_GET_ALG_FAIL | -100  |表示检查获取 ALG 失败。|
| HUKS_ERROR_CHECK_GET_KEY_SIZE_FAIL | -101  |表示检查获取密钥大小失败。|
| HUKS_ERROR_CHECK_GET_PADDING_FAIL | -102  |表示检查获取填充失败。|
| HUKS_ERROR_CHECK_GET_PURPOSE_FAIL | -103  |表示检查获取目的失败。|
| HUKS_ERROR_CHECK_GET_DIGEST_FAIL | -104  |表示检查获取摘要失败。|
| HUKS_ERROR_CHECK_GET_MODE_FAIL | -105  |表示检查获取模式失败。|
| HUKS_ERROR_CHECK_GET_NONCE_FAIL | -106  |表示检查获取随机数失败。|
| HUKS_ERROR_CHECK_GET_AAD_FAIL | -107  |表示检查获取 AAD 失败。|
| HUKS_ERROR_CHECK_GET_IV_FAIL | -108  |表示检查 GET IV 失败。|
| HUKS_ERROR_CHECK_GET_AE_TAG_FAIL | -109  |表示检查获取 AE 标记失败。|
| HUKS_ERROR_CHECK_GET_SALT_FAIL | -110  |表示检查获取SALT失败。|
| HUKS_ERROR_CHECK_GET_ITERATION_FAIL | -111  |表示检查获取迭代失败。|
| HUKS_ERROR_INVALID_ALGORITHM | -112  |表示无效的算法。|
| HUKS_ERROR_INVALID_KEY_SIZE | -113  |表示无效的密钥大小。|
| HUKS_ERROR_INVALID_PADDING | -114  |表示无效的填充。|
| HUKS_ERROR_INVALID_PURPOSE | -115  |表示无效的目的。|
| HUKS_ERROR_INVALID_MODE | -116  |表示无效模式。|
| HUKS_ERROR_INVALID_DIGEST | -117  |表示无效的摘要。|
| HUKS_ERROR_INVALID_SIGNATURE_SIZE | -118  |表示签名大小无效。|
| HUKS_ERROR_INVALID_IV | -119  |表示无效的 IV。|
| HUKS_ERROR_INVALID_AAD | -120  |表示无效的 AAD。|
| HUKS_ERROR_INVALID_NONCE | -121  |表示无效的随机数。|
| HUKS_ERROR_INVALID_AE_TAG | -122  |表示无效的 AE 标签。|
| HUKS_ERROR_INVALID_SALT | -123  |表示无效SALT。|
| HUKS_ERROR_INVALID_ITERATION | -124  |表示无效的迭代。|
| HUKS_ERROR_INVALID_OPERATION | -125  |表示无效操作。|
| HUKS_ERROR_INVALID_WRAPPED_FORMAT<sup>9+</sup> | -126 |表示导入加密密钥时,密钥格式错误。|
| HUKS_ERROR_INVALID_USAGE_OF_KEY<sup>9+</sup> | -127 |表示导入加密密钥时,密钥用途错误。|
| HUKS_ERROR_INTERNAL_ERROR | -999  |表示内部错误。|
| HUKS_ERROR_UNKNOWN_ERROR | -1000 |表示未知错误。|

C
CheungVane 已提交
2569
## HuksExceptionErrCode<sup>9+</sup>
2570 2571 2572

表示错误码的枚举以及对应的错误信息, 错误码表示错误类型,错误信息展示错误详情。

2573 2574
关于错误码的具体信息,可在[错误码参考文档](../errorcodes/errorcode-huks.md)中查看。

2575 2576
**系统能力**:SystemCapability.Security.Huks

C
CheungVane 已提交
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
| 名称                                           | 说明                        | 错误码   |
| ---------------------------------------------- | --------------------------- | -------- |
| HUKS_ERR_CODE_PERMISSION_FAIL                  | 权限错误导致失败。          | 201      |
| HUKS_ERR_CODE_ILLEGAL_ARGUMENT                 | 参数错误导致失败。          | 401      |
| HUKS_ERR_CODE_NOT_SUPPORTED_API                | 不支持的API。               | 801      |
| HUKS_ERR_CODE_FEATURE_NOT_SUPPORTED            | 不支持的功能/特性。         | 12000001 |
| HUKS_ERR_CODE_MISSING_CRYPTO_ALG_ARGUMENT      | 缺少密钥算法参数。          | 12000002 |
| HUKS_ERR_CODE_INVALID_CRYPTO_ALG_ARGUMENT      | 无效密钥算法参数。          | 12000003 |
| HUKS_ERR_CODE_FILE_OPERATION_FAIL              | 文件操作失败。              | 12000004 |
| HUKS_ERR_CODE_COMMUNICATION_FAIL               | 通信失败。                  | 12000005 |
| HUKS_ERR_CODE_CRYPTO_FAIL                      | 算法库操作失败。            | 12000006 |
| HUKS_ERR_CODE_KEY_AUTH_PERMANENTLY_INVALIDATED | 密钥访问失败-密钥访问失效。 | 12000007 |
| HUKS_ERR_CODE_KEY_AUTH_VERIFY_FAILED           | 密钥访问失败-密钥认证失败。 | 12000008 |
| HUKS_ERR_CODE_KEY_AUTH_TIME_OUT                | 密钥访问失败-密钥访问超时。 | 12000009 |
| HUKS_ERR_CODE_SESSION_LIMIT                    | 密钥操作会话数已达上限。    | 12000010 |
| HUKS_ERR_CODE_ITEM_NOT_EXIST                   | 目标对象不存在。            | 12000011 |
| HUKS_ERR_CODE_EXTERNAL_ERROR                   | 外部错误。                  | 12000012 |
| HUKS_ERR_CODE_CREDENTIAL_NOT_EXIST             | 缺失所需凭据。              | 12000013 |
| HUKS_ERR_CODE_INSUFFICIENT_MEMORY              | 内存不足。                  | 12000014 |
| HUKS_ERR_CODE_CALL_SERVICE_FAILED              | 调用其他系统服务失败。      | 12000015 |
2597

C
CheungVane 已提交
2598
## HuksKeyPurpose
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615

表示密钥用途。

**系统能力**:SystemCapability.Security.Huks

| 名称                     | 值   | 说明                             |
| ------------------------ | ---- | -------------------------------- |
| HUKS_KEY_PURPOSE_ENCRYPT | 1    | 表示密钥用于对明文进行加密操作。 |
| HUKS_KEY_PURPOSE_DECRYPT | 2    | 表示密钥用于对密文进行解密操作。 |
| HUKS_KEY_PURPOSE_SIGN    | 4    | 表示密钥用于对数据进行签名。     |
| HUKS_KEY_PURPOSE_VERIFY  | 8    | 表示密钥用于验证签名后的数据。   |
| HUKS_KEY_PURPOSE_DERIVE  | 16   | 表示密钥用于派生密钥。           |
| HUKS_KEY_PURPOSE_WRAP    | 32   | 表示密钥用于加密导出。           |
| HUKS_KEY_PURPOSE_UNWRAP  | 64   | 表示密钥加密导入。               |
| HUKS_KEY_PURPOSE_MAC     | 128  | 表示密钥用于生成mac消息验证码。  |
| HUKS_KEY_PURPOSE_AGREE   | 256  | 表示密钥用于进行密钥协商。       |

C
CheungVane 已提交
2616
## HuksKeyDigest
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632

表示摘要算法。

**系统能力**:SystemCapability.Security.Huks

| 名称                   | 值   | 说明                                     |
| ---------------------- | ---- | ---------------------------------------- |
| HUKS_DIGEST_NONE       | 0   | 表示无摘要算法。 |
| HUKS_DIGEST_MD5        | 1    | 表示MD5摘要算法。 |
| HUKS_DIGEST_SM3<sup>9+</sup> | 2 | 表示SM3摘要算法。 |
| HUKS_DIGEST_SHA1       | 10   | 表示SHA1摘要算法。 |
| HUKS_DIGEST_SHA224 | 11   | 表示SHA224摘要算法。 |
| HUKS_DIGEST_SHA256 | 12  | 表示SHA256摘要算法。 |
| HUKS_DIGEST_SHA384  | 13  | 表示SHA384摘要算法。 |
| HUKS_DIGEST_SHA512 | 14  | 表示SHA512摘要算法。 |

C
CheungVane 已提交
2633
## HuksKeyPadding
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647

表示补齐算法。

**系统能力**:SystemCapability.Security.Huks

| 名称                   | 值   | 说明                                     |
| ---------------------- | ---- | ---------------------------------------- |
| HUKS_PADDING_NONE | 0    | 表示不使用补齐算法。 |
| HUKS_PADDING_OAEP | 1    | 表示使用OAEP补齐算法。 |
| HUKS_PADDING_PSS | 2    | 表示使用PSS补齐算法。 |
| HUKS_PADDING_PKCS1_V1_5 | 3    | 表示使用PKCS1_V1_5补齐算法。 |
| HUKS_PADDING_PKCS5 | 4   | 表示使用PKCS5补齐算法。 |
| HUKS_PADDING_PKCS7 | 5   | 表示使用PKCS7补齐算法。 |

C
CheungVane 已提交
2648
## HuksCipherMode
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662

表示加密模式。

**系统能力**:SystemCapability.Security.Huks

| 名称          | 值   | 说明                  |
| ------------- | ---- | --------------------- |
| HUKS_MODE_ECB | 1    | 表示使用ECB加密模式。 |
| HUKS_MODE_CBC | 2    | 表示使用CBC加密模式。 |
| HUKS_MODE_CTR | 3    | 表示使用CTR加密模式。 |
| HUKS_MODE_OFB | 4    | 表示使用OFB加密模式。 |
| HUKS_MODE_CCM | 31   | 表示使用CCM加密模式。 |
| HUKS_MODE_GCM | 32   | 表示使用GCM加密模式。 |

C
CheungVane 已提交
2663
## HuksKeySize
2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691

表示密钥长度。

**系统能力**:SystemCapability.Security.Huks

| 名称                               | 值   | 说明                                       |
| ---------------------------------- | ---- | ------------------------------------------ |
| HUKS_RSA_KEY_SIZE_512              | 512  | 表示使用RSA算法的密钥长度为512bit。        |
| HUKS_RSA_KEY_SIZE_768              | 768  | 表示使用RSA算法的密钥长度为768bit。        |
| HUKS_RSA_KEY_SIZE_1024             | 1024 | 表示使用RSA算法的密钥长度为1024bit。       |
| HUKS_RSA_KEY_SIZE_2048             | 2048 | 表示使用RSA算法的密钥长度为2048bit。       |
| HUKS_RSA_KEY_SIZE_3072             | 3072 | 表示使用RSA算法的密钥长度为3072bit。       |
| HUKS_RSA_KEY_SIZE_4096             | 4096 | 表示使用RSA算法的密钥长度为4096bit。       |
| HUKS_ECC_KEY_SIZE_224              | 224  | 表示使用ECC算法的密钥长度为224bit。        |
| HUKS_ECC_KEY_SIZE_256              | 256  | 表示使用ECC算法的密钥长度为256bit。        |
| HUKS_ECC_KEY_SIZE_384              | 384  | 表示使用ECC算法的密钥长度为384bit。        |
| HUKS_ECC_KEY_SIZE_521              | 521  | 表示使用ECC算法的密钥长度为521bit。        |
| HUKS_AES_KEY_SIZE_128              | 128  | 表示使用AES算法的密钥长度为128bit。        |
| HUKS_AES_KEY_SIZE_192              | 196  | 表示使用AES算法的密钥长度为196bit。        |
| HUKS_AES_KEY_SIZE_256              | 256  | 表示使用AES算法的密钥长度为256bit。        |
| HUKS_AES_KEY_SIZE_512              | 512  | 表示使用AES算法的密钥长度为512bit。        |
| HUKS_CURVE25519_KEY_SIZE_256       | 256  | 表示使用CURVE25519算法的密钥长度为256bit。 |
| HUKS_DH_KEY_SIZE_2048              | 2048 | 表示使用DH算法的密钥长度为2048bit。        |
| HUKS_DH_KEY_SIZE_3072              | 3072 | 表示使用DH算法的密钥长度为3072bit。        |
| HUKS_DH_KEY_SIZE_4096              | 4096 | 表示使用DH算法的密钥长度为4096bit。        |
| HUKS_SM2_KEY_SIZE_256<sup>9+</sup> | 256  | 表示SM2算法的密钥长度为256bit。            |
| HUKS_SM4_KEY_SIZE_128<sup>9+</sup> | 128  | 表示SM4算法的密钥长度为128bit。            |

C
CheungVane 已提交
2692
## HuksKeyAlg
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714

表示密钥使用的算法。

**系统能力**:SystemCapability.Security.Huks

| 名称                      | 值   | 说明                  |
| ------------------------- | ---- | --------------------- |
| HUKS_ALG_RSA              | 1    | 表示使用RSA算法。     |
| HUKS_ALG_ECC              | 2    | 表示使用ECC算法。     |
| HUKS_ALG_DSA              | 3    | 表示使用DSA算法。     |
| HUKS_ALG_AES              | 20   | 表示使用AES算法。     |
| HUKS_ALG_HMAC             | 50   | 表示使用HMAC算法。    |
| HUKS_ALG_HKDF             | 51   | 表示使用HKDF算法。    |
| HUKS_ALG_PBKDF2           | 52   | 表示使用PBKDF2算法。  |
| HUKS_ALG_ECDH             | 100  | 表示使用ECDH算法。    |
| HUKS_ALG_X25519           | 101  | 表示使用X25519算法。  |
| HUKS_ALG_ED25519          | 102  | 表示使用ED25519算法。 |
| HUKS_ALG_DH               | 103  | 表示使用DH算法。      |
| HUKS_ALG_SM2<sup>9+</sup> | 150  | 表示使用SM2算法。     |
| HUKS_ALG_SM3<sup>9+</sup> | 151  | 表示使用SM3算法。     |
| HUKS_ALG_SM4<sup>9+</sup> | 152  | 表示使用SM4算法。     |

C
CheungVane 已提交
2715
## HuksKeyGenerateType
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726

表示生成密钥的类型。

**系统能力**:SystemCapability.Security.Huks

| 名称                           | 值   | 说明             |
| ------------------------------ | ---- | ---------------- |
| HUKS_KEY_GENERATE_TYPE_DEFAULT | 0    | 默认生成的密钥。 |
| HUKS_KEY_GENERATE_TYPE_DERIVE  | 1    | 派生生成的密钥。 |
| HUKS_KEY_GENERATE_TYPE_AGREE   | 2    | 协商生成的密钥。 |

C
CheungVane 已提交
2727
## HuksKeyFlag
2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739

表示密钥的产生方式。

**系统能力**:SystemCapability.Security.Huks

| 名称                       | 值   | 说明                                 |
| -------------------------- | ---- | ------------------------------------ |
| HUKS_KEY_FLAG_IMPORT_KEY   | 1    | 表示通过导入公钥接口导入的密钥。     |
| HUKS_KEY_FLAG_GENERATE_KEY | 2    | 表示通过生成密钥接口生成的密钥。     |
| HUKS_KEY_FLAG_AGREE_KEY    | 3    | 表示通过生成密钥协商接口生成的密钥。 |
| HUKS_KEY_FLAG_DERIVE_KEY   | 4    | 表示通过生成密钥派生接口生成的密钥。 |

C
CheungVane 已提交
2740
## HuksKeyStorageType
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750

表示密钥存储方式。

**系统能力**:SystemCapability.Security.Huks

| 名称                    | 值   | 说明                           |
| ----------------------- | ---- | ------------------------------ |
| HUKS_STORAGE_TEMP       | 0    | 表示通过本地直接管理密钥。     |
| HUKS_STORAGE_PERSISTENT | 1    | 表示通过HUKS service管理密钥。 |

C
CheungVane 已提交
2751
## HuksSendType
Z
zhangcheng 已提交
2752

2753
表示发送Tag的方式。
Z
zhangcheng 已提交
2754

2755
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
2756

2757 2758 2759 2760
| 名称                 | 值   | 说明              |
| -------------------- | ---- | ----------------- |
| HUKS_SEND_TYPE_ASYNC | 0    | 表示异步发送TAG。 |
| HUKS_SEND_TYPE_SYNC  | 1    | 表示同步发送TAG。 |
Z
zhangcheng 已提交
2761

C
CheungVane 已提交
2762
## HuksUnwrapSuite<sup>9+</sup>
2763 2764

表示导入加密密钥的算法套件。
V
Vincentchenhao 已提交
2765

2766
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
2767

2768 2769 2770 2771 2772
| 名称                                           | 值   | 说明                                                  |
| ---------------------------------------------- | ---- | ----------------------------------------------------- |
| HUKS_UNWRAP_SUITE_X25519_AES_256_GCM_NOPADDING | 1    | 导入加密密钥时,X25519密钥协商后使用AES-256 GCM加密。 |
| HUKS_UNWRAP_SUITE_ECDH_AES_256_GCM_NOPADDING   | 2    | 导入加密密钥时,ECDH密钥协商后使用AES-256 GCM加密。   |

C
CheungVane 已提交
2773
## HuksImportKeyType<sup>9+</sup>
2774 2775

表示导入密钥的密钥类型,默认为导入公钥,导入对称密钥时不需要该字段。
V
Vincentchenhao 已提交
2776

2777
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
2778

2779 2780 2781 2782 2783
| 名称                      | 值   | 说明                           |
| ------------------------- | ---- | ------------------------------ |
| HUKS_KEY_TYPE_PUBLIC_KEY  | 0    | 表示导入的密钥类型为公钥。     |
| HUKS_KEY_TYPE_PRIVATE_KEY | 1    | 表示导入的密钥类型为私钥。     |
| HUKS_KEY_TYPE_KEY_PAIR    | 2    | 表示导入的密钥类型为公私钥对。 |
V
Vincentchenhao 已提交
2784

C
CheungVane 已提交
2785
## HuksUserAuthType<sup>9+</sup>
V
Vincentchenhao 已提交
2786

2787
表示用户认证类型。
V
Vincentchenhao 已提交
2788

2789
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
2790

2791 2792 2793 2794 2795
| 名称                            | 值   | 说明                      |
| ------------------------------- | ---- | ------------------------- |
| HUKS_USER_AUTH_TYPE_FINGERPRINT | 1    | 表示用户认证类型为指纹。  |
| HUKS_USER_AUTH_TYPE_FACE        | 2    | 表示用户认证类型为人脸 。 |
| HUKS_USER_AUTH_TYPE_PIN         | 4    | 表示用户认证类型为PIN码。 |
V
Vincentchenhao 已提交
2796

C
CheungVane 已提交
2797
## HuksAuthAccessType<sup>9+</sup>
V
Vincentchenhao 已提交
2798

2799
表示安全访问控制类型。
V
Vincentchenhao 已提交
2800

2801
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
2802

2803 2804 2805 2806
| 名称                                    | 值   | 说明                                             |
| --------------------------------------- | ---- | ------------------------------------------------ |
| HUKS_AUTH_ACCESS_INVALID_CLEAR_PASSWORD | 1    | 表示安全访问控制类型为清除密码后密钥无效。       |
| HUKS_AUTH_ACCESS_INVALID_NEW_BIO_ENROLL | 2    | 表示安全访问控制类型为新录入生物特征后密钥无效。 |
V
Vincentchenhao 已提交
2807

C
CheungVane 已提交
2808
## HuksChallengeType<sup>9+</sup>
V
Vincentchenhao 已提交
2809

2810
表示密钥使用时生成challenge的类型。
Z
zhangcheng 已提交
2811 2812 2813

**系统能力**:SystemCapability.Security.Huks

2814 2815 2816 2817 2818
| 名称                            | 值   | 说明                           |
| ------------------------------- | ---- | ------------------------------ |
| HUKS_CHALLENGE_TYPE_NORMAL | 0    | 表示challenge为普通类型,默认32字节。 |
| HUKS_CHALLENGE_TYPE_CUSTOM        | 1    | 表示challenge为用户自定义类型。支持使用多个密钥仅一次认证。 |
| HUKS_CHALLENGE_TYPE_NONE         | 2    | 表示免challenge类型。 |
Z
zhangcheng 已提交
2819

C
CheungVane 已提交
2820
## HuksChallengePosition<sup>9+</sup>
V
Vincentchenhao 已提交
2821

2822
表示challenge类型为用户自定义类型时,生成的challenge有效长度仅为8字节连续的数据,且仅支持4种位置 。
V
Vincentchenhao 已提交
2823

2824
**系统能力**:SystemCapability.Security.Huks
S
shuyi 已提交
2825

2826 2827 2828 2829 2830 2831
| 名称                            | 值   | 说明                           |
| ------------------------------- | ---- | ------------------------------ |
| HUKS_CHALLENGE_POS_0 | 0    | 表示0~7字节为当前密钥的有效challenge。 |
| HUKS_CHALLENGE_POS_1        | 1    | 表示8~15字节为当前密钥的有效challenge。 |
| HUKS_CHALLENGE_POS_2         | 2    | 表示16~23字节为当前密钥的有效challenge。 |
| HUKS_CHALLENGE_POS_3        | 3   | 表示24~31字节为当前密钥的有效challenge。 |
S
shuyi 已提交
2832

C
CheungVane 已提交
2833
## HuksSecureSignType<sup>9+</sup>
S
shuyi 已提交
2834

2835
表示生成或导入密钥时,指定该密钥的签名类型。
V
Vincentchenhao 已提交
2836

2837
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
2838

2839 2840 2841 2842
| 名称                           | 值   | 说明                                                         |
| ------------------------------ | ---- | ------------------------------------------------------------ |
| HUKS_SECURE_SIGN_WITH_AUTHINFO | 1    | 表示签名类型为携带认证信息。生成或导入密钥时指定该字段,则在使用密钥进行签名时,对待签名的数据添加认证信息后进行签名。 |

C
CheungVane 已提交
2843
## HuksTagType
2844 2845

表示Tag的数据类型。
Z
zhangcheng 已提交
2846 2847 2848

**系统能力**:SystemCapability.Security.Huks

2849 2850 2851 2852 2853 2854 2855 2856 2857
| 名称                  | 值      | 说明                                    |
| --------------------- | ------- | --------------------------------------- |
| HUKS_TAG_TYPE_INVALID | 0 << 28 | 表示非法的Tag类型。                     |
| HUKS_TAG_TYPE_INT     | 1 << 28 | 表示该Tag的数据类型为int类型的number。  |
| HUKS_TAG_TYPE_UINT    | 2 << 28 | 表示该Tag的数据类型为uint类型的number。 |
| HUKS_TAG_TYPE_ULONG   | 3 << 28 | 表示该Tag的数据类型为bigint。           |
| HUKS_TAG_TYPE_BOOL    | 4 << 28 | 表示该Tag的数据类型为boolean。          |
| HUKS_TAG_TYPE_BYTES   | 5 << 28 | 表示该Tag的数据类型为Uint8Array。       |

C
CheungVane 已提交
2858
## HuksTag
Z
zhangcheng 已提交
2859

2860
表示调用参数的Tag。
Z
zhangcheng 已提交
2861

2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947
**系统能力**:SystemCapability.Security.Huks

| 名称                                         | 值                                       | 说明                                   |
| -------------------------------------------- | ---------------------------------------- | -------------------------------------- |
| HUKS_TAG_INVALID                             | HuksTagType.HUKS_TAG_TYPE_INVALID \| 0   | 表示非法的Tag。                        |
| HUKS_TAG_ALGORITHM                           | HUKS_TAG_TYPE_UINT \| 1                  | 表示算法的Tag。                        |
| HUKS_TAG_PURPOSE                             | HuksTagType.HUKS_TAG_TYPE_UINT \| 2      | 表示密钥用途的Tag。                    |
| HUKS_TAG_KEY_SIZE                            | HuksTagType.HUKS_TAG_TYPE_UINT \| 3      | 表示密钥长度的Tag。                    |
| HUKS_TAG_DIGEST                              | HuksTagType.HUKS_TAG_TYPE_UINT \| 4      | 表示摘要算法的Tag。                    |
| HUKS_TAG_PADDING                             | HuksTagType.HUKS_TAG_TYPE_UINT \| 5      | 表示补齐算法的Tag。                    |
| HUKS_TAG_BLOCK_MODE                          | HuksTagType.HUKS_TAG_TYPE_UINT \| 6      | 表示加密模式的Tag。                    |
| HUKS_TAG_KEY_TYPE                            | HuksTagType.HUKS_TAG_TYPE_UINT \| 7      | 表示密钥类型的Tag。                    |
| HUKS_TAG_ASSOCIATED_DATA                     | HuksTagType.HUKS_TAG_TYPE_BYTES \| 8     | 表示附加身份验证数据的Tag。            |
| HUKS_TAG_NONCE                               | HuksTagType.HUKS_TAG_TYPE_BYTES \| 9     | 表示密钥加解密的字段。                 |
| HUKS_TAG_IV                                  | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10    | 表示密钥初始化的向量。                 |
| HUKS_TAG_INFO                                | HuksTagType.HUKS_TAG_TYPE_BYTES \| 11    | 表示密钥派生时的info。                 |
| HUKS_TAG_SALT                                | HuksTagType.HUKS_TAG_TYPE_BYTES \| 12    | 表示密钥派生时的盐值。                 |
| HUKS_TAG_PWD                                 | HuksTagType.HUKS_TAG_TYPE_BYTES \| 13    | 表示密钥派生时的password。             |
| HUKS_TAG_ITERATION                           | HuksTagType.HUKS_TAG_TYPE_UINT \| 14     | 表示密钥派生时的迭代次数。             |
| HUKS_TAG_KEY_GENERATE_TYPE                   | HuksTagType.HUKS_TAG_TYPE_UINT \| 15     | 表示生成密钥类型的Tag。                |
| HUKS_TAG_DERIVE_MAIN_KEY                     | HuksTagType.HUKS_TAG_TYPE_BYTES \| 16    | 表示密钥派生时的主密钥。               |
| HUKS_TAG_DERIVE_FACTOR                       | HuksTagType.HUKS_TAG_TYPE_BYTES \| 17    | 表示密钥派生时的派生因子。             |
| HUKS_TAG_DERIVE_ALG                          | HuksTagType.HUKS_TAG_TYPE_UINT \| 18     | 表示密钥派生时的算法类型。             |
| HUKS_TAG_AGREE_ALG                           | HuksTagType.HUKS_TAG_TYPE_UINT \| 19     | 表示密钥协商时的算法类型。             |
| HUKS_TAG_AGREE_PUBLIC_KEY_IS_KEY_ALIAS       | HuksTagType.HUKS_TAG_TYPE_BOOL \| 20     | 表示密钥协商时的公钥别名。             |
| HUKS_TAG_AGREE_PRIVATE_KEY_ALIAS             | HuksTagType.HUKS_TAG_TYPE_BYTES \| 21    | 表示密钥协商时的私钥别名。             |
| HUKS_TAG_AGREE_PUBLIC_KEY                    | HuksTagType.HUKS_TAG_TYPE_BYTES \| 22    | 表示密钥协商时的公钥。                 |
| HUKS_TAG_KEY_ALIAS                           | HuksTagType.HUKS_TAG_TYPE_BYTES \| 23    | 表示密钥别名。                         |
| HUKS_TAG_DERIVE_KEY_SIZE                     | HuksTagType.HUKS_TAG_TYPE_UINT \| 24     | 表示派生密钥的大小。                   |
| HUKS_TAG_IMPORT_KEY_TYPE<sup>9+</sup>        | HuksTagType.HUKS_TAG_TYPE_UINT \| 25     | 表示导入的密钥类型。                     |
| HUKS_TAG_UNWRAP_ALGORITHM_SUITE<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 26     | 表示导入加密密钥的套件。                 |
| HUKS_TAG_ACTIVE_DATETIME                     | HuksTagType.HUKS_TAG_TYPE_ULONG \| 201   | 预留。                                 |
| HUKS_TAG_ORIGINATION_EXPIRE_DATETIME         | HuksTagType.HUKS_TAG_TYPE_ULONG \| 202   | 预留。                                 |
| HUKS_TAG_USAGE_EXPIRE_DATETIME               | HuksTagType.HUKS_TAG_TYPE_ULONG \| 203   | 预留。                                 |
| HUKS_TAG_CREATION_DATETIME                   | HuksTagType.HUKS_TAG_TYPE_ULONG \| 204   | 预留。                                 |
| HUKS_TAG_ALL_USERS                           | ksTagType.HUKS_TAG_TYPE_BOOL \| 301      | 预留。                                 |
| HUKS_TAG_USER_ID                             | HuksTagType.HUKS_TAG_TYPE_UINT \| 302    | 预留。                                 |
| HUKS_TAG_NO_AUTH_REQUIRED                    | HuksTagType.HUKS_TAG_TYPE_BOOL \| 303    | 预留。                                 |
| HUKS_TAG_USER_AUTH_TYPE                      | HuksTagType.HUKS_TAG_TYPE_UINT \| 304    | 表示用户认证类型。从[HuksUserAuthType](#huksuserauthtype9)中选择,需要与安全访问控制类型同时设置。支持同时指定两种用户认证类型,如:安全访问控制类型指定为HKS_SECURE_ACCESS_INVALID_NEW_BIO_ENROLL时,密钥访问认证类型可以指定以下三种: HKS_USER_AUTH_TYPE_FACE 、HKS_USER_AUTH_TYPE_FINGERPRINT、HKS_USER_AUTH_TYPE_FACE \| HKS_USER_AUTH_TYPE_FINGERPRINT |
| HUKS_TAG_AUTH_TIMEOUT                        | HuksTagType.HUKS_TAG_TYPE_UINT \| 305    | 预留。                                 |
| HUKS_TAG_AUTH_TOKEN                          | HuksTagType.HUKS_TAG_TYPE_BYTES \| 306   | 预留。                                 |
| HUKS_TAG_KEY_AUTH_ACCESS_TYPE<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 307 | 表示安全访问控制类型。从[HuksAuthAccessType](#huksauthaccesstype9)中选择,需要和用户认证类型同时设置。 |
| HUKS_TAG_KEY_SECURE_SIGN_TYPE<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 308 | 表示生成或导入密钥时,指定该密钥的签名类型。 |
| HUKS_TAG_CHALLENGE_TYPE<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 309 | 表示密钥使用时生成的challenge类型。从[HuksChallengeType](#hukschallengetype9)中选择 |
| HUKS_TAG_CHALLENGE_POS<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 310 | 表示challenge类型为用户自定义类型时,huks产生的challenge有效长度仅为8字节连续的数据。从[HuksChallengePosition](#hukschallengeposition9)中选择。 |
| HUKS_TAG_ATTESTATION_CHALLENGE               | HuksTagType.HUKS_TAG_TYPE_BYTES \| 501   | 表示attestation时的挑战值。            |
| HUKS_TAG_ATTESTATION_APPLICATION_ID          | HuksTagType.HUKS_TAG_TYPE_BYTES \| 502   | 表示attestation时拥有该密钥的application的Id。    |
| HUKS_TAG_ATTESTATION_ID_BRAND                | HuksTagType.HUKS_TAG_TYPE_BYTES \| 503   | 表示设备的品牌。                      |
| HUKS_TAG_ATTESTATION_ID_DEVICE               | HuksTagType.HUKS_TAG_TYPE_BYTES \| 504   | 表示设备的设备ID。                     |
| HUKS_TAG_ATTESTATION_ID_PRODUCT              | HuksTagType.HUKS_TAG_TYPE_BYTES \| 505   | 表示设备的产品名。                    |
| HUKS_TAG_ATTESTATION_ID_SERIAL               | HuksTagType.HUKS_TAG_TYPE_BYTES \| 506   | 表示设备的SN号。                       |
| HUKS_TAG_ATTESTATION_ID_IMEI                 | HuksTagType.HUKS_TAG_TYPE_BYTES \| 507   | 表示设备的IMEI号。                     |
| HUKS_TAG_ATTESTATION_ID_MEID                 | HuksTagType.HUKS_TAG_TYPE_BYTES \| 508   | 表示设备的MEID号。                     |
| HUKS_TAG_ATTESTATION_ID_MANUFACTURER         | HuksTagType.HUKS_TAG_TYPE_BYTES \| 509   | 表示设备的制造商。                     |
| HUKS_TAG_ATTESTATION_ID_MODEL                | HuksTagType.HUKS_TAG_TYPE_BYTES \| 510   | 表示设备的型号。                       |
| HUKS_TAG_ATTESTATION_ID_ALIAS                | HuksTagType.HUKS_TAG_TYPE_BYTES \| 511   | 表示attestation时的密钥别名。          |
| HUKS_TAG_ATTESTATION_ID_SOCID                | HuksTagType.HUKS_TAG_TYPE_BYTES \| 512   | 表示设备的SOCID。                      |
| HUKS_TAG_ATTESTATION_ID_UDID                 | HuksTagType.HUKS_TAG_TYPE_BYTES \| 513   | 表示设备的UDID。                       |
| HUKS_TAG_ATTESTATION_ID_SEC_LEVEL_INFO       | HuksTagType.HUKS_TAG_TYPE_BYTES \| 514   | 表示attestation时的安全凭据。          |
| HUKS_TAG_ATTESTATION_ID_VERSION_INFO         | HuksTagType.HUKS_TAG_TYPE_BYTES \| 515   | 表示attestation时的版本号。            |
| HUKS_TAG_IS_KEY_ALIAS                        | HuksTagType.HUKS_TAG_TYPE_BOOL \| 1001   | 表示是否使用生成key时传入的别名的Tag。 |
| HUKS_TAG_KEY_STORAGE_FLAG                    | HuksTagType.HUKS_TAG_TYPE_UINT \| 1002   | 表示密钥存储方式的Tag。                |
| HUKS_TAG_IS_ALLOWED_WRAP                     | HuksTagType.HUKS_TAG_TYPE_BOOL \| 1003   | 预留。                                 |
| HUKS_TAG_KEY_WRAP_TYPE                       | HuksTagType.HUKS_TAG_TYPE_UINT \| 1004   | 预留。                                 |
| HUKS_TAG_KEY_AUTH_ID                         | HuksTagType.HUKS_TAG_TYPE_BYTES \| 1005  | 预留。                                 |
| HUKS_TAG_KEY_ROLE                            | HuksTagType.HUKS_TAG_TYPE_UINT \| 1006   | 预留。                                 |
| HUKS_TAG_KEY_FLAG                            | HuksTagType.HUKS_TAG_TYPE_UINT \| 1007   | 表示密钥标志的Tag。                    |
| HUKS_TAG_IS_ASYNCHRONIZED                    | HuksTagType.HUKS_TAG_TYPE_UINT \| 1008   | 预留。                                 |
| HUKS_TAG_SECURE_KEY_ALIAS                    | HuksTagType.HUKS_TAG_TYPE_BOOL \| 1009   | 预留。                                 |
| HUKS_TAG_SECURE_KEY_UUID                     | HuksTagType.HUKS_TAG_TYPE_BYTES \| 1010  | 预留。                                 |
| HUKS_TAG_KEY_DOMAIN                          | HuksTagType.HUKS_TAG_TYPE_UINT \| 1011   | 预留。                                 |
| HUKS_TAG_PROCESS_NAME                        | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10001 | 表示进程名称的Tag。                    |
| HUKS_TAG_PACKAGE_NAME                        | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10002 | 预留。                                 |
| HUKS_TAG_ACCESS_TIME                         | HuksTagType.HUKS_TAG_TYPE_UINT \| 10003  | 预留。                                 |
| HUKS_TAG_USES_TIME                           | HuksTagType.HUKS_TAG_TYPE_UINT \| 10004  | 预留。                                 |
| HUKS_TAG_CRYPTO_CTX                          | HuksTagType.HUKS_TAG_TYPE_ULONG \| 10005 | 预留。                                 |
| HUKS_TAG_KEY                                 | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10006 | 预留。                                 |
| HUKS_TAG_KEY_VERSION                         | HuksTagType.HUKS_TAG_TYPE_UINT \| 10007  | 表示密钥版本的Tag。                    |
| HUKS_TAG_PAYLOAD_LEN                         | HuksTagType.HUKS_TAG_TYPE_UINT \| 10008  | 预留。                                 |
| HUKS_TAG_AE_TAG                              | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10009 | 预留。                                 |
| HUKS_TAG_IS_KEY_HANDLE                       | HuksTagType.HUKS_TAG_TYPE_ULONG \| 10010 | 预留。                                 |
| HUKS_TAG_OS_VERSION                          | HuksTagType.HUKS_TAG_TYPE_UINT \| 10101  | 表示操作系统版本的Tag。                |
| HUKS_TAG_OS_PATCHLEVEL                       | HuksTagType.HUKS_TAG_TYPE_UINT \| 10102  | 表示操作系统补丁级别的Tag。            |
| HUKS_TAG_SYMMETRIC_KEY_DATA                  | HuksTagType.HUKS_TAG_TYPE_BYTES \| 20001 | 预留。                                 |
| HUKS_TAG_ASYMMETRIC_PUBLIC_KEY_DATA          | HuksTagType.HUKS_TAG_TYPE_BYTES \| 20002 | 预留。                                 |
| HUKS_TAG_ASYMMETRIC_PRIVATE_KEY_DATA         | HuksTagType.HUKS_TAG_TYPE_BYTES \| 20003 | 预留。                                 |