js-apis-huks.md 129.3 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'
```

16
## 属性
V
Vincentchenhao 已提交
17

18
### HuksParam
S
shuyi 已提交
19

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

22
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
23

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

29
### HuksOptions
Z
zhangcheng 已提交
30

31
调用接口使用的options。
Z
zhangcheng 已提交
32

33
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
34

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

40
### HuksHandle<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
41

42
huks Handle结构体。
V
Vincentchenhao 已提交
43

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

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

52
### HuksSessionHandle<sup>9+</sup>
V
Vincentchenhao 已提交
53

54
huks Handle结构体。
V
Vincentchenhao 已提交
55

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

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

63
### HuksResult<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
64

65
调用接口返回的result。
V
Vincentchenhao 已提交
66

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

V
Vincentchenhao 已提交
69 70


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

78
### HuksReturnResult<sup>9+</sup>
S
shuyi 已提交
79

80
调用接口返回的result。
V
Vincentchenhao 已提交
81

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


S
shuyi 已提交
85

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

92
## 方法
V
Vincentchenhao 已提交
93

94
### huks.generateKey<sup>(deprecated)</sup>
S
shuyi 已提交
95

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

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

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

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

104
**参数:** 
S
shuyi 已提交
105

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

112
**示例:**
V
Vincentchenhao 已提交
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 141 142 143 144
```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 已提交
145

146
### huks.generateKey<sup>(deprecated)</sup>
S
shuyi 已提交
147

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

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

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

154
**系统能力**:SystemCapability.Security.Huks
H
haixiangw 已提交
155

156
**参数:** 
H
haixiangw 已提交
157

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

C
CheungVane 已提交
163
**返回值**
H
haixiangw 已提交
164

165 166
| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
C
CheungVane 已提交
167
| Promise\<[HuksResult](#huksresult)> | Promise对象。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 |
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 194 195 196 197
```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);
```
198

199
### huks.generateKeyItem<sup>9+</sup>
200

201
generateKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void
202

203
生成密钥,使用Callback回调异步返回结果。
204 205 206

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

207
**参数:** 
208

209 210 211 212
| 参数名   | 类型                        | 必填 | 说明                                          |
| -------- | --------------------------- | ---- | --------------------------------------------- |
| keyAlias | string                      | 是   | 别名。                                        |
| options  | [HuksOptions](#huksoptions) | 是   | 用于存放生成key所需TAG。                      |
C
CheungVane 已提交
213
| callback | AsyncCallback\<void>        | 是   | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 |
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 250 251 252 253
```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}`);
}
```
254

255
### huks.generateKeyItem<sup>9+</sup>
256

257
generateKeyItem(keyAlias: string, options: HuksOptions) : Promise\<void>
258

259
生成密钥,使用Promise方式异步返回结果。
260 261 262

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

263
**参数:** 
264

265 266 267 268
| 参数名   | 类型                        | 必填 | 说明                     |
| -------- | --------------------------- | ---- | ------------------------ |
| keyAlias | string                      | 是   | 密钥别名。               |
| options  | [HuksOptions](#huksoptions) | 是   | 用于存放生成key所需TAG。 |
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 305 306 307 308
```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 已提交
309

310
### huks.deleteKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
311

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

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

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

318
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
319

320
**参数:**
V
Vincentchenhao 已提交
321

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

**示例:**

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

339
### huks.deleteKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
340

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

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

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

347
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
348

349
**参数:**
V
Vincentchenhao 已提交
350

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

356
**返回值:**
V
Vincentchenhao 已提交
357

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

**示例:**

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

373
### huks.deleteKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
374

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

377
删除密钥,使用Callback回调异步返回结果。
Z
zhangcheng 已提交
378 379 380

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

381
**参数:**
Z
zhangcheng 已提交
382 383 384

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

**示例:**

```js
392
/* 此处options选择emptyOptions传空 */
Z
zhangcheng 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
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}`);
}
```

410
### huks.deleteKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
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 442 443 444 445

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}`);
}
```

446
### huks.getSdkVersion
V
Vincentchenhao 已提交
447 448 449 450 451

getSdkVersion(options: HuksOptions) : string

获取当前系统sdk版本。

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

V
Vincentchenhao 已提交
454 455 456 457
**参数:**

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

**返回值:**

S
shuyi 已提交
462 463 464
| 类型   | 说明          |
| ------ | ------------- |
| string | 返回sdk版本。 |
V
Vincentchenhao 已提交
465 466 467 468

**示例:**

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

476
### huks.importKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
477

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

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

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

484
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
485 486 487 488 489

**参数:**

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

**示例:**

```js
S
shuyi 已提交
497 498 499 500 501 502 503 504 505
/* 以导入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 已提交
506 507 508 509
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
  tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
S
shuyi 已提交
510
  value: huks.HuksKeyAlg.HUKS_ALG_AES
V
Vincentchenhao 已提交
511 512 513
};
properties[1] = {
  tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
S
shuyi 已提交
514
  value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256
V
Vincentchenhao 已提交
515 516 517
};
properties[2] = {
  tag: huks.HuksTag.HUKS_TAG_PURPOSE,
S
shuyi 已提交
518 519
  value:
huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
V
Vincentchenhao 已提交
520 521 522
};
properties[3] = {
  tag: huks.HuksTag.HUKS_TAG_PADDING,
S
shuyi 已提交
523
  value:huks.HuksKeyPadding.HUKS_PADDING_PKCS7
V
Vincentchenhao 已提交
524 525
};
properties[4] = {
S
shuyi 已提交
526 527
  tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
  value: huks.HuksCipherMode.HUKS_MODE_ECB
V
Vincentchenhao 已提交
528 529 530
};
var options = {
  properties: properties,
S
shuyi 已提交
531
  inData: plainTextSize32
V
Vincentchenhao 已提交
532 533 534 535
};
huks.importKey(keyAlias, options, function (err, data){});
```

536
### huks.importKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
537

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

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

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

544
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
545 546 547 548 549

**参数:**

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

**返回值:**

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

**示例:**

```js
S
shuyi 已提交
562 563 564 565 566 567 568 569 570 571 572 573
/* 以导入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 已提交
574 575 576 577
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
  tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
S
shuyi 已提交
578
  value: huks.HuksKeyAlg.HUKS_ALG_AES
V
Vincentchenhao 已提交
579 580 581
};
properties[1] = {
  tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
S
shuyi 已提交
582
  value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128
V
Vincentchenhao 已提交
583 584 585
};
properties[2] = {
  tag: huks.HuksTag.HUKS_TAG_PURPOSE,
S
shuyi 已提交
586
  value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
V
Vincentchenhao 已提交
587 588 589
};
properties[3] = {
  tag: huks.HuksTag.HUKS_TAG_PADDING,
S
shuyi 已提交
590
  value:huks.HuksKeyPadding.HUKS_PADDING_PKCS7
V
Vincentchenhao 已提交
591 592
};
properties[4] = {
S
shuyi 已提交
593 594
  tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
  value: huks.HuksCipherMode.HUKS_MODE_ECB
V
Vincentchenhao 已提交
595
};
S
shuyi 已提交
596
var huksoptions = {
V
Vincentchenhao 已提交
597
  properties: properties,
S
shuyi 已提交
598
  inData: plainTextSize32
V
Vincentchenhao 已提交
599
};
S
shuyi 已提交
600
var result = huks.importKey(keyAlias, huksoptions);
V
Vincentchenhao 已提交
601 602
```

603
### huks.importKeyItem<sup>9+</sup>
C
CheungVane 已提交
604

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

Z
zhangcheng 已提交
607
导入明文密钥,使用Callback方式回调异步返回结果 。
C
CheungVane 已提交
608 609 610 611 612

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

**参数:**

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

**示例:**

```js
Z
zhangcheng 已提交
622 623 624 625 626 627 628 629 630 631 632 633
/* 以导入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] = {
634
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
Z
zhangcheng 已提交
635 636 637
    value: huks.HuksKeyAlg.HUKS_ALG_AES
};
properties[1] = {
638
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
Z
zhangcheng 已提交
639 640 641
    value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256
};
properties[2] = {
642
    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
Z
zhangcheng 已提交
643 644 645 646
    value:
    huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
};
properties[3] = {
647
    tag: huks.HuksTag.HUKS_TAG_PADDING,
Z
zhangcheng 已提交
648 649 650
    value:huks.HuksKeyPadding.HUKS_PADDING_PKCS7
};
properties[4] = {
651 652
    tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
    value: huks.HuksCipherMode.HUKS_MODE_ECB
Z
zhangcheng 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
};
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 已提交
668 669 670
}
```

671
### huks.importKeyItem<sup>9+</sup>
C
CheungVane 已提交
672

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

Z
zhangcheng 已提交
675
导入明文密钥,使用Promise方式异步返回结果。
C
CheungVane 已提交
676 677 678 679 680

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

**参数:**

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

C
CheungVane 已提交
686 687 688
**示例:**

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

Z
zhangcheng 已提交
692 693 694 695 696 697 698
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 已提交
699

Z
zhangcheng 已提交
700 701 702 703
/*第一步:生成密钥*/
var keyAlias = 'keyAlias';
var properties = new Array();
properties[0] = {
704
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
Z
zhangcheng 已提交
705 706 707 708 709 710 711
    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] = {
712
    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
Z
zhangcheng 已提交
713 714 715
    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
};
properties[3] = {
716
    tag: huks.HuksTag.HUKS_TAG_PADDING,
Z
zhangcheng 已提交
717 718 719
    value:huks.HuksKeyPadding.HUKS_PADDING_PKCS7
};
properties[4] = {
720 721
    tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE,
    value: huks.HuksCipherMode.HUKS_MODE_ECB
Z
zhangcheng 已提交
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
};
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}`);
}
```
739

740
### huks.attestKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753

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

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

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

**参数:**

| 参数名   | 类型                                                 | 必填 | 说明                                          |
| -------- | ---------------------------------------------------- | ---- | --------------------------------------------- |
| keyAlias | string                                               | 是   | 密钥别名,存放待获取证书密钥的别名。          |
| options  | [HuksOptions](#huksoptions)                          | 是   | 用于获取证书时指定所需参数与数据。            |
C
CheungVane 已提交
754
| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult)> | 是   | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 |
Z
zhangcheng 已提交
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 818 819 820 821

**示例:**

```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}`);
    }
822 823
}

C
CheungVane 已提交
824
async function attestKey() {
Z
zhangcheng 已提交
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 855 856 857 858
    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 已提交
859 860 861
}
```

862
### huks.attestKeyItem<sup>9+</sup>
H
haixiangw 已提交
863

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

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

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

**参数:**

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

**返回值:**

| 类型                                           | 说明                                          |
| ---------------------------------------------- | --------------------------------------------- |
C
CheungVane 已提交
881
| Promise<[HuksReturnResult](#huksreturnresult)> | Promise对象。不返回err值时表示接口使用成功,其他时为错误。 |
Z
zhangcheng 已提交
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 985 986 987 988

**示例:**

```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}`);
    }
}
```

989
### huks.importWrappedKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
990 991

importWrappedKeyItem(keyAlias: string, wrappingKeyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void
H
haixiangw 已提交
992 993 994

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

995
**系统能力**:SystemCapability.Security.Huks
H
haixiangw 已提交
996 997 998

**参数:**

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

**示例:**

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

H
haixiangw 已提交
1011 1012 1013 1014 1015
var exportWrappingKey;
var alias1 = "importAlias";
var alias2 = "wrappingKeyAlias";

async function TestGenFunc(alias, options) {
Z
zhangcheng 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    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 已提交
1027 1028 1029
}

function genKey(alias, options) {
Z
zhangcheng 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    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 已提交
1042 1043 1044 1045
    });
}

async function TestExportFunc(alias, options) {
Z
zhangcheng 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    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 已提交
1058 1059
}

Z
zhangcheng 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
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 已提交
1073 1074 1075 1076
    });
}

async function TestImportWrappedFunc(alias, wrappingAlias, options) {
Z
zhangcheng 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
    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 已提交
1088 1089 1090
}

function importWrappedKey(alias, wrappingAlias, options) {
Z
zhangcheng 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
    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 已提交
1103 1104 1105 1106
    });
}

async function TestImportWrappedKeyFunc(
Z
zhangcheng 已提交
1107 1108 1109 1110
        alias,
        wrappingAlias,
        genOptions,
        importOptions
H
haixiangw 已提交
1111 1112 1113 1114 1115 1116 1117 1118
) {
    await TestGenFunc(wrappingAlias, genOptions);
    await TestExportFunc(wrappingAlias, genOptions);

    /* 以下操作不需要调用HUKS接口,此处不给出具体实现。
     * 假设待导入的密钥为keyA
     * 1.生成ECC公私钥keyB,公钥为keyB_pub, 私钥为keyB_pri
     * 2.使用keyB_pri和wrappingAlias密钥中获取的公钥进行密钥协商,协商出共享密钥share_key
1119 1120
     * 3.随机生成密钥kek,用于加密keyA,采用AES-GCM加密,加密过程中需要记录:nonce1、aad1、加密后的密文keyA_enc、加密后的tag1。
     * 4.使用share_key加密kek,采用AES-GCM加密,加密过程中需要记录:nonce2、aad2、加密后的密文kek_enc、加密后的tag2。
H
haixiangw 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
     * 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 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
    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 已提交
1159 1160 1161
};

function makeImportOptions() {
Z
zhangcheng 已提交
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 1187 1188 1189 1190
    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 已提交
1191 1192 1193
};

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

1205
### huks.importWrappedKeyItem<sup>9+</sup>
H
haixiangw 已提交
1206

Z
zhangcheng 已提交
1207
importWrappedKeyItem(keyAlias: string, wrappingKeyAlias: string, options: HuksOptions) : Promise\<void>
H
haixiangw 已提交
1208 1209 1210

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

1211
**系统能力**:SystemCapability.Security.Huks
H
haixiangw 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225

**参数:**

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

**示例:**

```js
/* 处理流程与callback类似,主要差异点为如下函数: */
async function TestImportWrappedFunc(alias, wrappingAlias, options) {
Z
zhangcheng 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
    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 已提交
1237 1238 1239
}
```

1240
### huks.exportKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1241

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

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

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

1248
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1249 1250 1251

**参数:**

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

**示例:**

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

1269
### huks.exportKey<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1270

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

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

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

1277
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1278 1279 1280 1281 1282

**参数:**

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

**返回值:**

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

**示例:**

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

1303
### huks.exportKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

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

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

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

**参数:**

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

**示例:**

```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}`);
}
```

1340
### huks.exportKeyItem<sup>9+</sup>
Z
zhangcheng 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

```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}`);
}
```

1382
### huks.getKeyProperties<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1383

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

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

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

1390
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1391 1392 1393 1394 1395

**参数:**

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

**示例:**

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

1411
### huks.getKeyProperties<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1412

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

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

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

1419
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1420 1421 1422 1423 1424

**参数:**

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

**返回值:**

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

**示例:**

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

1445
### huks.getKeyItemProperties<sup>9+</sup>
Z
zhangcheng 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458

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

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

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

**参数:**

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

**示例:**

```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}`);
}
```

1482
### huks.getKeyItemProperties<sup>9+</sup>
Z
zhangcheng 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

```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}`);
}
```

1524
### huks.isKeyExist<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1525

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

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

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

1532
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1533 1534 1535 1536 1537

**参数:**

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

**示例:**

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

1553
### huks.isKeyExist<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1554

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

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

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

1561
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1562 1563 1564 1565 1566

**参数:**

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

**返回值:**

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

**示例:**

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

1587
### huks.isKeyItemExist<sup>9+</sup>
Z
zhangcheng 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600

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

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

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

**参数:**

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

**示例:**

```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}`);
}
```

1624
### huks.isKeyItemExist<sup>9+</sup>
V
Vincentchenhao 已提交
1625

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

```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}`);
}
```

1666
### huks.init<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1667

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

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

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

1674
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1675 1676 1677 1678 1679

**参数:**

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


1685
### huks.init<sup>(deprecated)</sup>
Z
zengyawen 已提交
1686 1687

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

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

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

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

V
Vincentchenhao 已提交
1695 1696 1697 1698
**参数:**

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

**返回值**

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

1708
### huks.initSession<sup>9+</sup>
Z
zhangcheng 已提交
1709 1710 1711

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

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

1714
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
1715 1716 1717 1718 1719 1720 1721

**参数:**

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


1725
### huks.initSession<sup>9+</sup>
Z
zhangcheng 已提交
1726 1727 1728

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

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

1731
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
1732 1733 1734 1735 1736 1737 1738

**参数:**

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

**返回值**

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

1746
### huks.update<sup>(deprecated)</sup>
Z
zhangcheng 已提交
1747 1748 1749

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

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

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

Z
zhangcheng 已提交
1754 1755 1756 1757 1758 1759 1760 1761
**系统能力**:SystemCapability.Security.Huks

**参数:**

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

1764
### huks.update<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1765

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

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

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

1772
**系统能力**: SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1773

1774
**参数:**
V
Vincentchenhao 已提交
1775

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

1783
### huks.update<sup>(deprecated)</sup>
Z
zengyawen 已提交
1784

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

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

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

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

V
Vincentchenhao 已提交
1793 1794
**参数:**

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

**返回值**

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

1807
### huks.updateSession<sup>9+</sup>
1808

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

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

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

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

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


1824
### huks.updateSession<sup>9+</sup>
1825

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

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

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

**参数:**

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

1841
### huks.updateSession<sup>9+</sup>
1842

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

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

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

**参数:**

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

**返回值**

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

1863
### huks.finish<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1864

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

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

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

1871
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1872 1873 1874 1875 1876

**参数:**

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


1882
### huks.finish<sup>(deprecated)</sup>
Z
zengyawen 已提交
1883 1884

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

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

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

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

V
Vincentchenhao 已提交
1892 1893 1894 1895
**参数:**

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

**返回值**

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

1905
### huks.finishSession<sup>9+</sup>
1906

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

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

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

**参数:**

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

1922
### huks.finishSession<sup>9+</sup>
1923

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

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

Z
zhangcheng 已提交
1928 1929 1930 1931 1932 1933 1934 1935 1936
**系统能力**:SystemCapability.Security.Huks

**参数:**

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


1940
### huks.finishSession<sup>9+</sup>
Z
zhangcheng 已提交
1941 1942 1943

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

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

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

**参数:**

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

**返回值**

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

1962
### huks.abort<sup>(deprecated)</sup>
V
Vincentchenhao 已提交
1963

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

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

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

1970
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
1971 1972 1973 1974 1975

**参数:**

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

**示例:**

```js
S
shuyi 已提交
1983 1984 1985 1986 1987
/* huks.init, huks.update, huks.finish为三段式接口,需要一起使用,当huks.init和huks.update
 * 以及huks.finish操作中的任一阶段发生错误时,都需要调用huks.abort来终止密钥的使用。
 *
 * 以下以RSA1024密钥的callback操作使用为例
 */
S
shuyi 已提交
1988
var keyalias = "HuksDemoRSA";
S
shuyi 已提交
1989 1990 1991 1992 1993
var properties = new Array();
var options = {
  properties: properties,
  inData: new Uint8Array(0)
};
S
shuyi 已提交
1994
var handle;
S
shuyi 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
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 已提交
2017
  huks.generateKey(keyalias, options);
S
shuyi 已提交
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
}
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 已提交
2028
  await huks.init(keyalias, options).then((data) => {
S
shuyi 已提交
2029
    console.log(`test init data: ${JSON.stringify(data)}`);
S
shuyi 已提交
2030
    handle = data.handle;
S
shuyi 已提交
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 2067 2068 2069 2070
  }).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 已提交
2071 2072
```

2073
### huks.abort<sup>(deprecated)</sup>
Z
zengyawen 已提交
2074 2075

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

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

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

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

V
Vincentchenhao 已提交
2083 2084 2085 2086
**参数:**

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

**返回值**

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

**示例:**

```js
S
shuyi 已提交
2099 2100 2101 2102 2103
/* huks.init, huks.update, huks.finish为三段式接口,需要一起使用,当huks.init和huks.update
 * 以及huks.finish操作中的任一阶段发生错误时,都需要调用huks.abort来终止密钥的使用。
 *
 * 以下以RSA1024密钥的promise操作使用为例
 */
S
shuyi 已提交
2104
var keyalias = "HuksDemoRSA";
S
shuyi 已提交
2105 2106 2107 2108 2109
var properties = new Array();
var options = {
  properties: properties,
  inData: new Uint8Array(0)
};
S
shuyi 已提交
2110
var handle;
S
shuyi 已提交
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 2138 2139 2140 2141
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 已提交
2142
  huks.generateKey(keyalias, options, function (err, data) { });
S
shuyi 已提交
2143 2144 2145
}
async function huksInit() {
  return new Promise((resolve, reject) => {
S
shuyi 已提交
2146
    huks.init(keyalias, options, async function (err, data) {
S
shuyi 已提交
2147
      if (data.errorCode === 0) {
S
shuyi 已提交
2148 2149
        resultMessage = "init success!"
        handle = data.handle;
S
shuyi 已提交
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
      } 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 已提交
2165
        }
S
shuyi 已提交
2166 2167 2168
      });
    });
    console.log(resultMessage);
S
shuyi 已提交
2169

S
shuyi 已提交
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
}

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 已提交
2193 2194
```

2195
### huks.abortSession<sup>9+</sup>
Z
zhangcheng 已提交
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208

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

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

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

**参数:**

| 参数名   | 类型                        | 必填 | 说明                                        |
| -------- | --------------------------- | ---- | ------------------------------------------- |
| handle   | number                      | 是   | Abort操作的handle。                         |
| options  | [HuksOptions](#huksoptions) | 是   | Abort操作的参数集合。                       |
C
CheungVane 已提交
2209
| callback | AsyncCallback\<void>        | 是   | 回调函数。将Abort操作的结果添加到密钥管理系统的回调。 |
Z
zhangcheng 已提交
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 2336 2337 2338 2339

**示例:**

```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}`);
    }
}
```

2340
### huks.abortSession<sup>9+</sup>
Z
zhangcheng 已提交
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353

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

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

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

**参数:**

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

**返回值**

| 类型                                | 说明                                               |
| ----------------------------------- | -------------------------------------------------- |
| Promise\<void>             | Promise对象。将Abort操作的结果添加到密钥管理系统的回调。 |
Z
zhangcheng 已提交
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 2409 2410 2411 2412

**示例:**

```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,
    }

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 2486 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 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
    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}`);
    }
}
```

## 枚举

### HuksErrorCode<sup>(deprecated)</sup>

表示错误码的枚举。

**系统能力**: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 |表示未知错误。|

### HuksExceptionErrCode<sup>9+</sup>

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

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

2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 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 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757
**系统能力**:SystemCapability.Security.Huks

| 类型                      | 名称                                           | 说明                        | 错误码   |
| ------------------------- | ---------------------------------------------- | --------------------------- | -------- |
| 权限                      | HUKS_ERR_CODE_PERMISSION_FAIL                  | 权限错误导致失败。          | 201      |
| 参数                      | HUKS_ERR_CODE_ILLEGAL_ARGUMENT                 | 参数错误导致失败。          | 401      |
| 不支持的API               | 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 |

### HuksKeyPurpose

表示密钥用途。

**系统能力**: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  | 表示密钥用于进行密钥协商。       |

### HuksKeyDigest

表示摘要算法。

**系统能力**: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摘要算法。 |

### HuksKeyPadding

表示补齐算法。

**系统能力**: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补齐算法。 |

### HuksCipherMode

表示加密模式。

**系统能力**: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加密模式。 |

### HuksKeySize

表示密钥长度。

**系统能力**: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。            |

### HuksKeyAlg

表示密钥使用的算法。

**系统能力**: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算法。     |

### HuksKeyGenerateType

表示生成密钥的类型。

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

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

### HuksKeyFlag

表示密钥的产生方式。

**系统能力**: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    | 表示通过生成密钥派生接口生成的密钥。 |

### HuksKeyStorageType

表示密钥存储方式。

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

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

### HuksSendType
Z
zhangcheng 已提交
2758

2759
表示发送Tag的方式。
Z
zhangcheng 已提交
2760

2761
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
2762

2763 2764 2765 2766
| 名称                 | 值   | 说明              |
| -------------------- | ---- | ----------------- |
| HUKS_SEND_TYPE_ASYNC | 0    | 表示异步发送TAG。 |
| HUKS_SEND_TYPE_SYNC  | 1    | 表示同步发送TAG。 |
Z
zhangcheng 已提交
2767

2768 2769 2770
### HuksUnwrapSuite<sup>9+</sup>

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

2772
**系统能力**:SystemCapability.Security.Huks
V
Vincentchenhao 已提交
2773

2774 2775 2776 2777 2778
| 名称                                           | 值   | 说明                                                  |
| ---------------------------------------------- | ---- | ----------------------------------------------------- |
| 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 已提交
2779
### HuksImportKeyType<sup>9+</sup>
2780 2781

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

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

2785 2786 2787 2788 2789
| 名称                      | 值   | 说明                           |
| ------------------------- | ---- | ------------------------------ |
| HUKS_KEY_TYPE_PUBLIC_KEY  | 0    | 表示导入的密钥类型为公钥。     |
| HUKS_KEY_TYPE_PRIVATE_KEY | 1    | 表示导入的密钥类型为私钥。     |
| HUKS_KEY_TYPE_KEY_PAIR    | 2    | 表示导入的密钥类型为公私钥对。 |
V
Vincentchenhao 已提交
2790

2791
### HuksUserAuthType<sup>9+</sup>
V
Vincentchenhao 已提交
2792

2793
表示用户认证类型。
V
Vincentchenhao 已提交
2794

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

2797 2798 2799 2800 2801
| 名称                            | 值   | 说明                      |
| ------------------------------- | ---- | ------------------------- |
| HUKS_USER_AUTH_TYPE_FINGERPRINT | 1    | 表示用户认证类型为指纹。  |
| HUKS_USER_AUTH_TYPE_FACE        | 2    | 表示用户认证类型为人脸 。 |
| HUKS_USER_AUTH_TYPE_PIN         | 4    | 表示用户认证类型为PIN码。 |
V
Vincentchenhao 已提交
2802

2803
### HuksAuthAccessType<sup>9+</sup>
V
Vincentchenhao 已提交
2804

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

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

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

2814
### HuksChallengeType<sup>9+</sup>
V
Vincentchenhao 已提交
2815

2816
表示密钥使用时生成challenge的类型。
Z
zhangcheng 已提交
2817 2818 2819

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

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

2826
### HuksChallengePosition<sup>9+</sup>
V
Vincentchenhao 已提交
2827

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

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

2832 2833 2834 2835 2836 2837
| 名称                            | 值   | 说明                           |
| ------------------------------- | ---- | ------------------------------ |
| 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 已提交
2838

2839
### HuksSecureSignType<sup>9+</sup>
S
shuyi 已提交
2840

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

2843
**系统能力**:SystemCapability.Security.Huks
Z
zhangcheng 已提交
2844

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

### HuksTagType

表示Tag的数据类型。
Z
zhangcheng 已提交
2852 2853 2854

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

2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
| 名称                  | 值      | 说明                                    |
| --------------------- | ------- | --------------------------------------- |
| 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。       |

### HuksTag
Z
zhangcheng 已提交
2865

2866
表示调用参数的Tag。
Z
zhangcheng 已提交
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 2948 2949 2950 2951 2952 2953
**系统能力**: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 | 预留。                                 |