1# @ohos.security.huks (通用密钥库系统) 2 3向应用提供密钥库能力,包括密钥管理及密钥的密码学操作等功能。 4HUKS所管理的密钥可以由应用导入或者由应用调用HUKS接口生成。 5 6> **说明** 7> 8> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 9 10## 导入模块 11 12```ts 13import huks from '@ohos.security.huks' 14``` 15 16## HuksParam 17 18调用接口使用的options中的properties数组中的param。 19 20**系统能力**:SystemCapability.Security.Huks.Core 21 22| 名称 | 类型 | 必填 | 说明 | 23| ------ | ----------------------------------- | ---- | ------------ | 24| tag | [HuksTag](#hukstag) | 是 | 标签。 | 25| value | boolean\|number\|bigint\|Uint8Array | 是 | 标签对应值。 | 26 27## HuksOptions 28 29调用接口使用的options。 30 31**系统能力**:SystemCapability.Security.Huks.Core 32 33| 名称 | 类型 | 必填 | 说明 | 34| ---------- | ----------------- | ---- | ------------------------ | 35| properties | Array\<[HuksParam](#huksparam)> | 否 | 属性,用于存HuksParam的数组。 | 36| inData | Uint8Array | 否 | 输入数据。 | 37 38## HuksSessionHandle<sup>9+</sup> 39 40huks Handle结构体。 41 42**系统能力**:SystemCapability.Security.Huks.Core 43 44| 名称 | 类型 | 必填 | 说明 | 45| --------- | ---------- | ---- | ---------------------------------------------------- | 46| handle | number | 是 | 表示handle值。 | 47| challenge | Uint8Array | 否 | 表示[initSession](#huksinitsession9)操作之后获取到的challenge信息。 | 48 49## HuksReturnResult<sup>9+</sup> 50 51调用接口返回的result。 52 53**系统能力**:SystemCapability.Security.Huks.Core 54 55 56 57| 名称 | 类型 | 必填 | 说明 | 58| ---------- | ------------------------------- | ---- | ---------------- | 59| outData | Uint8Array | 否 | 表示输出数据。 | 60| properties | Array\<[HuksParam](#huksparam)> | 否 | 表示属性信息。 | 61| certChains | Array\<string> | 否 | 表示证书链数据。 | 62 63 64## huks.generateKeyItem<sup>9+</sup> 65 66generateKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void 67 68生成密钥,使用Callback回调异步返回结果。 69 70**系统能力**:SystemCapability.Security.Huks.Core 71 72**参数:** 73 74| 参数名 | 类型 | 必填 | 说明 | 75| -------- | --------------------------- | ---- | --------------------------------------------- | 76| keyAlias | string | 是 | 别名。 | 77| options | [HuksOptions](#huksoptions) | 是 | 用于存放生成key所需TAG。其中密钥使用的算法、密钥用途、密钥长度为必选参数。 | 78| callback | AsyncCallback\<void> | 是 | 回调函数。未捕获error时代表用户指定别名的密钥生成成功,基于密钥不出TEE原则,此接口不会返回密钥材料内容,若捕获error,则为生成阶段出现异常。 | 79 80**错误码:** 81 82以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 83 84| 错误码ID | 错误信息 | 85| -------- | ------------- | 86| 401 | argument is invalid. | 87| 801 | api is not supported. | 88| 12000001 | algorithm mode is not supported. | 89| 12000002 | algorithm param is missing. | 90| 12000003 | algorithm param is invalid. | 91| 12000004 | operating file failed. | 92| 12000005 | IPC communication failed. | 93| 12000006 | error occured in crypto engine. | 94| 12000012 | external error. | 95| 12000013 | queried credential does not exist. | 96| 12000014 | memory is insufficient. | 97| 12000015 | call service failed. | 98 99**示例:** 100 101```ts 102import huks from '@ohos.security.huks'; 103/* 以生成ECC256密钥为例 */ 104class HuksProperties { 105 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 106 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | huks.HuksKeyDigest = huks.HuksKeyAlg.HUKS_ALG_ECC 107} 108let keyAlias: string = 'keyAlias'; 109let properties: HuksProperties[] = [ 110 { 111 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 112 value: huks.HuksKeyAlg.HUKS_ALG_ECC 113 }, 114 { 115 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 116 value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_256 117 }, 118 { 119 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 120 value: 121 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_SIGN | 122 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY 123 }, 124 { 125 tag: huks.HuksTag.HUKS_TAG_DIGEST, 126 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 127 }, 128]; 129let options: huks.HuksOptions = { 130 properties: properties 131}; 132try { 133 huks.generateKeyItem(keyAlias, options, (error, data) => { 134 if (error) { 135 console.error(`callback: generateKeyItem failed`); 136 } else { 137 console.info(`callback: generateKeyItem key success`); 138 } 139 }); 140} catch (error) { 141 console.error(`callback: generateKeyItem input arg invalid`); 142} 143``` 144 145## huks.generateKeyItem<sup>9+</sup> 146 147generateKeyItem(keyAlias: string, options: HuksOptions) : Promise\<void> 148 149生成密钥,使用Promise方式异步返回结果。基于密钥不出TEE原则,通过promise不会返回密钥材料内容,只用于表示此次调用是否成功。 150 151**系统能力**:SystemCapability.Security.Huks.Extension 152 153**参数:** 154 155| 参数名 | 类型 | 必填 | 说明 | 156| -------- | --------------------------- | ---- | ------------------------ | 157| keyAlias | string | 是 | 密钥别名。 | 158| options | [HuksOptions](#huksoptions) | 是 | 用于存放生成key所需TAG。其中密钥使用的算法、密钥用途、密钥长度为必选参数。 | 159 160**错误码:** 161 162以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 163 164| 错误码ID | 错误信息 | 165| -------- | ------------- | 166| 401 | argument is invalid. | 167| 801 | api is not supported. | 168| 12000001 | algorithm mode is not supported. | 169| 12000002 | algorithm param is missing. | 170| 12000003 | algorithm param is invalid. | 171| 12000004 | operating file failed. | 172| 12000005 | IPC communication failed. | 173| 12000006 | error occured in crypto engine. | 174| 12000012 | external error. | 175| 12000013 | queried credential does not exist. | 176| 12000014 | memory is insufficient. | 177| 12000015 | call service failed. | 178 179**示例:** 180 181```ts 182/* 以生成ECC256密钥为例 */ 183import huks from '@ohos.security.huks'; 184import { BusinessError } from '@ohos.base'; 185class HuksProperties { 186 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 187 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | huks.HuksKeyDigest = huks.HuksKeyAlg.HUKS_ALG_ECC 188} 189let keyAlias = 'keyAlias'; 190let properties: HuksProperties[] = [ 191 { 192 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 193 value: huks.HuksKeyAlg.HUKS_ALG_ECC 194 }, 195 { 196 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 197 value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_256 198 }, 199 { 200 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 201 value: 202 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_SIGN | 203 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY 204 }, 205 { 206 tag: huks.HuksTag.HUKS_TAG_DIGEST, 207 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 208 }, 209]; 210let options: huks.HuksOptions = { 211 properties: properties 212}; 213try { 214 huks.generateKeyItem(keyAlias, options) 215 .then((data) => { 216 console.info(`promise: generateKeyItem success`); 217 }) 218 .catch((error: BusinessError) => { 219 console.error(`promise: generateKeyItem failed`); 220 }); 221} catch (error) { 222 console.error(`promise: generateKeyItem input arg invalid`); 223} 224``` 225 226## huks.deleteKeyItem<sup>9+</sup> 227 228deleteKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void 229 230删除密钥,使用Callback回调异步返回结果。 231 232**系统能力**:SystemCapability.Security.Huks.Core 233 234**参数:** 235 236| 参数名 | 类型 | 必填 | 说明 | 237| -------- | --------------------------- | ---- | --------------------------------------------- | 238| keyAlias | string | 是 | 密钥别名,应为生成key时传入的别名。 | 239| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 240| callback | AsyncCallback\<void> | 是 | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 | 241 242**错误码:** 243 244以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 245 246| 错误码ID | 错误信息 | 247| -------- | ------------- | 248| 401 | argument is invalid. | 249| 801 | api is not supported. | 250| 12000004 | operating file failed. | 251| 12000005 | IPC communication failed. | 252| 12000011 | queried entity does not exist. | 253| 12000012 | external error. | 254| 12000014 | memory is insufficient. | 255 256**示例:** 257 258```ts 259import huks from '@ohos.security.huks'; 260/* 此处options选择emptyOptions传空 */ 261let keyAlias = 'keyAlias'; 262let emptyOptions: huks.HuksOptions = { 263 properties: [] 264}; 265try { 266 huks.deleteKeyItem(keyAlias, emptyOptions, (error, data) => { 267 if (error) { 268 console.error(`callback: deleteKeyItem failed`); 269 } else { 270 console.info(`callback: deleteKeyItem key success`); 271 } 272 }); 273} catch (error) { 274 console.error(`callback: deleteKeyItem input arg invalid`); 275} 276``` 277 278## huks.deleteKeyItem<sup>9+</sup> 279 280deleteKeyItem(keyAlias: string, options: HuksOptions) : Promise\<void> 281 282删除密钥,使用Promise方式异步返回结果。 283 284**系统能力**:SystemCapability.Security.Huks.Extension 285 286**参数:** 287 288| 参数名 | 类型 | 必填 | 说明 | 289| -------- | --------------------------- | ---- | ----------------------------------- | 290| keyAlias | string | 是 | 密钥别名,应为生成key时传入的别名。 | 291| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 292 293**错误码:** 294 295以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 296 297| 错误码ID | 错误信息 | 298| -------- | ------------- | 299| 401 | argument is invalid. | 300| 801 | api is not supported. | 301| 12000004 | operating file failed. | 302| 12000005 | IPC communication failed. | 303| 12000011 | queried entity does not exist. | 304| 12000012 | external error. | 305| 12000014 | memory is insufficient. | 306 307**示例:** 308 309```ts 310import huks from '@ohos.security.huks'; 311import { BusinessError } from '@ohos.base'; 312/* 此处options选择emptyOptions传空 */ 313let keyAlias = 'keyAlias'; 314let emptyOptions: huks.HuksOptions = { 315 properties: [] 316}; 317try { 318 huks.deleteKeyItem(keyAlias, emptyOptions) 319 .then ((data) => { 320 console.info(`promise: deleteKeyItem key success`); 321 }) 322 .catch((error: BusinessError) => { 323 console.error(`promise: deleteKeyItem failed`); 324 }); 325} catch (error) { 326 console.error(`promise: deleteKeyItem input arg invalid`); 327} 328``` 329 330## huks.getSdkVersion 331 332getSdkVersion(options: HuksOptions) : string 333 334获取当前系统sdk版本。 335 336**系统能力**:SystemCapability.Security.Huks.Extension 337 338**参数:** 339 340| 参数名 | 类型 | 必填 | 说明 | 341| ------- | ---------- | ---- | ------------------------- | 342| options | [HuksOptions](#huksoptions) | 是 | 空对象,用于存放sdk版本。 | 343 344**返回值:** 345 346| 类型 | 说明 | 347| ------ | ------------- | 348| string | 返回sdk版本。 | 349 350**示例:** 351 352```ts 353import huks from '@ohos.security.huks'; 354/* 此处options选择emptyOptions传空 */ 355let emptyOptions: huks.HuksOptions = { 356 properties: [] 357}; 358let result = huks.getSdkVersion(emptyOptions); 359``` 360 361## huks.importKeyItem<sup>9+</sup> 362 363importKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void 364 365导入明文密钥,使用Callback方式回调异步返回结果 。 366 367**系统能力**:SystemCapability.Security.Huks.Extension 368 369**参数:** 370 371| 参数名 | 类型 | 必填 | 说明 | 372| -------- | --------------------------- | ---- | --------------------------------------------- | 373| keyAlias | string | 是 | 密钥别名。 | 374| options | [HuksOptions](#huksoptions) | 是 | 用于导入时所需TAG和需要导入的密钥。其中密钥使用的算法、密钥用途、密钥长度为必选参数。 | 375| callback | AsyncCallback\<void> | 是 | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 | 376 377**错误码:** 378 379以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 380 381| 错误码ID | 错误信息 | 382| -------- | ------------- | 383| 401 | argument is invalid. | 384| 801 | api is not supported. | 385| 12000001 | algorithm mode is not supported. | 386| 12000002 | algorithm param is missing. | 387| 12000003 | algorithm param is invalid. | 388| 12000004 | operating file failed. | 389| 12000005 | IPC communication failed. | 390| 12000006 | error occured in crypto engine. | 391| 12000011 | queried entity does not exist. | 392| 12000012 | external error. | 393| 12000013 | queried credential does not exist. | 394| 12000014 | memory is insufficient. | 395| 12000015 | call service failed. | 396 397**示例:** 398 399```ts 400import huks from '@ohos.security.huks'; 401/* 以导入AES256密钥为例 */ 402class HuksProperties { 403 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 404 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | huks.HuksKeyPadding | 405 huks.HuksCipherMode = huks.HuksKeyAlg.HUKS_ALG_ECC 406} 407let plainTextSize32 = makeRandomArr(32); 408function makeRandomArr(size: number) { 409 let arr = new Uint8Array(size); 410 for (let i = 0; i < size; i++) { 411 arr[i] = Math.floor(Math.random() * 10); 412 } 413 return arr; 414}; 415let keyAlias = 'keyAlias'; 416let properties: HuksProperties[] = [ 417 { 418 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 419 value: huks.HuksKeyAlg.HUKS_ALG_AES 420 }, 421 { 422 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 423 value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256 424 }, 425 { 426 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 427 value: 428 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT 429 }, 430 { 431 tag: huks.HuksTag.HUKS_TAG_PADDING, 432 value: huks.HuksKeyPadding.HUKS_PADDING_PKCS7 433 }, 434 { 435 tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, 436 value: huks.HuksCipherMode.HUKS_MODE_ECB 437 } 438]; 439let options: huks.HuksOptions = { 440 properties: properties, 441 inData: plainTextSize32 442}; 443try { 444 huks.importKeyItem(keyAlias, options, (error, data) => { 445 if (error) { 446 console.error(`callback: importKeyItem failed`); 447 } else { 448 console.info(`callback: importKeyItem success`); 449 } 450 }); 451} catch (error) { 452 console.error(`callback: importKeyItem input arg invalid`); 453} 454``` 455 456## huks.importKeyItem<sup>9+</sup> 457 458importKeyItem(keyAlias: string, options: HuksOptions) : Promise\<void> 459 460导入明文密钥,使用Promise方式异步返回结果。 461 462**系统能力**:SystemCapability.Security.Huks.Extension 463 464**参数:** 465 466| 参数名 | 类型 | 必填 | 说明 | 467| -------- | --------------------------- | ---- | ----------------------------------- | 468| keyAlias | string | 是 | 密钥别名。 | 469| options | [HuksOptions](#huksoptions) | 是 | 用于导入时所需TAG和需要导入的密钥。其中密钥使用的算法、密钥用途、密钥长度为必选参数。 | 470 471**错误码:** 472 473以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 474 475| 错误码ID | 错误信息 | 476| -------- | ------------- | 477| 401 | argument is invalid. | 478| 801 | api is not supported. | 479| 12000001 | algorithm mode is not supported. | 480| 12000002 | algorithm param is missing. | 481| 12000003 | algorithm param is invalid. | 482| 12000004 | operating file failed. | 483| 12000005 | IPC communication failed. | 484| 12000006 | error occured in crypto engine. | 485| 12000011 | queried entity does not exist. | 486| 12000012 | external error. | 487| 12000013 | queried credential does not exist. | 488| 12000014 | memory is insufficient. | 489| 12000015 | call service failed. | 490 491**示例:** 492 493```ts 494import huks from '@ohos.security.huks'; 495import { BusinessError } from '@ohos.base'; 496/* 以导入AES128为例 */ 497class HuksProperties { 498 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 499 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | huks.HuksKeyPadding | 500 huks.HuksCipherMode = huks.HuksKeyAlg.HUKS_ALG_ECC 501} 502let plainTextSize32 = makeRandomArr(32); 503function makeRandomArr(size: number) { 504 let arr = new Uint8Array(size); 505 for (let i = 0; i < size; i++) { 506 arr[i] = Math.floor(Math.random() * 10); 507 } 508 return arr; 509}; 510/*第一步:生成密钥*/ 511let keyAlias = 'keyAlias'; 512let properties: HuksProperties[] = [ 513 { 514 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 515 value: huks.HuksKeyAlg.HUKS_ALG_AES 516 }, 517 { 518 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 519 value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256 520 }, 521 { 522 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 523 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT 524 }, 525 { 526 tag: huks.HuksTag.HUKS_TAG_PADDING, 527 value: huks.HuksKeyPadding.HUKS_PADDING_PKCS7 528 }, 529 { 530 tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, 531 value: huks.HuksCipherMode.HUKS_MODE_ECB 532 } 533]; 534let huksoptions: huks.HuksOptions = { 535 properties: properties, 536 inData: plainTextSize32 537}; 538try { 539 huks.importKeyItem(keyAlias, huksoptions) 540 .then((data) => { 541 console.info(`promise: importKeyItem success`); 542 }) 543 .catch((error: BusinessError) => { 544 console.error(`promise: importKeyItem failed`); 545 }); 546} catch (error) { 547 console.error(`promise: importKeyItem input arg invalid`); 548} 549``` 550 551## huks.attestKeyItem<sup>9+</sup> 552 553attestKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void 554 555获取密钥证书,使用Callback方式回调异步返回结果 。 556 557**系统能力**:SystemCapability.Security.Huks.Extension 558 559**参数:** 560 561| 参数名 | 类型 | 必填 | 说明 | 562| -------- | ---------------------------------------------------- | ---- | --------------------------------------------- | 563| keyAlias | string | 是 | 密钥别名,存放待获取证书密钥的别名。 | 564| options | [HuksOptions](#huksoptions) | 是 | 用于获取证书时指定所需参数与数据。 | 565| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult9)> | 是 | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 | 566 567**错误码:** 568 569以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 570 571| 错误码ID | 错误信息 | 572| -------- | ------------- | 573| 201 | check permission failed. | 574| 401 | argument is invalid. | 575| 801 | api is not supported. | 576| 12000001 | algorithm mode is not supported. | 577| 12000002 | algorithm param is missing. | 578| 12000003 | algorithm param is invalid. | 579| 12000004 | operating file failed. | 580| 12000005 | IPC communication failed. | 581| 12000006 | error occured in crypto engine. | 582| 12000011 | queried entity does not exist. | 583| 12000012 | external error. | 584| 12000014 | memory is insufficient. | 585 586**示例:** 587 588```ts 589import huks from '@ohos.security.huks'; 590class HuksProperties { 591 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 592 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | huks.HuksKeyDigest | 593 huks.HuksKeyStorageType | huks.HuksKeyPadding | huks.HuksKeyGenerateType | 594 huks.HuksCipherMode | Uint8Array = huks.HuksKeyAlg.HUKS_ALG_ECC 595} 596let securityLevel = stringToUint8Array('sec_level'); 597let challenge = stringToUint8Array('challenge_data'); 598let versionInfo = stringToUint8Array('version_info'); 599let keyAliasString = "key attest"; 600function stringToUint8Array(str: string) { 601 let arr: number[] = []; 602 for (let i = 0, j = str.length; i < j; ++i) { 603 arr.push(str.charCodeAt(i)); 604 } 605 let tmpUint8Array = new Uint8Array(arr); 606 return tmpUint8Array; 607} 608 609async function generateKeyThenattestKey(alias: string) { 610 let aliasString = keyAliasString; 611 let aliasUint8 = stringToUint8Array(aliasString); 612 let generateProperties: HuksProperties[] = [ 613 { 614 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 615 value: huks.HuksKeyAlg.HUKS_ALG_RSA 616 }, 617 { 618 tag: huks.HuksTag.HUKS_TAG_KEY_STORAGE_FLAG, 619 value: huks.HuksKeyStorageType.HUKS_STORAGE_PERSISTENT 620 }, 621 { 622 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 623 value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048 624 }, 625 { 626 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 627 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY 628 }, 629 { 630 tag: huks.HuksTag.HUKS_TAG_DIGEST, 631 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 632 }, 633 { 634 tag: huks.HuksTag.HUKS_TAG_PADDING, 635 value: huks.HuksKeyPadding.HUKS_PADDING_PSS 636 }, 637 { 638 tag: huks.HuksTag.HUKS_TAG_KEY_GENERATE_TYPE, 639 value: huks.HuksKeyGenerateType.HUKS_KEY_GENERATE_TYPE_DEFAULT 640 }, 641 { 642 tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, 643 value: huks.HuksCipherMode.HUKS_MODE_ECB 644 } 645 ]; 646 let generateOptions: huks.HuksOptions = { 647 properties: generateProperties 648 }; 649 let attestProperties: HuksProperties[] = [ 650 { 651 tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_SEC_LEVEL_INFO, 652 value: securityLevel 653 }, 654 { 655 tag: huks.HuksTag.HUKS_TAG_ATTESTATION_CHALLENGE, 656 value: challenge 657 }, 658 { 659 tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_VERSION_INFO, 660 value: versionInfo 661 }, 662 { 663 tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_ALIAS, 664 value: aliasUint8 665 } 666 ]; 667 let attestOptions: huks.HuksOptions = { 668 properties: attestProperties 669 }; 670 try { 671 huks.generateKeyItem(alias, generateOptions, (error, data) => { 672 if (error) { 673 console.error(`callback: generateKeyItem failed`); 674 } else { 675 console.info(`callback: generateKeyItem success`); 676 try { 677 huks.attestKeyItem(aliasString, attestOptions, (error, data) => { 678 if (error) { 679 console.error(`callback: attestKeyItem failed`); 680 } else { 681 console.info(`callback: attestKeyItem success`); 682 } 683 }); 684 } catch (error) { 685 console.error(`callback: attestKeyItem input arg invalid`); 686 } 687 } 688 }); 689 } catch (error) { 690 console.error(`callback: generateKeyItem input arg invalid`); 691 } 692} 693``` 694 695## huks.attestKeyItem<sup>9+</sup> 696 697attestKeyItem(keyAlias: string, options: HuksOptions) : Promise\<HuksReturnResult> 698 699获取密钥证书,使用Promise方式异步返回结果 。 700 701**系统能力**:SystemCapability.Security.Huks.Extension 702 703**参数:** 704 705| 参数名 | 类型 | 必填 | 说明 | 706| -------- | --------------------------- | ---- | ------------------------------------ | 707| keyAlias | string | 是 | 密钥别名,存放待获取证书密钥的别名。 | 708| options | [HuksOptions](#huksoptions) | 是 | 用于获取证书时指定所需参数与数据。 | 709 710**返回值:** 711 712| 类型 | 说明 | 713| ---------------------------------------------- | --------------------------------------------- | 714| Promise<[HuksReturnResult](#huksreturnresult9)> | Promise对象。不返回err值时表示接口使用成功,其他时为错误。 | 715 716**错误码:** 717 718以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 719 720| 错误码ID | 错误信息 | 721| -------- | ------------- | 722| 201 | check permission failed. | 723| 401 | argument is invalid. | 724| 801 | api is not supported. | 725| 12000001 | algorithm mode is not supported. | 726| 12000002 | algorithm param is missing. | 727| 12000003 | algorithm param is invalid. | 728| 12000004 | operating file failed. | 729| 12000005 | IPC communication failed. | 730| 12000006 | error occured in crypto engine. | 731| 12000011 | queried entity does not exist. | 732| 12000012 | external error. | 733| 12000014 | memory is insufficient. | 734 735**示例:** 736 737```ts 738import huks from '@ohos.security.huks'; 739import { BusinessError } from '@ohos.base'; 740class HuksProperties { 741 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 742 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | huks.HuksKeyDigest | 743 huks.HuksKeyStorageType | huks.HuksKeyPadding | huks.HuksKeyGenerateType | 744 huks.HuksCipherMode | Uint8Array = huks.HuksKeyAlg.HUKS_ALG_ECC 745} 746let securityLevel = stringToUint8Array('sec_level'); 747let challenge = stringToUint8Array('challenge_data'); 748let versionInfo = stringToUint8Array('version_info'); 749let keyAliasString = "key attest"; 750function stringToUint8Array(str: string) { 751 let arr: number[] = []; 752 for (let i = 0, j = str.length; i < j; ++i) { 753 arr.push(str.charCodeAt(i)); 754 } 755 let tmpUint8Array = new Uint8Array(arr); 756 return tmpUint8Array; 757} 758async function generateKey(alias: string) { 759 let properties: HuksProperties[] = [ 760 { 761 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 762 value: huks.HuksKeyAlg.HUKS_ALG_RSA 763 }, 764 { 765 tag: huks.HuksTag.HUKS_TAG_KEY_STORAGE_FLAG, 766 value: huks.HuksKeyStorageType.HUKS_STORAGE_PERSISTENT 767 }, 768 { 769 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 770 value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048 771 }, 772 { 773 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 774 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY 775 }, 776 { 777 tag: huks.HuksTag.HUKS_TAG_DIGEST, 778 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 779 }, 780 { 781 tag: huks.HuksTag.HUKS_TAG_PADDING, 782 value: huks.HuksKeyPadding.HUKS_PADDING_PSS 783 }, 784 { 785 tag: huks.HuksTag.HUKS_TAG_KEY_GENERATE_TYPE, 786 value: huks.HuksKeyGenerateType.HUKS_KEY_GENERATE_TYPE_DEFAULT 787 }, 788 { 789 tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, 790 value: huks.HuksCipherMode.HUKS_MODE_ECB 791 } 792 ]; 793 let options: huks.HuksOptions = { 794 properties: properties 795 }; 796 try { 797 await huks.generateKeyItem(alias, options) 798 .then((data) => { 799 console.info(`promise: generateKeyItem success`); 800 }) 801 .catch((error: BusinessError) => { 802 console.error(`promise: generateKeyItem failed`); 803 }); 804 } catch (error) { 805 console.error(`promise: generateKeyItem input arg invalid`); 806 } 807} 808async function attestKey() { 809 let aliasString = keyAliasString; 810 let aliasUint8 = stringToUint8Array(aliasString); 811 let properties: HuksProperties[] = [ 812 { 813 tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_SEC_LEVEL_INFO, 814 value: securityLevel 815 }, 816 { 817 tag: huks.HuksTag.HUKS_TAG_ATTESTATION_CHALLENGE, 818 value: challenge 819 }, 820 { 821 tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_VERSION_INFO, 822 value: versionInfo 823 }, 824 { 825 tag: huks.HuksTag.HUKS_TAG_ATTESTATION_ID_ALIAS, 826 value: aliasUint8 827 } 828 ]; 829 let options: huks.HuksOptions = { 830 properties: properties 831 }; 832 await generateKey(aliasString); 833 try { 834 await huks.attestKeyItem(aliasString, options) 835 .then((data) => { 836 console.info(`promise: attestKeyItem success`); 837 }) 838 .catch((error: BusinessError) => { 839 console.error(`promise: attestKeyItem failed`); 840 }); 841 } catch (error) { 842 console.error(`promise: attestKeyItem input arg invalid`); 843 } 844} 845``` 846 847## huks.importWrappedKeyItem<sup>9+</sup> 848 849importWrappedKeyItem(keyAlias: string, wrappingKeyAlias: string, options: HuksOptions, callback: AsyncCallback\<void>) : void 850 851导入加密密钥,使用Callback方式回调异步返回结果 。 852 853**系统能力**:SystemCapability.Security.Huks.Extension 854 855**参数:** 856 857| 参数名 | 类型 | 必填 | 说明 | 858| ---------------- | --------------------------- | ---- | --------------------------------------------- | 859| keyAlias | string | 是 | 密钥别名,存放待导入密钥的别名。 | 860| wrappingKeyAlias | string | 是 | 密钥别名,对应密钥用于解密加密的密钥数据。 | 861| options | [HuksOptions](#huksoptions) | 是 | 用于导入时所需TAG和需要导入的加密的密钥数据。其中密钥使用的算法、密钥用途、密钥长度为必选参数。 | 862| callback | AsyncCallback\<void> | 是 | 回调函数。不返回err值时表示接口使用成功,其他时为错误。 | 863 864**错误码:** 865 866以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 867 868| 错误码ID | 错误信息 | 869| -------- | ------------- | 870| 401 | argument is invalid. | 871| 801 | api is not supported. | 872| 12000001 | algorithm mode is not supported. | 873| 12000002 | algorithm param is missing. | 874| 12000003 | algorithm param is invalid. | 875| 12000004 | operating file failed. | 876| 12000005 | IPC communication failed. | 877| 12000006 | error occured in crypto engine. | 878| 12000011 | queried entity does not exist. | 879| 12000012 | external error. | 880| 12000013 | queried credential does not exist. | 881| 12000014 | memory is insufficient. | 882| 12000015 | call service failed. | 883 884**示例:** 885 886```ts 887import huks from '@ohos.security.huks'; 888import { BusinessError } from '@ohos.base'; 889class HuksProperties { 890 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 891 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | 892 huks.HuksKeyDigest | huks.HuksKeyPadding | huks.HuksUnwrapSuite | 893 huks.HuksCipherMode | huks.HuksImportKeyType = huks.HuksKeyAlg.HUKS_ALG_ECC 894} 895let alias1 = "importAlias"; 896let alias2 = "wrappingKeyAlias"; 897async function TestGenFunc(alias: string, options: huks.HuksOptions) { 898 try { 899 await genKey(alias, options) 900 .then((data) => { 901 console.info(`callback: generateKeyItem success`); 902 }) 903 .catch((error: BusinessError) => { 904 console.error(`callback: generateKeyItem failed`); 905 }); 906 } catch (error) { 907 console.error(`callback: generateKeyItem input arg invalid`); 908 } 909} 910function genKey(alias: string, options: huks.HuksOptions) { 911 return new Promise<void>((resolve, reject) => { 912 try { 913 huks.generateKeyItem(alias, options, (error, data) => { 914 if (error) { 915 reject(error); 916 } else { 917 resolve(data); 918 } 919 }); 920 } catch (error) { 921 throw (new Error(error)); 922 } 923 }); 924} 925async function TestExportFunc(alias: string, options: huks.HuksOptions) { 926 try { 927 await exportKey(alias, options) 928 .then((data) => { 929 console.info(`callback: exportKeyItem success, data = ${JSON.stringify(data)}`); 930 }) 931 .catch((error: BusinessError) => { 932 console.error(`callback: exportKeyItem failed`); 933 }); 934 } catch (error) { 935 console.error(`callback: exportKeyItem input arg invalid`); 936 } 937} 938function exportKey(alias: string, options: huks.HuksOptions) { 939 return new Promise<huks.HuksReturnResult>((resolve, reject) => { 940 try { 941 huks.exportKeyItem(alias, options, (error, data) => { 942 if (error) { 943 reject(error); 944 } else { 945 resolve(data); 946 } 947 }); 948 } catch (error) { 949 throw (new Error(error)); 950 } 951 }); 952} 953async function TestImportWrappedFunc(alias: string, wrappingAlias: string, options: huks.HuksOptions) { 954 try { 955 await importWrappedKey(alias, wrappingAlias, options) 956 .then((data) => { 957 console.info(`callback: importWrappedKeyItem success`); 958 }) 959 .catch((error: BusinessError) => { 960 console.error(`callback: importWrappedKeyItem failed`); 961 }); 962 } catch (error) { 963 console.error(`callback: importWrappedKeyItem input arg invalid`); 964 } 965} 966function importWrappedKey(alias: string, wrappingAlias: string, options: huks.HuksOptions) { 967 return new Promise<void>((resolve, reject) => { 968 try { 969 huks.importWrappedKeyItem(alias, wrappingAlias, options, (error, data) => { 970 if (error) { 971 reject(error); 972 } else { 973 resolve(data); 974 } 975 }); 976 } catch (error) { 977 throw (new Error(error)); 978 } 979 }); 980} 981async function TestImportWrappedKeyFunc( 982 alias: string, 983 wrappingAlias: string, 984 genOptions: huks.HuksOptions, 985 importOptions: huks.HuksOptions 986) { 987 await TestGenFunc(wrappingAlias, genOptions); 988 await TestExportFunc(wrappingAlias, genOptions); 989 990 /* 以下操作不需要调用HUKS接口,此处不给出具体实现。 991 * 假设待导入的密钥为keyA 992 * 1.生成ECC公私钥keyB,公钥为keyB_pub, 私钥为keyB_pri 993 * 2.使用keyB_pri和wrappingAlias密钥中获取的公钥进行密钥协商,协商出共享密钥share_key 994 * 3.随机生成密钥kek,用于加密keyA,采用AES-GCM加密,加密过程中需要记录:nonce1、aad1、加密后的密文keyA_enc、加密后的tag1。 995 * 4.使用share_key加密kek,采用AES-GCM加密,加密过程中需要记录:nonce2、aad2、加密后的密文kek_enc、加密后的tag2。 996 * 5.拼接importOptions.inData字段,满足以下格式: 997 * keyB_pub的长度(4字节) + keyB_pub的数据 + aad2的长度(4字节) + aad2的数据 + 998 * nonce2的长度(4字节) + nonce2的数据 + tag2的长度(4字节) + tag2的数据 + 999 * kek_enc的长度(4字节) + kek_enc的数据 + aad1的长度(4字节) + aad1的数据 + 1000 * nonce1的长度(4字节) + nonce1的数据 + tag1的长度(4字节) + tag1的数据 + 1001 * keyA长度占用的内存长度(4字节) + keyA的长度 + keyA_enc的长度(4字节) + keyA_enc的数据 1002 */ 1003 /* 该处为示例代码,实际运行过程中,应使用实际导入密钥数据。数据构造方式由上注释可见说明 */ 1004 let inputKey = new Uint8Array([0x02, 0x00, 0x00, 0x00]); 1005 importOptions.inData = inputKey; 1006 await TestImportWrappedFunc(alias, wrappingAlias, importOptions); 1007} 1008function makeGenerateOptions() { 1009 let properties: HuksProperties[] = [ 1010 { 1011 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 1012 value: huks.HuksKeyAlg.HUKS_ALG_ECC 1013 }, 1014 { 1015 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 1016 value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_256 1017 }, 1018 { 1019 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 1020 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_UNWRAP 1021 }, 1022 { 1023 tag: huks.HuksTag.HUKS_TAG_DIGEST, 1024 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 1025 }, 1026 { 1027 tag: huks.HuksTag.HUKS_TAG_IMPORT_KEY_TYPE, 1028 value: huks.HuksImportKeyType.HUKS_KEY_TYPE_KEY_PAIR, 1029 } 1030 ]; 1031 let options: huks.HuksOptions = { 1032 properties: properties 1033 }; 1034 return options; 1035}; 1036function makeImportOptions() { 1037 let properties: HuksProperties[] = [ 1038 { 1039 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 1040 value: huks.HuksKeyAlg.HUKS_ALG_AES 1041 }, 1042 { 1043 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 1044 value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256 1045 }, 1046 { 1047 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 1048 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT 1049 }, 1050 { 1051 tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, 1052 value: huks.HuksCipherMode.HUKS_MODE_CBC 1053 }, 1054 { 1055 tag: huks.HuksTag.HUKS_TAG_PADDING, 1056 value: huks.HuksKeyPadding.HUKS_PADDING_NONE 1057 }, 1058 { 1059 tag: huks.HuksTag.HUKS_TAG_UNWRAP_ALGORITHM_SUITE, 1060 value: huks.HuksUnwrapSuite.HUKS_UNWRAP_SUITE_ECDH_AES_256_GCM_NOPADDING 1061 } 1062 ]; 1063 let options: huks.HuksOptions = { 1064 properties: properties 1065 }; 1066 return options; 1067}; 1068function huksImportWrappedKey() { 1069 let genOptions = makeGenerateOptions(); 1070 let importOptions = makeImportOptions(); 1071 TestImportWrappedKeyFunc( 1072 alias1, 1073 alias2, 1074 genOptions, 1075 importOptions 1076 ); 1077} 1078``` 1079 1080## huks.importWrappedKeyItem<sup>9+</sup> 1081 1082importWrappedKeyItem(keyAlias: string, wrappingKeyAlias: string, options: HuksOptions) : Promise\<void> 1083 1084导入加密密钥,使用Promise方式异步返回结果。 1085 1086**系统能力**:SystemCapability.Security.Huks.Extension 1087 1088**参数:** 1089 1090| 参数名 | 类型 | 必填 | 说明 | 1091| ---------------- | --------------------------- | ---- | --------------------------------------------- | 1092| keyAlias | string | 是 | 密钥别名,存放待导入密钥的别名。 | 1093| wrappingKeyAlias | string | 是 | 密钥别名,对应密钥用于解密加密的密钥数据。 | 1094| options | [HuksOptions](#huksoptions) | 是 | 用于导入时所需TAG和需要导入的加密的密钥数据。其中密钥使用的算法、密钥用途、密钥长度为必选参数。 | 1095 1096**错误码:** 1097 1098以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1099 1100| 错误码ID | 错误信息 | 1101| -------- | ------------- | 1102| 401 | argument is invalid. | 1103| 801 | api is not supported. | 1104| 12000001 | algorithm mode is not supported. | 1105| 12000002 | algorithm param is missing. | 1106| 12000003 | algorithm param is invalid. | 1107| 12000004 | operating file failed. | 1108| 12000005 | IPC communication failed. | 1109| 12000006 | error occured in crypto engine. | 1110| 12000011 | queried entity does not exist. | 1111| 12000012 | external error. | 1112| 12000013 | queried credential does not exist. | 1113| 12000014 | memory is insufficient. | 1114| 12000015 | call service failed. | 1115 1116**示例:** 1117 1118```ts 1119import huks from '@ohos.security.huks'; 1120import { BusinessError } from '@ohos.base'; 1121/* 处理流程与callback类似,主要差异点为如下函数: */ 1122/* 该处为示例代码,实际运行过程中,应使用实际导入密钥数据。数据构造方式由上注释可见说明 */ 1123async function TestImportWrappedFunc(alias: string, wrappingAlias: string, options: huks.HuksOptions) { 1124 try { 1125 await huks.importWrappedKeyItem(alias, wrappingAlias, options) 1126 .then ((data) => { 1127 console.info(`promise: importWrappedKeyItem success`); 1128 }) 1129 .catch((error: BusinessError) => { 1130 console.error(`promise: importWrappedKeyItem failed`); 1131 }); 1132 } catch (error) { 1133 console.error(`promise: importWrappedKeyItem input arg invalid`); 1134 } 1135} 1136``` 1137 1138## huks.exportKeyItem<sup>9+</sup> 1139 1140exportKeyItem(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void 1141 1142导出密钥,使用Callback方式回调异步返回的结果。 1143 1144**系统能力**:SystemCapability.Security.Huks.Extension 1145 1146**参数:** 1147 1148| 参数名 | 类型 | 必填 | 说明 | 1149| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ | 1150| keyAlias | string | 是 | 密钥别名,应与所用密钥生成时使用的别名相同。 | 1151| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 1152| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult9)> | 是 | 回调函数。不返回err值时表示接口使用成功,其他时为错误。outData:返回从密钥中导出的公钥。 | 1153 1154**错误码:** 1155 1156以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1157 1158| 错误码ID | 错误信息 | 1159| -------- | ------------- | 1160| 401 | argument is invalid. | 1161| 801 | api is not supported. | 1162| 12000001 | algorithm mode is not supported. | 1163| 12000002 | algorithm param is missing. | 1164| 12000003 | algorithm param is invalid. | 1165| 12000004 | operating file failed. | 1166| 12000005 | IPC communication failed. | 1167| 12000006 | error occured in crypto engine. | 1168| 12000011 | queried entity does not exist. | 1169| 12000012 | external error. | 1170| 12000014 | memory is insufficient. | 1171 1172**示例:** 1173 1174```ts 1175import huks from '@ohos.security.huks'; 1176/* 此处options选择emptyOptions来传空 */ 1177let keyAlias = 'keyAlias'; 1178let emptyOptions: huks.HuksOptions = { 1179 properties: [] 1180}; 1181try { 1182 huks.exportKeyItem(keyAlias, emptyOptions, (error, data) => { 1183 if (error) { 1184 console.error(`callback: exportKeyItem failed`); 1185 } else { 1186 console.info(`callback: exportKeyItem success, data = ${JSON.stringify(data)}`); 1187 } 1188 }); 1189} catch (error) { 1190 console.error(`callback: exportKeyItem input arg invalid`); 1191} 1192``` 1193 1194## huks.exportKeyItem<sup>9+</sup> 1195 1196exportKeyItem(keyAlias: string, options: HuksOptions) : Promise\<HuksReturnResult> 1197 1198导出密钥,使用Promise方式回调异步返回的结果。 1199 1200**系统能力**:SystemCapability.Security.Huks.Extension 1201 1202**参数:** 1203 1204| 参数名 | 类型 | 必填 | 说明 | 1205| -------- | --------------------------- | ---- | -------------------------------------------- | 1206| keyAlias | string | 是 | 密钥别名,应与所用密钥生成时使用的别名相同。 | 1207| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 1208 1209**返回值:** 1210 1211| 类型 | 说明 | 1212| ---------------------------------------------- | ------------------------------------------------------------ | 1213| Promise<[HuksReturnResult](#huksreturnresult9)> | Promise对象。不返回err值时表示接口使用成功,其他时为错误。outData:返回从密钥中导出的公钥。 | 1214 1215**错误码:** 1216 1217以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1218 1219| 错误码ID | 错误信息 | 1220| -------- | ------------- | 1221| 401 | argument is invalid. | 1222| 801 | api is not supported. | 1223| 12000001 | algorithm mode is not supported. | 1224| 12000002 | algorithm param is missing. | 1225| 12000003 | algorithm param is invalid. | 1226| 12000004 | operating file failed. | 1227| 12000005 | IPC communication failed. | 1228| 12000006 | error occured in crypto engine. | 1229| 12000011 | queried entity does not exist. | 1230| 12000012 | external error. | 1231| 12000014 | memory is insufficient. | 1232 1233**示例:** 1234 1235```ts 1236import huks from '@ohos.security.huks'; 1237import { BusinessError } from '@ohos.base'; 1238/* 此处options选择emptyOptions来传空 */ 1239let keyAlias = 'keyAlias'; 1240let emptyOptions: huks.HuksOptions = { 1241 properties: [] 1242}; 1243try { 1244 huks.exportKeyItem(keyAlias, emptyOptions) 1245 .then ((data) => { 1246 console.info(`promise: exportKeyItem success, data = ${JSON.stringify(data)}`); 1247 }) 1248 .catch((error: BusinessError) => { 1249 console.error(`promise: exportKeyItem failed`); 1250 }); 1251} catch (error) { 1252 console.error(`promise: exportKeyItem input arg invalid`); 1253} 1254``` 1255 1256## huks.getKeyItemProperties<sup>9+</sup> 1257 1258getKeyItemProperties(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void 1259 1260获取密钥属性,使用Callback回调异步返回结果。 1261 1262**系统能力**:SystemCapability.Security.Huks.Extension 1263 1264**参数:** 1265 1266| 参数名 | 类型 | 必填 | 说明 | 1267| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ | 1268| keyAlias | string | 是 | 密钥别名,应与所用密钥生成时使用的别名相同。 | 1269| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 1270| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult9)> | 是 | 回调函数。不返回err值时表示接口使用成功,其他时为错误。properties:返回值为生成密钥时所需参数。 | 1271 1272**错误码:** 1273 1274以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1275 1276| 错误码ID | 错误信息 | 1277| -------- | ------------- | 1278| 401 | argument is invalid. | 1279| 801 | api is not supported. | 1280| 12000001 | algorithm mode is not supported. | 1281| 12000002 | algorithm param is missing. | 1282| 12000003 | algorithm param is invalid. | 1283| 12000004 | operating file failed. | 1284| 12000005 | IPC communication failed. | 1285| 12000006 | error occured in crypto engine. | 1286| 12000011 | queried entity does not exist. | 1287| 12000012 | external error. | 1288| 12000014 | memory is insufficient. | 1289 1290**示例:** 1291 1292```ts 1293import huks from '@ohos.security.huks'; 1294/* 此处options选择emptyOptions来传空 */ 1295let keyAlias = 'keyAlias'; 1296let emptyOptions: huks.HuksOptions = { 1297 properties: [] 1298}; 1299try { 1300 huks.getKeyItemProperties(keyAlias, emptyOptions, (error, data) => { 1301 if (error) { 1302 console.error(`callback: getKeyItemProperties failed`); 1303 } else { 1304 console.info(`callback: getKeyItemProperties success, data = ${JSON.stringify(data)}`); 1305 } 1306 }); 1307} catch (error) { 1308 console.error(`callback: getKeyItemProperties input arg invalid`); 1309} 1310``` 1311 1312## huks.getKeyItemProperties<sup>9+</sup> 1313 1314getKeyItemProperties(keyAlias: string, options: HuksOptions) : Promise\<HuksReturnResult> 1315 1316获取密钥属性,使用Promise回调异步返回结果。 1317 1318**系统能力**:SystemCapability.Security.Huks.Extension 1319 1320**参数:** 1321 1322| 参数名 | 类型 | 必填 | 说明 | 1323| -------- | --------------------------- | ---- | -------------------------------------------- | 1324| keyAlias | string | 是 | 密钥别名,应与所用密钥生成时使用的别名相同。 | 1325| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 1326 1327**返回值:** 1328 1329| 类型 | 说明 | 1330| ----------------------------------------------- | ------------------------------------------------------------ | 1331| Promise\<[HuksReturnResult](#huksreturnresult9)> | Promise对象。不返回err值时表示接口使用成功,其他时为错误。properties:返回值为生成密钥时所需参数。 | 1332 1333**错误码:** 1334 1335以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1336 1337| 错误码ID | 错误信息 | 1338| -------- | ------------- | 1339| 401 | argument is invalid. | 1340| 801 | api is not supported. | 1341| 12000001 | algorithm mode is not supported. | 1342| 12000002 | algorithm param is missing. | 1343| 12000003 | algorithm param is invalid. | 1344| 12000004 | operating file failed. | 1345| 12000005 | IPC communication failed. | 1346| 12000006 | error occured in crypto engine. | 1347| 12000011 | queried entity does not exist. | 1348| 12000012 | external error. | 1349| 12000014 | memory is insufficient. | 1350 1351**示例:** 1352 1353```ts 1354import huks from '@ohos.security.huks'; 1355import { BusinessError } from '@ohos.base'; 1356/* 此处options选择emptyOptions来传空 */ 1357let keyAlias = 'keyAlias'; 1358let emptyOptions: huks.HuksOptions = { 1359 properties: [] 1360}; 1361try { 1362 huks.getKeyItemProperties(keyAlias, emptyOptions) 1363 .then ((data) => { 1364 console.info(`promise: getKeyItemProperties success, data = ${JSON.stringify(data)}`); 1365 }) 1366 .catch((error: BusinessError) => { 1367 console.error(`promise: getKeyItemProperties failed`); 1368 }); 1369} catch (error) { 1370 console.error(`promise: getKeyItemProperties input arg invalid`); 1371} 1372``` 1373 1374## huks.isKeyItemExist<sup>9+</sup> 1375 1376isKeyItemExist(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<boolean>) : void 1377 1378判断密钥是否存在,使用Callback回调异步返回结果 。 1379 1380**系统能力**:SystemCapability.Security.Huks.Core 1381 1382**参数:** 1383 1384| 参数名 | 类型 | 必填 | 说明 | 1385| -------- | --------------------------- | ---- | --------------------------------------- | 1386| keyAlias | string | 是 | 所需查找的密钥的别名。 | 1387| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 1388| callback | AsyncCallback\<boolean> | 是 | 回调函数。若密钥存在,data为true,若密钥不存在,则error中会输出密钥不存在的error code。 | 1389 1390**错误码:** 1391 1392以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1393 1394| 错误码ID | 错误信息 | 1395| -------- | ------------- | 1396| 401 | argument is invalid. | 1397| 801 | api is not supported. | 1398| 12000002 | algorithm param is missing. | 1399| 12000003 | algorithm param is invalid. | 1400| 12000004 | operating file failed. | 1401| 12000005 | IPC communication failed. | 1402| 12000006 | error occured in crypto engine. | 1403| 12000011 | The entity does not exist. | 1404| 12000012 | external error. | 1405| 12000014 | memory is insufficient. | 1406 1407**示例:** 1408 1409```ts 1410import huks from '@ohos.security.huks'; 1411import promptAction from '@ohos.promptAction'; 1412/* 此处options选择emptyOptions来传空 */ 1413let keyAlias = 'keyAlias'; 1414let emptyOptions: huks.HuksOptions = { 1415 properties: [] 1416}; 1417huks.isKeyItemExist(keyAlias, emptyOptions, (error, data) => { 1418 if (data) { 1419 promptAction.showToast({ 1420 message: "keyAlias: " + keyAlias +"is existed!", 1421 duration: 2500, 1422 }) 1423 } else { 1424 promptAction.showToast({ 1425 message: "find key failed", 1426 duration: 2500, 1427 }) 1428 } 1429}); 1430``` 1431 1432## huks.isKeyItemExist<sup>9+</sup> 1433 1434isKeyItemExist(keyAlias: string, options: HuksOptions) : Promise\<boolean> 1435 1436判断密钥是否存在,使用Promise回调异步返回结果 。 1437 1438**系统能力**:SystemCapability.Security.Huks.Extension 1439 1440**参数:** 1441 1442| 参数名 | 类型 | 必填 | 说明 | 1443| -------- | --------------------------- | ---- | ------------------------ | 1444| keyAlias | string | 是 | 所需查找的密钥的别名。 | 1445| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 1446 1447**返回值:** 1448 1449| 类型 | 说明 | 1450| ----------------- | --------------------------------------- | 1451| Promise\<boolean> | Promise对象。密钥存在时,可通过then进行密钥存在后的相关处理,若不存在,可通过error处理密钥不存在后的相关业务操作。 | 1452 1453**错误码:** 1454 1455以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1456 1457| 错误码ID | 错误信息 | 1458| -------- | ------------- | 1459| 401 | argument is invalid. | 1460| 801 | api is not supported. | 1461| 12000002 | algorithm param is missing. | 1462| 12000003 | algorithm param is invalid. | 1463| 12000004 | operating file failed. | 1464| 12000005 | IPC communication failed. | 1465| 12000006 | error occured in crypto engine. | 1466| 12000011 | The entity does not exist. | 1467| 12000012 | external error. | 1468| 12000014 | memory is insufficient. | 1469 1470**示例:** 1471 1472```ts 1473import huks from '@ohos.security.huks'; 1474import { BusinessError } from '@ohos.base'; 1475import promptAction from '@ohos.promptAction'; 1476 1477/* 此处options选择emptyOptions来传空 */ 1478let keyAlias = 'keyAlias'; 1479let emptyOptions: huks.HuksOptions = { 1480 properties: [] 1481}; 1482huks.isKeyItemExist(keyAlias, emptyOptions).then((data) => { 1483 promptAction.showToast({ 1484 message: "keyAlias: " + keyAlias +"is existed!", 1485 duration: 500, 1486 }) 1487}).catch((error: BusinessError)=>{ 1488 promptAction.showToast({ 1489 message: "find key failed", 1490 duration: 6500, 1491 }) 1492}) 1493``` 1494 1495## huks.initSession<sup>9+</sup> 1496 1497initSession(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksSessionHandle>) : void 1498 1499initSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。 1500 1501**系统能力**:SystemCapability.Security.Huks.Core 1502 1503**参数:** 1504 1505| 参数名 | 类型 | 必填 | 说明 | 1506| -------- | ------------------------------------------------------- | ---- | ---------------------------------------------------- | 1507| keyAlias | string | 是 | initSession操作密钥的别名。 | 1508| options | [HuksOptions](#huksoptions) | 是 | initSession操作的参数集合。 | 1509| callback | AsyncCallback\<[HuksSessionHandle](#hukssessionhandle9)> | 是 | 回调函数。将initSession操作返回的handle添加到密钥管理系统的回调。 | 1510 1511**错误码:** 1512 1513以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1514 1515| 错误码ID | 错误信息 | 1516| -------- | ------------- | 1517| 401 | argument is invalid. | 1518| 801 | api is not supported. | 1519| 12000001 | algorithm mode is not supported. | 1520| 12000002 | algorithm param is missing. | 1521| 12000003 | algorithm param is invalid. | 1522| 12000004 | operating file failed. | 1523| 12000005 | IPC communication failed. | 1524| 12000006 | error occured in crypto engine. | 1525| 12000010 | the number of sessions has reached limit. | 1526| 12000011 | queried entity does not exist. | 1527| 12000012 | external error. | 1528| 12000014 | memory is insufficient. | 1529 1530## huks.initSession<sup>9+</sup> 1531 1532initSession(keyAlias: string, options: HuksOptions) : Promise\<HuksSessionHandle> 1533 1534initSession操作密钥接口,使用Promise方式异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。 1535 1536**系统能力**:SystemCapability.Security.Huks.Extension 1537 1538**参数:** 1539 1540| 参数名 | 类型 | 必填 | 说明 | 1541| -------- | ------------------------------------------------- | ---- | ------------------------------------------------ | 1542| keyAlias | string | 是 | initSession操作密钥的别名。 | 1543| options | [HuksOptions](#huksoptions) | 是 | initSession参数集合。 | 1544 1545**返回值**: 1546 1547| 类型 | 说明 | 1548| ----------------------------------- | -------------------------------------------------- | 1549| Promise\<[HuksSessionHandle](#hukssessionhandle9)> | Promise对象。将initSession操作返回的handle添加到密钥管理系统的回调。 | 1550 1551**错误码:** 1552 1553以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1554 1555| 错误码ID | 错误信息 | 1556| -------- | ------------- | 1557| 401 | argument is invalid. | 1558| 801 | api is not supported. | 1559| 12000001 | algorithm mode is not supported. | 1560| 12000002 | algorithm param is missing. | 1561| 12000003 | algorithm param is invalid. | 1562| 12000004 | operating file failed. | 1563| 12000005 | IPC communication failed. | 1564| 12000006 | error occured in crypto engine. | 1565| 12000010 | the number of sessions has reached limit. | 1566| 12000011 | queried entity does not exist. | 1567| 12000012 | external error. | 1568| 12000014 | memory is insufficient. | 1569 1570## huks.updateSession<sup>9+</sup> 1571 1572updateSession(handle: number, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void 1573 1574updateSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。 1575 1576**系统能力**:SystemCapability.Security.Huks.Core 1577 1578**参数:** 1579 1580| 参数名 | 类型 | 必填 | 说明 | 1581| -------- | ---------------------------------------------------- | ---- | -------------------------------------------- | 1582| handle | number | 是 | updateSession操作的handle。 | 1583| options | [HuksOptions](#huksoptions) | 是 | updateSession的参数集合。 | 1584| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult9)> | 是 | 回调函数。将updateSession操作的结果添加到密钥管理系统的回调。 | 1585 1586**错误码:** 1587 1588以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1589 1590| 错误码ID | 错误信息 | 1591| -------- | ------------- | 1592| 401 | argument is invalid. | 1593| 801 | api is not supported. | 1594| 12000001 | algorithm mode is not supported. | 1595| 12000002 | algorithm param is missing. | 1596| 12000003 | algorithm param is invalid. | 1597| 12000004 | operating file failed. | 1598| 12000005 | IPC communication failed. | 1599| 12000006 | error occured in crypto engine. | 1600| 12000007 | this credential is already invalidated permanently. | 1601| 12000008 | verify authtoken failed. | 1602| 12000009 | authtoken is already timeout. | 1603| 12000011 | queried entity does not exist. | 1604| 12000012 | external error. | 1605| 12000014 | memory is insufficient. | 1606 1607## huks.updateSession<sup>9+</sup> 1608 1609updateSession(handle: number, options: HuksOptions, token: Uint8Array, callback: AsyncCallback\<HuksReturnResult>) : void 1610 1611updateSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。 1612 1613**系统能力**:SystemCapability.Security.Huks.Extension 1614 1615**参数:** 1616 1617| 参数名 | 类型 | 必填 | 说明 | 1618| -------- | ---------------------------------------------------- | ---- | -------------------------------------------- | 1619| handle | number | 是 | updateSession操作的handle。 | 1620| options | [HuksOptions](#huksoptions) | 是 | updateSession操作的参数集合。 | 1621| token | Uint8Array | 是 | updateSession操作的token。 | 1622| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult9)> | 是 | 回调函数。将updateSession操作的结果添加到密钥管理系统的回调。 | 1623 1624**错误码:** 1625 1626以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1627 1628| 错误码ID | 错误信息 | 1629| -------- | ------------- | 1630| 401 | argument is invalid. | 1631| 801 | api is not supported. | 1632| 12000001 | algorithm mode is not supported. | 1633| 12000002 | algorithm param is missing. | 1634| 12000003 | algorithm param is invalid. | 1635| 12000004 | operating file failed. | 1636| 12000005 | IPC communication failed. | 1637| 12000006 | error occured in crypto engine. | 1638| 12000007 | this credential is already invalidated permanently. | 1639| 12000008 | verify authtoken failed. | 1640| 12000009 | authtoken is already timeout. | 1641| 12000011 | queried entity does not exist. | 1642| 12000012 | external error. | 1643| 12000014 | memory is insufficient. | 1644 1645## huks.updateSession<sup>9+</sup> 1646 1647updateSession(handle: number, options: HuksOptions, token?: Uint8Array) : Promise\<HuksReturnResult> 1648 1649updateSession操作密钥接口,使用Promise方式异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。 1650 1651**系统能力**:SystemCapability.Security.Huks.Extension 1652 1653**参数:** 1654 1655| 参数名 | 类型 | 必填 | 说明 | 1656| ------- | ---------------------------------------------- | ---- | -------------------------------------------- | 1657| handle | number | 是 | updateSession操作的handle。 | 1658| options | [HuksOptions](#huksoptions) | 是 | updateSession操作的参数集合。 | 1659| token | Uint8Array | 否 | updateSession操作的token。 | 1660 1661**返回值**: 1662 1663| 类型 | 说明 | 1664| ----------------------------------- | -------------------------------------------------- | 1665| Promise<[HuksReturnResult](#huksreturnresult9)> | Promise对象。将updateSession操作的结果添加到密钥管理系统的回调。 | 1666 1667**错误码:** 1668 1669以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1670 1671| 错误码ID | 错误信息 | 1672| -------- | ------------- | 1673| 401 | argument is invalid. | 1674| 801 | api is not supported. | 1675| 12000001 | algorithm mode is not supported. | 1676| 12000002 | algorithm param is missing. | 1677| 12000003 | algorithm param is invalid. | 1678| 12000004 | operating file failed. | 1679| 12000005 | IPC communication failed. | 1680| 12000006 | error occured in crypto engine. | 1681| 12000007 | this credential is already invalidated permanently. | 1682| 12000008 | verify authtoken failed. | 1683| 12000009 | authtoken is already timeout. | 1684| 12000011 | queried entity does not exist. | 1685| 12000012 | external error. | 1686| 12000014 | memory is insufficient. | 1687 1688## huks.finishSession<sup>9+</sup> 1689 1690finishSession(handle: number, options: HuksOptions, callback: AsyncCallback\<HuksReturnResult>) : void 1691 1692finishSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。 1693 1694**系统能力**:SystemCapability.Security.Huks.Core 1695 1696**参数:** 1697 1698| 参数名 | 类型 | 必填 | 说明 | 1699| -------- | ---------------------------------------------------- | ---- | -------------------------------------------- | 1700| handle | number | 是 | finishSession操作的handle。 | 1701| options | [HuksOptions](#huksoptions) | 是 | finishSession的参数集合。 | 1702| callback | AsyncCallback<[HuksReturnResult](#huksreturnresult9)> | 是 | 回调函数。将finishSession操作的结果添加到密钥管理系统的回调。 | 1703 1704**错误码:** 1705 1706以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1707 1708| 错误码ID | 错误信息 | 1709| -------- | ------------- | 1710| 401 | argument is invalid. | 1711| 801 | api is not supported. | 1712| 12000001 | algorithm mode is not supported. | 1713| 12000002 | algorithm param is missing. | 1714| 12000003 | algorithm param is invalid. | 1715| 12000004 | operating file failed. | 1716| 12000005 | IPC communication failed. | 1717| 12000006 | error occured in crypto engine. | 1718| 12000007 | this credential is already invalidated permanently. | 1719| 12000008 | verify authtoken failed. | 1720| 12000009 | authtoken is already timeout. | 1721| 12000011 | queried entity does not exist. | 1722| 12000012 | external error. | 1723| 12000014 | memory is insufficient. | 1724 1725## huks.finishSession<sup>9+</sup> 1726 1727finishSession(handle: number, options: HuksOptions, token: Uint8Array, callback: AsyncCallback\<HuksReturnResult>) : void 1728 1729finishSession操作密钥接口,使用Callback回调异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。 1730 1731**系统能力**:SystemCapability.Security.Huks.Extension 1732 1733**参数:** 1734 1735| 参数名 | 类型 | 必填 | 说明 | 1736| -------- | ----------------------------------------------------- | ---- | -------------------------------------------- | 1737| handle | number | 是 | finishSession操作的handle。 | 1738| options | [HuksOptions](#huksoptions) | 是 | finishSession的参数集合。 | 1739| token | Uint8Array | 是 | finishSession操作的token。 | 1740| callback | AsyncCallback\<[HuksReturnResult](#huksreturnresult9)> | 是 | 回调函数。将finishSession操作的结果添加到密钥管理系统的回调。 | 1741 1742**错误码:** 1743 1744以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1745 1746| 错误码ID | 错误信息 | 1747| -------- | ------------- | 1748| 401 | argument is invalid. | 1749| 801 | api is not supported. | 1750| 12000001 | algorithm mode is not supported. | 1751| 12000002 | algorithm param is missing. | 1752| 12000003 | algorithm param is invalid. | 1753| 12000004 | operating file failed. | 1754| 12000005 | IPC communication failed. | 1755| 12000006 | error occured in crypto engine. | 1756| 12000007 | this credential is already invalidated permanently. | 1757| 12000008 | verify authtoken failed. | 1758| 12000009 | authtoken is already timeout. | 1759| 12000011 | queried entity does not exist. | 1760| 12000012 | external error. | 1761| 12000014 | memory is insufficient. | 1762 1763## huks.finishSession<sup>9+</sup> 1764 1765finishSession(handle: number, options: HuksOptions, token?: Uint8Array) : Promise\<HuksReturnResult> 1766 1767finishSession操作密钥接口,使用Promise方式异步返回结果。huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用。 1768 1769**系统能力**:SystemCapability.Security.Huks.Extension 1770 1771**参数:** 1772 1773| 参数名 | 类型 | 必填 | 说明 | 1774| ------- | ----------------------------------------------- | ---- | ----------------------------------- | 1775| handle | number | 是 | finishSession操作的handle。 | 1776| options | [HuksOptions](#huksoptions) | 是 | finishSession操作的参数集合。 | 1777| token | Uint8Array | 否 | finishSession操作的token。 | 1778 1779**返回值**: 1780 1781| 类型 | 说明 | 1782| ----------------------------------- | -------------------------------------------------- | 1783| Promise\<[HuksReturnResult](#huksreturnresult9)> | Promise对象,用于获取异步返回结果。 | 1784 1785**错误码:** 1786 1787以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1788 1789| 错误码ID | 错误信息 | 1790| -------- | ------------- | 1791| 401 | argument is invalid. | 1792| 801 | api is not supported. | 1793| 12000001 | algorithm mode is not supported. | 1794| 12000002 | algorithm param is missing. | 1795| 12000003 | algorithm param is invalid. | 1796| 12000004 | operating file failed. | 1797| 12000005 | IPC communication failed. | 1798| 12000006 | error occured in crypto engine. | 1799| 12000007 | this credential is already invalidated permanently. | 1800| 12000008 | verify authtoken failed. | 1801| 12000009 | authtoken is already timeout. | 1802| 12000011 | queried entity does not exist. | 1803| 12000012 | external error. | 1804| 12000014 | memory is insufficient. | 1805 1806## huks.abortSession<sup>9+</sup> 1807 1808abortSession(handle: number, options: HuksOptions, callback: AsyncCallback\<void>) : void 1809 1810abortSession操作密钥接口,使用Callback回调异步返回结果 。 1811 1812**系统能力**:SystemCapability.Security.Huks.Core 1813 1814**参数:** 1815 1816| 参数名 | 类型 | 必填 | 说明 | 1817| -------- | --------------------------- | ---- | ------------------------------------------- | 1818| handle | number | 是 | abortSession操作的handle。 | 1819| options | [HuksOptions](#huksoptions) | 是 | abortSession操作的参数集合。 | 1820| callback | AsyncCallback\<void> | 是 | 回调函数。将abortSession操作的结果添加到密钥管理系统的回调。 | 1821 1822**错误码:** 1823 1824以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1825 1826| 错误码ID | 错误信息 | 1827| -------- | ------------- | 1828| 401 | argument is invalid. | 1829| 801 | api is not supported. | 1830| 12000004 | operating file failed. | 1831| 12000005 | IPC communication failed. | 1832| 12000006 | error occured in crypto engine. | 1833| 12000012 | external error. | 1834| 12000014 | memory is insufficient. | 1835 1836**示例:** 1837 1838```ts 1839import huks from '@ohos.security.huks'; 1840/* huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用,当 1841 * huks.initSession和huks.updateSession 1842 * 以及huks.finishSession操作中的任一阶段发生错误时, 1843 * 都需要调用huks.abortSession来终止密钥的使用。 1844 * 1845 * 以下以RSA1024密钥的callback功能使用为例 1846 */ 1847class HuksProperties { 1848 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 1849 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | huks.HuksKeyDigest | 1850 huks.HuksKeyPadding | huks.HuksCipherMode = huks.HuksKeyAlg.HUKS_ALG_ECC 1851} 1852function stringToUint8Array(str: string) { 1853 let arr: number[] = []; 1854 for (let i = 0, j = str.length; i < j; ++i) { 1855 arr.push(str.charCodeAt(i)); 1856 } 1857 let tmpUint8Array = new Uint8Array(arr); 1858 return tmpUint8Array; 1859} 1860let keyAlias = "HuksDemoRSA"; 1861let properties: HuksProperties[] = [] 1862let options: huks.HuksOptions = { 1863 properties: properties, 1864 inData: new Uint8Array(0) 1865}; 1866let handle: number = 0; 1867async function generateKey() { 1868 properties[0] = { 1869 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 1870 value: huks.HuksKeyAlg.HUKS_ALG_RSA 1871 }; 1872 properties[1] = { 1873 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 1874 value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024 1875 }; 1876 properties[2] = { 1877 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 1878 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT 1879 }; 1880 properties[3] = { 1881 tag: huks.HuksTag.HUKS_TAG_PADDING, 1882 value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5 1883 }; 1884 properties[4] = { 1885 tag: huks.HuksTag.HUKS_TAG_DIGEST, 1886 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 1887 }; 1888 properties[5] = { 1889 tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, 1890 value: huks.HuksCipherMode.HUKS_MODE_ECB, 1891 } 1892 try { 1893 await huks.generateKeyItem(keyAlias, options, (error, data) => { 1894 if (error) { 1895 console.error(`callback: generateKeyItem failed`); 1896 } else { 1897 console.info(`callback: generateKeyItem success`); 1898 } 1899 }); 1900 } catch (error) { 1901 console.error(`callback: generateKeyItem input arg invalid`); 1902 } 1903} 1904async function huksInit() { 1905 console.log('enter huksInit'); 1906 try { 1907 huks.initSession(keyAlias, options, (error, data) => { 1908 if (error) { 1909 console.error(`callback: initSession failed`); 1910 } else { 1911 console.info(`callback: initSession success, data = ${JSON.stringify(data)}`); 1912 handle = data.handle; 1913 } 1914 }); 1915 } catch (error) { 1916 console.error(`callback: initSession input arg invalid`); 1917 } 1918} 1919async function huksUpdate() { 1920 console.log('enter huksUpdate'); 1921 options.inData = stringToUint8Array("huksHmacTest"); 1922 try { 1923 huks.updateSession(handle, options, (error, data) => { 1924 if (error) { 1925 console.error(`callback: updateSession failed`); 1926 } else { 1927 console.info(`callback: updateSession success, data = ${JSON.stringify(data)}`); 1928 } 1929 }); 1930 } catch (error) { 1931 console.error(`callback: updateSession input arg invalid`); 1932 } 1933} 1934async function huksFinish() { 1935 console.log('enter huksFinish'); 1936 options.inData = new Uint8Array(0); 1937 try { 1938 huks.finishSession(handle, options, (error, data) => { 1939 if (error) { 1940 console.error(`callback: finishSession failed`); 1941 } else { 1942 console.info(`callback: finishSession success, data = ${JSON.stringify(data)}`); 1943 } 1944 }); 1945 } catch (error) { 1946 console.error(`callback: finishSession input arg invalid`); 1947 } 1948} 1949async function huksAbort() { 1950 console.log('enter huksAbort'); 1951 try { 1952 huks.abortSession(handle, options, (error, data) => { 1953 if (error) { 1954 console.error(`callback: abortSession failed`); 1955 } else { 1956 console.info(`callback: abortSession success`); 1957 } 1958 }); 1959 } catch (error) { 1960 console.error(`callback: abortSession input arg invalid`); 1961 } 1962} 1963``` 1964 1965## huks.abortSession<sup>9+</sup> 1966 1967abortSession(handle: number, options: HuksOptions) : Promise\<void>; 1968 1969abortSession操作密钥接口,使用Promise方式异步返回结果。 1970 1971**系统能力**:SystemCapability.Security.Huks.Extension 1972 1973**参数:** 1974 1975| 参数名 | 类型 | 必填 | 说明 | 1976| ------- | --------------------------- | ---- | ------------------------------------------- | 1977| handle | number | 是 | abortSession操作的handle。 | 1978| options | [HuksOptions](#huksoptions) | 是 | abortSession操作的参数集合。 | 1979 1980**返回值**: 1981 1982| 类型 | 说明 | 1983| ----------------------------------- | -------------------------------------------------- | 1984| Promise\<void> | Promise对象。将abortSession操作的结果添加到密钥管理系统的回调。 | 1985 1986**错误码:** 1987 1988以下错误码的详细介绍请参见[HUKS错误码](../errorcodes/errorcode-huks.md)。 1989 1990| 错误码ID | 错误信息 | 1991| -------- | ------------- | 1992| 401 | argument is invalid. | 1993| 801 | api is not supported. | 1994| 12000004 | operating file failed. | 1995| 12000005 | IPC communication failed. | 1996| 12000006 | error occured in crypto engine. | 1997| 12000012 | external error. | 1998| 12000014 | memory is insufficient. | 1999 2000**示例:** 2001 2002```ts 2003import huks from '@ohos.security.huks'; 2004import { BusinessError } from '@ohos.base'; 2005/* huks.initSession, huks.updateSession, huks.finishSession为三段式接口,需要一起使用,当 2006 * huks.initSession和huks.updateSession 2007 * 以及huks.finishSession操作中的任一阶段发生错误时, 2008 * 都需要调用huks.abortSession来终止密钥的使用。 2009 * 2010 * 以下以RSA1024密钥的callback功能使用为例 2011 */ 2012class HuksProperties { 2013 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 2014 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | 2015 huks.HuksKeyDigest | huks.HuksKeyPadding | huks.HuksKeyGenerateType | 2016 huks.HuksCipherMode = huks.HuksKeyAlg.HUKS_ALG_ECC 2017} 2018 2019function stringToUint8Array(str: string) { 2020 let arr: number[] = []; 2021 for (let i = 0, j = str.length; i < j; ++i) { 2022 arr.push(str.charCodeAt(i)); 2023 } 2024 let tmpUint8Array = new Uint8Array(arr); 2025 return tmpUint8Array; 2026} 2027 2028let keyAlias = "HuksDemoRSA"; 2029let properties: HuksProperties[] = [] 2030let options: huks.HuksOptions = { 2031 properties: properties, 2032 inData: new Uint8Array(0) 2033}; 2034let handle: number = 0; 2035 2036async function generateKey() { 2037 properties[0] = { 2038 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 2039 value: huks.HuksKeyAlg.HUKS_ALG_RSA 2040 }; 2041 properties[1] = { 2042 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 2043 value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024 2044 }; 2045 properties[2] = { 2046 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 2047 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT 2048 }; 2049 properties[3] = { 2050 tag: huks.HuksTag.HUKS_TAG_PADDING, 2051 value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5 2052 }; 2053 properties[4] = { 2054 tag: huks.HuksTag.HUKS_TAG_DIGEST, 2055 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 2056 }; 2057 properties[5] = { 2058 tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, 2059 value: huks.HuksCipherMode.HUKS_MODE_ECB, 2060 } 2061 2062 try { 2063 await huks.generateKeyItem(keyAlias, options) 2064 .then((data) => { 2065 console.info(`promise: generateKeyItem success`); 2066 }) 2067 .catch((error: BusinessError) => { 2068 console.error(`promise: generateKeyItem failed`); 2069 }); 2070 } catch (error) { 2071 console.error(`promise: generateKeyItem input arg invalid`); 2072 } 2073} 2074 2075async function huksInit() { 2076 console.log('enter huksInit'); 2077 try { 2078 await huks.initSession(keyAlias, options) 2079 .then((data) => { 2080 console.info(`promise: initSession success, data = ${JSON.stringify(data)}`); 2081 handle = data.handle; 2082 }) 2083 .catch((error: BusinessError) => { 2084 console.error(`promise: initSession key failed`); 2085 }); 2086 } catch (error) { 2087 console.error(`promise: initSession input arg invalid`); 2088 } 2089} 2090 2091async function huksUpdate() { 2092 console.log('enter huksUpdate'); 2093 options.inData = stringToUint8Array("huksHmacTest"); 2094 try { 2095 await huks.updateSession(handle, options) 2096 .then((data) => { 2097 console.info(`promise: updateSession success, data = ${JSON.stringify(data)}`); 2098 }) 2099 .catch((error: BusinessError) => { 2100 console.error(`promise: updateSession failed`); 2101 }); 2102 } catch (error) { 2103 console.error(`promise: updateSession input arg invalid`); 2104 } 2105} 2106 2107async function huksFinish() { 2108 console.log('enter huksFinish'); 2109 options.inData = new Uint8Array(0); 2110 try { 2111 await huks.finishSession(handle, options) 2112 .then((data) => { 2113 console.info(`promise: finishSession success, data = ${JSON.stringify(data)}`); 2114 }) 2115 .catch((error: BusinessError) => { 2116 console.error(`promise: finishSession failed`); 2117 }); 2118 } catch (error) { 2119 console.error(`promise: finishSession input arg invalid`); 2120 } 2121} 2122 2123async function huksAbort() { 2124 console.log('enter huksAbort'); 2125 try { 2126 await huks.abortSession(handle, options) 2127 .then((data) => { 2128 console.info(`promise: abortSession success`); 2129 }) 2130 .catch((error: BusinessError) => { 2131 console.error(`promise: abortSession failed`); 2132 }); 2133 } catch (error) { 2134 console.error(`promise: abortSession input arg invalid`); 2135 } 2136} 2137``` 2138 2139 2140## HuksExceptionErrCode<sup>9+</sup> 2141 2142表示错误码的枚举以及对应的错误信息, 错误码表示错误类型,错误信息展示错误详情。 2143 2144关于错误码的具体信息,可在[错误码参考文档](../errorcodes/errorcode-huks.md)中查看。 2145 2146**系统能力**:SystemCapability.Security.Huks.Core 2147 2148| 名称 | 值 | 说明 | 2149| ---------------------------------------------- | -------- |--------------------------- | 2150| HUKS_ERR_CODE_PERMISSION_FAIL | 201 | 权限错误导致失败。 | 2151| HUKS_ERR_CODE_ILLEGAL_ARGUMENT | 401 | 参数错误导致失败。 | 2152| HUKS_ERR_CODE_NOT_SUPPORTED_API | 801 | 不支持的API。 | 2153| HUKS_ERR_CODE_FEATURE_NOT_SUPPORTED | 12000001 | 不支持的功能/特性。 | 2154| HUKS_ERR_CODE_MISSING_CRYPTO_ALG_ARGUMENT | 12000002 | 缺少密钥算法参数。 | 2155| HUKS_ERR_CODE_INVALID_CRYPTO_ALG_ARGUMENT | 12000003 | 无效密钥算法参数。 | 2156| HUKS_ERR_CODE_FILE_OPERATION_FAIL | 12000004 | 文件操作失败。 | 2157| HUKS_ERR_CODE_COMMUNICATION_FAIL | 12000005 | 通信失败。 | 2158| HUKS_ERR_CODE_CRYPTO_FAIL | 12000006 | 算法库操作失败。 | 2159| HUKS_ERR_CODE_KEY_AUTH_PERMANENTLY_INVALIDATED | 12000007 | 密钥访问失败-密钥访问失效。 | 2160| HUKS_ERR_CODE_KEY_AUTH_VERIFY_FAILED | 12000008 | 密钥访问失败-密钥认证失败。 | 2161| HUKS_ERR_CODE_KEY_AUTH_TIME_OUT | 12000009 | 密钥访问失败-密钥访问超时。 | 2162| HUKS_ERR_CODE_SESSION_LIMIT | 12000010 | 密钥操作会话数已达上限。 | 2163| HUKS_ERR_CODE_ITEM_NOT_EXIST | 12000011 | 目标对象不存在。 | 2164| HUKS_ERR_CODE_EXTERNAL_ERROR | 12000012 | 外部错误。 | 2165| HUKS_ERR_CODE_CREDENTIAL_NOT_EXIST | 12000013 | 缺失所需凭据。 | 2166| HUKS_ERR_CODE_INSUFFICIENT_MEMORY | 12000014 | 内存不足。 | 2167| HUKS_ERR_CODE_CALL_SERVICE_FAILED | 12000015 | 调用其他系统服务失败。 | 2168 2169## HuksKeyPurpose 2170 2171表示密钥用途。 2172 2173**系统能力**:SystemCapability.Security.Huks.Core 2174 2175| 名称 | 值 | 说明 | 2176| ------------------------ | ---- | -------------------------------- | 2177| HUKS_KEY_PURPOSE_ENCRYPT | 1 | 表示密钥用于对明文进行加密操作。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2178| HUKS_KEY_PURPOSE_DECRYPT | 2 | 表示密钥用于对密文进行解密操作。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2179| HUKS_KEY_PURPOSE_SIGN | 4 | 表示密钥用于对数据进行签名。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2180| HUKS_KEY_PURPOSE_VERIFY | 8 | 表示密钥用于验证签名后的数据。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2181| HUKS_KEY_PURPOSE_DERIVE | 16 | 表示密钥用于派生密钥。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2182| HUKS_KEY_PURPOSE_WRAP | 32 | 表示密钥用于加密导出。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2183| HUKS_KEY_PURPOSE_UNWRAP | 64 | 表示密钥加密导入。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2184| HUKS_KEY_PURPOSE_MAC | 128 | 表示密钥用于生成mac消息验证码。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2185| HUKS_KEY_PURPOSE_AGREE | 256 | 表示密钥用于进行密钥协商。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2186 2187## HuksKeyDigest 2188 2189表示摘要算法。 2190 2191**系统能力**:SystemCapability.Security.Huks.Extension 2192 2193| 名称 | 值 | 说明 | 2194| ---------------------- | ---- | ---------------------------------------- | 2195| HUKS_DIGEST_NONE | 0 | 表示无摘要算法。 | 2196| HUKS_DIGEST_MD5 | 1 | 表示MD5摘要算法。 | 2197| HUKS_DIGEST_SM3<sup>9+</sup> | 2 | 表示SM3摘要算法。 | 2198| HUKS_DIGEST_SHA1 | 10 | 表示SHA1摘要算法。 | 2199| HUKS_DIGEST_SHA224 | 11 | 表示SHA224摘要算法。 | 2200| HUKS_DIGEST_SHA256 | 12 | 表示SHA256摘要算法。 | 2201| HUKS_DIGEST_SHA384 | 13 | 表示SHA384摘要算法。 | 2202| HUKS_DIGEST_SHA512 | 14 | 表示SHA512摘要算法。 | 2203 2204## HuksKeyPadding 2205 2206表示补齐算法。 2207 2208**系统能力**:SystemCapability.Security.Huks.Core 2209 2210| 名称 | 值 | 说明 | 2211| ---------------------- | ---- | ---------------------------------------- | 2212| HUKS_PADDING_NONE | 0 | 表示不使用补齐算法。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2213| HUKS_PADDING_OAEP | 1 | 表示使用OAEP补齐算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2214| HUKS_PADDING_PSS | 2 | 表示使用PSS补齐算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2215| HUKS_PADDING_PKCS1_V1_5 | 3 | 表示使用PKCS1_V1_5补齐算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2216| HUKS_PADDING_PKCS5 | 4 | 表示使用PKCS5补齐算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2217| HUKS_PADDING_PKCS7 | 5 | 表示使用PKCS7补齐算法。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2218 2219## HuksCipherMode 2220 2221表示加密模式。 2222 2223**系统能力**:SystemCapability.Security.Huks.Core 2224 2225| 名称 | 值 | 说明 | 2226| ------------- | ---- | --------------------- | 2227| HUKS_MODE_ECB | 1 | 表示使用ECB加密模式。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2228| HUKS_MODE_CBC | 2 | 表示使用CBC加密模式。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2229| HUKS_MODE_CTR | 3 | 表示使用CTR加密模式。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2230| HUKS_MODE_OFB | 4 | 表示使用OFB加密模式。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2231| HUKS_MODE_CCM | 31 | 表示使用CCM加密模式。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2232| HUKS_MODE_GCM | 32 | 表示使用GCM加密模式。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2233 2234## HuksKeySize 2235 2236表示密钥长度。 2237 2238**系统能力**:SystemCapability.Security.Huks.Core 2239 2240| 名称 | 值 | 说明 | 2241| ---------------------------------- | ---- | ------------------------------------------ | 2242| HUKS_RSA_KEY_SIZE_512 | 512 | 表示使用RSA算法的密钥长度为512bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2243| HUKS_RSA_KEY_SIZE_768 | 768 | 表示使用RSA算法的密钥长度为768bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2244| HUKS_RSA_KEY_SIZE_1024 | 1024 | 表示使用RSA算法的密钥长度为1024bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2245| HUKS_RSA_KEY_SIZE_2048 | 2048 | 表示使用RSA算法的密钥长度为2048bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2246| HUKS_RSA_KEY_SIZE_3072 | 3072 | 表示使用RSA算法的密钥长度为3072bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2247| HUKS_RSA_KEY_SIZE_4096 | 4096 | 表示使用RSA算法的密钥长度为4096bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2248| HUKS_ECC_KEY_SIZE_224 | 224 | 表示使用ECC算法的密钥长度为224bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2249| HUKS_ECC_KEY_SIZE_256 | 256 | 表示使用ECC算法的密钥长度为256bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2250| HUKS_ECC_KEY_SIZE_384 | 384 | 表示使用ECC算法的密钥长度为384bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2251| HUKS_ECC_KEY_SIZE_521 | 521 | 表示使用ECC算法的密钥长度为521bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2252| HUKS_AES_KEY_SIZE_128 | 128 | 表示使用AES算法的密钥长度为128bit。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2253| HUKS_AES_KEY_SIZE_192 | 192 | 表示使用AES算法的密钥长度为192bit。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2254| HUKS_AES_KEY_SIZE_256 | 256 | 表示使用AES算法的密钥长度为256bit。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2255| HUKS_AES_KEY_SIZE_512 | 512 | 表示使用AES算法的密钥长度为512bit。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2256| HUKS_CURVE25519_KEY_SIZE_256 | 256 | 表示使用CURVE25519算法的密钥长度为256bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2257| HUKS_DH_KEY_SIZE_2048 | 2048 | 表示使用DH算法的密钥长度为2048bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2258| HUKS_DH_KEY_SIZE_3072 | 3072 | 表示使用DH算法的密钥长度为3072bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2259| HUKS_DH_KEY_SIZE_4096 | 4096 | 表示使用DH算法的密钥长度为4096bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2260| HUKS_SM2_KEY_SIZE_256<sup>9+</sup> | 256 | 表示SM2算法的密钥长度为256bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2261| HUKS_SM4_KEY_SIZE_128<sup>9+</sup> | 128 | 表示SM4算法的密钥长度为128bit。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2262 2263## HuksKeyAlg 2264 2265表示密钥使用的算法。 2266 2267**系统能力**:SystemCapability.Security.Huks.Core 2268 2269| 名称 | 值 | 说明 | 2270| ------------------------- | ---- | --------------------- | 2271| HUKS_ALG_RSA | 1 | 表示使用RSA算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2272| HUKS_ALG_ECC | 2 | 表示使用ECC算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2273| HUKS_ALG_DSA | 3 | 表示使用DSA算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2274| HUKS_ALG_AES | 20 | 表示使用AES算法。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2275| HUKS_ALG_HMAC | 50 | 表示使用HMAC算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2276| HUKS_ALG_HKDF | 51 | 表示使用HKDF算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2277| HUKS_ALG_PBKDF2 | 52 | 表示使用PBKDF2算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2278| HUKS_ALG_ECDH | 100 | 表示使用ECDH算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2279| HUKS_ALG_X25519 | 101 | 表示使用X25519算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2280| HUKS_ALG_ED25519 | 102 | 表示使用ED25519算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2281| HUKS_ALG_DH | 103 | 表示使用DH算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2282| HUKS_ALG_SM2<sup>9+</sup> | 150 | 表示使用SM2算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2283| HUKS_ALG_SM3<sup>9+</sup> | 151 | 表示使用SM3算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2284| HUKS_ALG_SM4<sup>9+</sup> | 152 | 表示使用SM4算法。 <br> **系统能力:** SystemCapability.Security.Huks.Extension| 2285 2286## HuksKeyGenerateType 2287 2288表示生成密钥的类型。 2289 2290**系统能力**:SystemCapability.Security.Huks.Extension 2291 2292| 名称 | 值 | 说明 | 2293| ------------------------------ | ---- | ---------------- | 2294| HUKS_KEY_GENERATE_TYPE_DEFAULT | 0 | 默认生成的密钥。 | 2295| HUKS_KEY_GENERATE_TYPE_DERIVE | 1 | 派生生成的密钥。 | 2296| HUKS_KEY_GENERATE_TYPE_AGREE | 2 | 协商生成的密钥。 | 2297 2298## HuksKeyFlag 2299 2300表示密钥的产生方式。 2301 2302**系统能力**:SystemCapability.Security.Huks.Core 2303 2304| 名称 | 值 | 说明 | 2305| -------------------------- | ---- | ------------------------------------ | 2306| HUKS_KEY_FLAG_IMPORT_KEY | 1 | 表示通过导入公钥接口导入的密钥。 | 2307| HUKS_KEY_FLAG_GENERATE_KEY | 2 | 表示通过生成密钥接口生成的密钥。 | 2308| HUKS_KEY_FLAG_AGREE_KEY | 3 | 表示通过生成密钥协商接口生成的密钥。 | 2309| HUKS_KEY_FLAG_DERIVE_KEY | 4 | 表示通过生成密钥派生接口生成的密钥。 | 2310 2311## HuksKeyStorageType 2312 2313表示密钥存储方式。 2314 2315**系统能力**:SystemCapability.Security.Huks.Core 2316 2317| 名称 | 值 | 说明 | 2318| -------------------------------------------- | ---- | ------------------------------ | 2319| HUKS_STORAGE_TEMP<sup>(deprecated)</sup> | 0 | 表示通过本地直接管理密钥。<br/> > **说明:** 从API version 10开始废弃,由于开发者正常使用密钥管理过程中并不需要使用此TAG,故无替代接口。针对针对密钥派生场景,可使用HUKS_STORAGE_ONLY_USED_IN_HUKS 与 HUKS_STORAGE_KEY_EXPORT_ALLOWED。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2320| HUKS_STORAGE_PERSISTENT<sup>(deprecated)</sup> | 1 | 表示通过HUKS service管理密钥。<br/> > **说明:** 从API version 10开始废弃,由于开发者正常使用密钥管理过程中并不需要使用此TAG,故无替代接口。针对密钥派生场景,可使用HUKS_STORAGE_ONLY_USED_IN_HUKS 与 HUKS_STORAGE_KEY_EXPORT_ALLOWED。 <br> **系统能力:** SystemCapability.Security.Huks.Core| 2321| HUKS_STORAGE_ONLY_USED_IN_HUKS<sup>10+</sup> | 2 | 表示主密钥派生的密钥存储于huks中,由HUKS进行托管。<br> **系统能力:** SystemCapability.Security.Huks.Extension| 2322| HUKS_STORAGE_KEY_EXPORT_ALLOWED<sup>10+</sup> | 3 | 表示主密钥派生的密钥直接导出给业务方,HUKS不对其进行托管服务。<br> **系统能力:** SystemCapability.Security.Huks.Extension| 2323 2324## HuksSendType 2325 2326表示发送Tag的方式。 2327 2328**系统能力**:SystemCapability.Security.Huks.Extension 2329 2330| 名称 | 值 | 说明 | 2331| -------------------- | ---- | ----------------- | 2332| HUKS_SEND_TYPE_ASYNC | 0 | 表示异步发送TAG。 | 2333| HUKS_SEND_TYPE_SYNC | 1 | 表示同步发送TAG。 | 2334 2335## HuksUnwrapSuite<sup>9+</sup> 2336 2337表示导入加密密钥的算法套件。 2338 2339**系统能力**:SystemCapability.Security.Huks.Extension 2340 2341| 名称 | 值 | 说明 | 2342| ---------------------------------------------- | ---- | ----------------------------------------------------- | 2343| HUKS_UNWRAP_SUITE_X25519_AES_256_GCM_NOPADDING | 1 | 导入加密密钥时,X25519密钥协商后使用AES-256 GCM加密。 | 2344| HUKS_UNWRAP_SUITE_ECDH_AES_256_GCM_NOPADDING | 2 | 导入加密密钥时,ECDH密钥协商后使用AES-256 GCM加密。 | 2345 2346## HuksImportKeyType<sup>9+</sup> 2347 2348表示导入密钥的密钥类型,默认为导入公钥,导入对称密钥时不需要该字段。 2349 2350**系统能力**:SystemCapability.Security.Huks.Extension 2351 2352| 名称 | 值 | 说明 | 2353| ------------------------- | ---- | ------------------------------ | 2354| HUKS_KEY_TYPE_PUBLIC_KEY | 0 | 表示导入的密钥类型为公钥。 | 2355| HUKS_KEY_TYPE_PRIVATE_KEY | 1 | 表示导入的密钥类型为私钥。 | 2356| HUKS_KEY_TYPE_KEY_PAIR | 2 | 表示导入的密钥类型为公私钥对。 | 2357 2358## HuksRsaPssSaltLenType<sup>10+</sup> 2359 2360表示Rsa在签名验签、padding为pss时需指定的salt_len类型。 2361 2362**系统能力**:SystemCapability.Security.Huks.Extension 2363 2364| 名称 | 值 | 说明 | 2365| ------------------------------------------ | ---- | ---------------------------- | 2366| HUKS_RSA_PSS_SALT_LEN_DIGEST<sup>10+</sup> | 0 | 表示以摘要长度设置salt_len。 | 2367| HUKS_RSA_PSS_SALT_LEN_MAX<sup>10+</sup> | 1 | 表示以最大长度设置salt_len。 | 2368 2369## HuksUserAuthType<sup>9+</sup> 2370 2371表示用户认证类型。 2372 2373**系统能力**:SystemCapability.Security.Huks.Extension 2374 2375| 名称 | 值 | 说明 | 2376| ------------------------------- | ---- | ------------------------- | 2377| HUKS_USER_AUTH_TYPE_FINGERPRINT | 1 << 0 | 表示用户认证类型为指纹。 | 2378| HUKS_USER_AUTH_TYPE_FACE | 1 << 1 | 表示用户认证类型为人脸 。 | 2379| HUKS_USER_AUTH_TYPE_PIN | 1 << 2 | 表示用户认证类型为PIN码。 | 2380 2381## HuksAuthAccessType<sup>9+</sup> 2382 2383表示安全访问控制类型。 2384 2385**系统能力**:SystemCapability.Security.Huks.Extension 2386 2387| 名称 | 值 | 说明 | 2388| --------------------------------------- | ---- | ------------------------------------------------ | 2389| HUKS_AUTH_ACCESS_INVALID_CLEAR_PASSWORD | 1 << 0 | 表示安全访问控制类型为清除密码后密钥无效。 | 2390| HUKS_AUTH_ACCESS_INVALID_NEW_BIO_ENROLL | 1 << 1 | 表示安全访问控制类型为新录入生物特征后密钥无效。 | 2391 2392## HuksChallengeType<sup>9+</sup> 2393 2394表示密钥使用时生成challenge的类型。 2395 2396**系统能力**:SystemCapability.Security.Huks.Extension 2397 2398| 名称 | 值 | 说明 | 2399| ------------------------------- | ---- | ------------------------------ | 2400| HUKS_CHALLENGE_TYPE_NORMAL | 0 | 表示challenge为普通类型,默认32字节。 | 2401| HUKS_CHALLENGE_TYPE_CUSTOM | 1 | 表示challenge为用户自定义类型。支持使用多个密钥仅一次认证。 | 2402| HUKS_CHALLENGE_TYPE_NONE | 2 | 表示免challenge类型。 | 2403 2404## HuksChallengePosition<sup>9+</sup> 2405 2406表示challenge类型为用户自定义类型时,生成的challenge有效长度仅为8字节连续的数据,且仅支持4种位置 。 2407 2408**系统能力**:SystemCapability.Security.Huks.Extension 2409 2410| 名称 | 值 | 说明 | 2411| ------------------------------- | ---- | ------------------------------ | 2412| HUKS_CHALLENGE_POS_0 | 0 | 表示0~7字节为当前密钥的有效challenge。 | 2413| HUKS_CHALLENGE_POS_1 | 1 | 表示8~15字节为当前密钥的有效challenge。 | 2414| HUKS_CHALLENGE_POS_2 | 2 | 表示16~23字节为当前密钥的有效challenge。 | 2415| HUKS_CHALLENGE_POS_3 | 3 | 表示24~31字节为当前密钥的有效challenge。 | 2416 2417## HuksSecureSignType<sup>9+</sup> 2418 2419表示生成或导入密钥时,指定该密钥的签名类型。 2420 2421**系统能力**:SystemCapability.Security.Huks.Extension 2422 2423| 名称 | 值 | 说明 | 2424| ------------------------------ | ---- | ------------------------------------------------------------ | 2425| HUKS_SECURE_SIGN_WITH_AUTHINFO | 1 | 表示签名类型为携带认证信息。生成或导入密钥时指定该字段,则在使用密钥进行签名时,对待签名的数据添加认证信息后进行签名。 | 2426 2427## HuksTagType 2428 2429表示Tag的数据类型。 2430 2431**系统能力**:SystemCapability.Security.Huks.Core 2432 2433| 名称 | 值 | 说明 | 2434| --------------------- | ------- | --------------------------------------- | 2435| HUKS_TAG_TYPE_INVALID | 0 << 28 | 表示非法的Tag类型。 | 2436| HUKS_TAG_TYPE_INT | 1 << 28 | 表示该Tag的数据类型为int类型的number。 | 2437| HUKS_TAG_TYPE_UINT | 2 << 28 | 表示该Tag的数据类型为uint类型的number。 | 2438| HUKS_TAG_TYPE_ULONG | 3 << 28 | 表示该Tag的数据类型为bigint。 | 2439| HUKS_TAG_TYPE_BOOL | 4 << 28 | 表示该Tag的数据类型为boolean。 | 2440| HUKS_TAG_TYPE_BYTES | 5 << 28 | 表示该Tag的数据类型为Uint8Array。 | 2441 2442## HuksTag 2443 2444表示调用参数的Tag。 2445 2446**系统能力**:SystemCapability.Security.Huks.Core 2447 2448| 名称 | 值 | 说明 | 2449| ----------------------------------------------------------- | ---------------------------------------- | ------------------------------------------------------------ | 2450| HUKS_TAG_INVALID<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_INVALID \| 0 | 表示非法的Tag。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2451| HUKS_TAG_ALGORITHM | HuksTagType.HUKS_TAG_TYPE_UINT \| 1 | 表示算法的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2452| HUKS_TAG_PURPOSE | HuksTagType.HUKS_TAG_TYPE_UINT \| 2 | 表示密钥用途的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2453| HUKS_TAG_KEY_SIZE | HuksTagType.HUKS_TAG_TYPE_UINT \| 3 | 表示密钥长度的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2454| HUKS_TAG_DIGEST | HuksTagType.HUKS_TAG_TYPE_UINT \| 4 | 表示摘要算法的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2455| HUKS_TAG_PADDING | HuksTagType.HUKS_TAG_TYPE_UINT \| 5 | 表示补齐算法的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2456| HUKS_TAG_BLOCK_MODE | HuksTagType.HUKS_TAG_TYPE_UINT \| 6 | 表示加密模式的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2457| HUKS_TAG_KEY_TYPE | HuksTagType.HUKS_TAG_TYPE_UINT \| 7 | 表示密钥类型的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2458| HUKS_TAG_ASSOCIATED_DATA | HuksTagType.HUKS_TAG_TYPE_BYTES \| 8 | 表示附加身份验证数据的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2459| HUKS_TAG_NONCE | HuksTagType.HUKS_TAG_TYPE_BYTES \| 9 | 表示密钥加解密的字段。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2460| HUKS_TAG_IV | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10 | 表示密钥初始化的向量。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2461| HUKS_TAG_INFO | HuksTagType.HUKS_TAG_TYPE_BYTES \| 11 | 表示密钥派生时的info。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2462| HUKS_TAG_SALT | HuksTagType.HUKS_TAG_TYPE_BYTES \| 12 | 表示密钥派生时的盐值。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2463| HUKS_TAG_PWD<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 13 | 表示密钥派生时的password。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2464| HUKS_TAG_ITERATION | HuksTagType.HUKS_TAG_TYPE_UINT \| 14 | 表示密钥派生时的迭代次数。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2465| HUKS_TAG_KEY_GENERATE_TYPE | HuksTagType.HUKS_TAG_TYPE_UINT \| 15 | 表示生成密钥类型的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2466| HUKS_TAG_DERIVE_MAIN_KEY<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 16 | 表示密钥派生时的主密钥。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2467| HUKS_TAG_DERIVE_FACTOR<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 17 | 表示密钥派生时的派生因子。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2468| HUKS_TAG_DERIVE_ALG<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 18 | 表示密钥派生时的算法类型。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2469| HUKS_TAG_AGREE_ALG | HuksTagType.HUKS_TAG_TYPE_UINT \| 19 | 表示密钥协商时的算法类型。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2470| HUKS_TAG_AGREE_PUBLIC_KEY_IS_KEY_ALIAS | HuksTagType.HUKS_TAG_TYPE_BOOL \| 20 | 表示密钥协商时的公钥别名。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2471| HUKS_TAG_AGREE_PRIVATE_KEY_ALIAS | HuksTagType.HUKS_TAG_TYPE_BYTES \| 21 | 表示密钥协商时的私钥别名。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2472| HUKS_TAG_AGREE_PUBLIC_KEY | HuksTagType.HUKS_TAG_TYPE_BYTES \| 22 | 表示密钥协商时的公钥。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2473| HUKS_TAG_KEY_ALIAS | HuksTagType.HUKS_TAG_TYPE_BYTES \| 23 | 表示密钥别名。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2474| HUKS_TAG_DERIVE_KEY_SIZE | HuksTagType.HUKS_TAG_TYPE_UINT \| 24 | 表示派生密钥的大小。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2475| HUKS_TAG_IMPORT_KEY_TYPE<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 25 | 表示导入的密钥类型。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2476| HUKS_TAG_UNWRAP_ALGORITHM_SUITE<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 26 | 表示导入加密密钥的套件。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2477| HUKS_TAG_DERIVED_AGREED_KEY_STORAGE_FLAG<sup>10+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \|29 | 表示派生密钥/协商密钥的存储类型。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2478| HUKS_TAG_RSA_PSS_SALT_LEN_TYPE<sup>10+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \|30 | 表示rsa_pss_salt_length的类型。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2479| HUKS_TAG_ACTIVE_DATETIME<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_ULONG \| 201 | 原为证书业务预留字段,当前证书管理已独立,此字段废弃,不再预留。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2480| HUKS_TAG_ORIGINATION_EXPIRE_DATETIME<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_ULONG \| 202 | 原为证书业务预留字段,当前证书管理已独立,此字段废弃,不再预留。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2481| HUKS_TAG_USAGE_EXPIRE_DATETIME<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_ULONG \| 203 | 原为证书业务预留字段,当前证书管理已独立,此字段废弃,不再预留。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2482| HUKS_TAG_CREATION_DATETIME<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_ULONG \| 204 | 原为证书业务预留字段,当前证书管理已独立,此字段废弃,不再预留。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2483| HUKS_TAG_ALL_USERS | HuksTagType.HUKS_TAG_TYPE_BOOL \| 301 | 预留 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2484| HUKS_TAG_USER_ID | HuksTagType.HUKS_TAG_TYPE_UINT \| 302 | 表示当前密钥属于哪个userID <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2485| HUKS_TAG_NO_AUTH_REQUIRED | HuksTagType.HUKS_TAG_TYPE_BOOL \| 303 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2486| 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 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2487| HUKS_TAG_AUTH_TIMEOUT | HuksTagType.HUKS_TAG_TYPE_UINT \| 305 | 表示authtoken单次有效期。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2488| HUKS_TAG_AUTH_TOKEN | HuksTagType.HUKS_TAG_TYPE_BYTES \| 306 | 用于传入authToken的字段 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2489| HUKS_TAG_KEY_AUTH_ACCESS_TYPE<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 307 | 表示安全访问控制类型。从[HuksAuthAccessType](#huksauthaccesstype9)中选择,需要和用户认证类型同时设置。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2490| HUKS_TAG_KEY_SECURE_SIGN_TYPE<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 308 | 表示生成或导入密钥时,指定该密钥的签名类型。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2491| HUKS_TAG_CHALLENGE_TYPE<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 309 | 表示密钥使用时生成的challenge类型。从[HuksChallengeType](#hukschallengetype9)中选择 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2492| HUKS_TAG_CHALLENGE_POS<sup>9+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 310 | 表示challenge类型为用户自定义类型时,huks产生的challenge有效长度仅为8字节连续的数据。从[HuksChallengePosition](#hukschallengeposition9)中选择。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2493| HUKS_TAG_KEY_AUTH_PURPOSE<sup>10+</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \|311 | 表示密钥认证用途的tag <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2494| HUKS_TAG_ATTESTATION_CHALLENGE | HuksTagType.HUKS_TAG_TYPE_BYTES \| 501 | 表示attestation时的挑战值。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2495| HUKS_TAG_ATTESTATION_APPLICATION_ID | HuksTagType.HUKS_TAG_TYPE_BYTES \| 502 | 表示attestation时拥有该密钥的application的Id。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2496| HUKS_TAG_ATTESTATION_ID_BRAND<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 503 | 表示设备的品牌。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2497| HUKS_TAG_ATTESTATION_ID_DEVICE<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 504 | 表示设备的设备ID。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2498| HUKS_TAG_ATTESTATION_ID_PRODUCT<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 505 | 表示设备的产品名。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2499| HUKS_TAG_ATTESTATION_ID_SERIAL<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 506 | 表示设备的SN号。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2500| HUKS_TAG_ATTESTATION_ID_IMEI<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 507 | 表示设备的IMEI号。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2501| HUKS_TAG_ATTESTATION_ID_MEID<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 508 | 表示设备的MEID号。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2502| HUKS_TAG_ATTESTATION_ID_MANUFACTURER<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 509 | 表示设备的制造商。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2503| HUKS_TAG_ATTESTATION_ID_MODEL<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 510 | 表示设备的型号。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2504| HUKS_TAG_ATTESTATION_ID_ALIAS | HuksTagType.HUKS_TAG_TYPE_BYTES \| 511 | 表示attestation时的密钥别名。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2505| HUKS_TAG_ATTESTATION_ID_SOCID<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 512 | 表示设备的SOCID。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2506| HUKS_TAG_ATTESTATION_ID_UDID<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 513 | 表示设备的UDID。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2507| HUKS_TAG_ATTESTATION_ID_SEC_LEVEL_INFO | HuksTagType.HUKS_TAG_TYPE_BYTES \| 514 | 表示attestation时的安全凭据。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2508| HUKS_TAG_ATTESTATION_ID_VERSION_INFO | HuksTagType.HUKS_TAG_TYPE_BYTES \| 515 | 表示attestation时的版本号。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2509| HUKS_TAG_IS_KEY_ALIAS | HuksTagType.HUKS_TAG_TYPE_BOOL \| 1001 | 表示是否使用生成key时传入的别名的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2510| HUKS_TAG_KEY_STORAGE_FLAG | HuksTagType.HUKS_TAG_TYPE_UINT \| 1002 | 表示密钥存储方式的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2511| HUKS_TAG_IS_ALLOWED_WRAP | HuksTagType.HUKS_TAG_TYPE_BOOL \| 1003 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2512| HUKS_TAG_KEY_WRAP_TYPE | HuksTagType.HUKS_TAG_TYPE_UINT \| 1004 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2513| HUKS_TAG_KEY_AUTH_ID | HuksTagType.HUKS_TAG_TYPE_BYTES \| 1005 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2514| HUKS_TAG_KEY_ROLE | HuksTagType.HUKS_TAG_TYPE_UINT \| 1006 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2515| HUKS_TAG_KEY_FLAG | HuksTagType.HUKS_TAG_TYPE_UINT \| 1007 | 表示密钥标志的Tag。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2516| HUKS_TAG_IS_ASYNCHRONIZED | HuksTagType.HUKS_TAG_TYPE_UINT \| 1008 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2517| HUKS_TAG_SECURE_KEY_ALIAS<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BOOL \| 1009 | 原为预留字段,从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2518| HUKS_TAG_SECURE_KEY_UUID<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 1010 | 原为预留字段,从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2519| HUKS_TAG_KEY_DOMAIN | HuksTagType.HUKS_TAG_TYPE_UINT \| 1011 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2520| HUKS_TAG_PROCESS_NAME<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10001 | 表示进程名称的Tag。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2521| HUKS_TAG_PACKAGE_NAME<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10002 | 原为预留字段,从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2522| HUKS_TAG_ACCESS_TIME<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 10003 | 原为预留字段,从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2523| HUKS_TAG_USES_TIME<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 10004 | 原为预留字段,从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2524| HUKS_TAG_CRYPTO_CTX<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_ULONG \| 10005 | 原为预留字段,从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2525| HUKS_TAG_KEY | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10006 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2526| HUKS_TAG_KEY_VERSION<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 10007 | 表示密钥版本的Tag。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2527| HUKS_TAG_PAYLOAD_LEN<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 10008 | 原为预留字段,从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2528| HUKS_TAG_AE_TAG | HuksTagType.HUKS_TAG_TYPE_BYTES \| 10009 | 用于传入GCM模式中的AEAD数据的字段。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2529| HUKS_TAG_IS_KEY_HANDLE<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_ULONG \| 10010 | 原为预留字段,从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2530| HUKS_TAG_OS_VERSION<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 10101 | 表示操作系统版本的Tag。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2531| HUKS_TAG_OS_PATCHLEVEL<sup>(deprecated)</sup> | HuksTagType.HUKS_TAG_TYPE_UINT \| 10102 | 表示操作系统补丁级别的Tag。从API version 9开始废弃。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2532| HUKS_TAG_SYMMETRIC_KEY_DATA | HuksTagType.HUKS_TAG_TYPE_BYTES \| 20001 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Core | 2533| HUKS_TAG_ASYMMETRIC_PUBLIC_KEY_DATA | HuksTagType.HUKS_TAG_TYPE_BYTES \| 20002 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2534| HUKS_TAG_ASYMMETRIC_PRIVATE_KEY_DATA | HuksTagType.HUKS_TAG_TYPE_BYTES \| 20003 | 预留。 <br> **系统能力:** SystemCapability.Security.Huks.Extension | 2535 2536## huks.generateKey<sup>(deprecated)</sup> 2537 2538generateKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void 2539 2540生成密钥,使用Callback回调异步返回结果。 2541 2542> **说明:** 2543> 2544> 从API version 9开始废弃,建议使用[huks.generateKeyItem<sup>9+</sup>](#huksgeneratekeyitem9)替代。 2545 2546**系统能力**:SystemCapability.Security.Huks.Extension 2547 2548**参数:** 2549 2550| 参数名 | 类型 | 必填 | 说明 | 2551| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2552| keyAlias | string | 是 | 别名。 | 2553| options | [HuksOptions](#huksoptions) | 是 | 用于存放生成key所需TAG。 | 2554| callback | AsyncCallback\<[HuksResult](#huksresultdeprecated)> | 是 | 回调函数。返回HUKS_SUCCESS时表示接口使用成功,其余结果请参考HuksResult进行错误码查询。 | 2555 2556**示例:** 2557 2558```ts 2559import huks from '@ohos.security.huks'; 2560/* 以生成RSA512密钥为例 */ 2561class HuksProperties { 2562 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 2563 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | 2564 huks.HuksKeyDigest | huks.HuksKeyPadding = huks.HuksKeyAlg.HUKS_ALG_ECC 2565} 2566let keyAlias = 'keyAlias'; 2567let properties: HuksProperties[] = [ 2568 { 2569 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 2570 value: huks.HuksKeyAlg.HUKS_ALG_RSA 2571 }, 2572 { 2573 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 2574 value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_512 2575 }, 2576 { 2577 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 2578 value: 2579 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | 2580 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT 2581 }, 2582 { 2583 tag: huks.HuksTag.HUKS_TAG_PADDING, 2584 value: huks.HuksKeyPadding.HUKS_PADDING_OAEP 2585 }, 2586 { 2587 tag: huks.HuksTag.HUKS_TAG_DIGEST, 2588 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 2589 } 2590]; 2591let options: huks.HuksOptions = { 2592 properties: properties 2593}; 2594huks.generateKey(keyAlias, options, (err, data) => { 2595}); 2596``` 2597 2598## huks.generateKey<sup>(deprecated)</sup> 2599 2600generateKey(keyAlias: string, options: HuksOptions) : Promise\<HuksResult> 2601 2602生成密钥,使用Promise方式异步返回结果。 2603 2604> **说明:** 2605> 2606> 从API version 9开始废弃,建议使用[huks.generateKeyItem<sup>9+</sup>](#huksgeneratekeyitem9-1)替代。 2607 2608**系统能力**:SystemCapability.Security.Huks.Extension 2609 2610**参数:** 2611 2612| 参数名 | 类型 | 必填 | 说明 | 2613| -------- | --------------------------- | ---- | ------------------------ | 2614| keyAlias | string | 是 | 密钥别名。 | 2615| options | [HuksOptions](#huksoptions) | 是 | 用于存放生成key所需TAG。 | 2616 2617**返回值**: 2618 2619| 类型 | 说明 | 2620| ----------------------------------- | -------------------------------------------------- | 2621| Promise\<[HuksResult](#huksresultdeprecated)> | Promise对象。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 | 2622 2623**示例:** 2624 2625```ts 2626import huks from '@ohos.security.huks'; 2627/* 以生成ECC256密钥为例 */ 2628class HuksProperties { 2629 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 2630 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | 2631 huks.HuksKeyDigest = huks.HuksKeyAlg.HUKS_ALG_ECC 2632} 2633 2634let keyAlias = 'keyAlias'; 2635let properties: HuksProperties[] = [ 2636 { 2637 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 2638 value: huks.HuksKeyAlg.HUKS_ALG_ECC 2639 }, 2640 { 2641 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 2642 value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_256 2643 }, 2644 { 2645 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 2646 value: 2647 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_SIGN | 2648 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY 2649 }, 2650 { 2651 tag: huks.HuksTag.HUKS_TAG_DIGEST, 2652 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 2653 } 2654]; 2655let options: huks.HuksOptions = { 2656 properties: properties 2657}; 2658let result = huks.generateKey(keyAlias, options); 2659``` 2660 2661## huks.deleteKey<sup>(deprecated)</sup> 2662 2663deleteKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void 2664 2665删除密钥,使用Callback回调异步返回结果。 2666 2667> **说明:** 2668> 2669> 从API version 9开始废弃,建议使用[huks.deleteKeyItem<sup>9+</sup>](#huksdeletekeyitem9)替代。 2670 2671**系统能力**:SystemCapability.Security.Huks.Extension 2672 2673**参数:** 2674 2675| 参数名 | 类型 | 必填 | 说明 | 2676| -------- | ----------------------------------------- | ---- | -------------------------------------------------- | 2677| keyAlias | string | 是 | 密钥别名,应为生成key时传入的别名。 | 2678| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 2679| callback | AsyncCallback\<[HuksResult](#huksresultdeprecated)> | 是 | 回调函数。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 | 2680 2681**示例:** 2682 2683```ts 2684import huks from '@ohos.security.huks'; 2685/* 此处options选择emptyOptions传空 */ 2686let keyAlias = 'keyAlias'; 2687let emptyOptions: huks.HuksOptions = { 2688 properties: [] 2689}; 2690huks.deleteKey(keyAlias, emptyOptions, (err, data) => { 2691}); 2692``` 2693 2694## huks.deleteKey<sup>(deprecated)</sup> 2695 2696deleteKey(keyAlias: string, options: HuksOptions) : Promise\<HuksResult> 2697 2698删除密钥,使用Promise方式异步返回结果。 2699 2700> **说明:** 2701> 2702> 从API version 9开始废弃,建议使用[huks.deleteKeyItem<sup>9+</sup>](#huksdeletekeyitem9-1)替代。 2703 2704**系统能力**:SystemCapability.Security.Huks.Extension 2705 2706**参数:** 2707 2708| 参数名 | 类型 | 必填 | 说明 | 2709| -------- | ----------- | ---- | ----------------------------------------------------- | 2710| keyAlias | string | 是 | 密钥别名,应为生成key时传入的别名。 | 2711| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 2712 2713**返回值:** 2714 2715| 类型 | 说明 | 2716| ----------------------------------- | -------------------------------------------------- | 2717| Promise\<[HuksResult](#huksresultdeprecated)> | Promise对象。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 | 2718 2719**示例:** 2720 2721```ts 2722import huks from '@ohos.security.huks'; 2723/* 此处options选择emptyOptions传空 */ 2724let keyAlias = 'keyAlias'; 2725let emptyOptions: huks.HuksOptions = { 2726 properties: [] 2727}; 2728let result = huks.deleteKey(keyAlias, emptyOptions); 2729``` 2730 2731## huks.importKey<sup>(deprecated)</sup> 2732 2733importKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void 2734 2735导入明文密钥,使用Callback方式回调异步返回结果 。 2736 2737> **说明:** 2738> 2739> 从API version 9开始废弃,建议使用[huks.importKeyItem<sup>9+</sup>](#huksimportkeyitem9)替代。 2740 2741**系统能力**:SystemCapability.Security.Huks.Extension 2742 2743**参数:** 2744 2745| 参数名 | 类型 | 必填 | 说明 | 2746| -------- | ------------------------ | ---- | ------------------------------------------------- | 2747| keyAlias | string | 是 | 密钥别名。 | 2748| options | [HuksOptions](#huksoptions) | 是 | 用于导入时所需TAG和需要导入的密钥。 | 2749| callback | AsyncCallback\<[HuksResult](#huksresultdeprecated)> | 是 | 回调函数。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 | 2750 2751**示例:** 2752 2753```ts 2754import huks from '@ohos.security.huks'; 2755/* 以导入AES256密钥为例 */ 2756class HuksProperties { 2757 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 2758 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | 2759 huks.HuksKeyPadding | huks.HuksCipherMode = huks.HuksKeyAlg.HUKS_ALG_ECC 2760} 2761let plainTextSize32 = makeRandomArr(32); 2762function makeRandomArr(size: number) { 2763 let arr = new Uint8Array(size); 2764 for (let i = 0; i < size; i++) { 2765 arr[i] = Math.floor(Math.random() * 10); 2766 } 2767 return arr; 2768}; 2769let keyAlias = 'keyAlias'; 2770let properties: HuksProperties[] = [ 2771 { 2772 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 2773 value: huks.HuksKeyAlg.HUKS_ALG_AES 2774 }, 2775 { 2776 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 2777 value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256 2778 }, 2779 { 2780 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 2781 value: 2782 huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT 2783 }, 2784 { 2785 tag: huks.HuksTag.HUKS_TAG_PADDING, 2786 value: huks.HuksKeyPadding.HUKS_PADDING_PKCS7 2787 }, 2788 { 2789 tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, 2790 value: huks.HuksCipherMode.HUKS_MODE_ECB 2791 } 2792]; 2793let options: huks.HuksOptions = { 2794 properties: properties, 2795 inData: plainTextSize32 2796}; 2797huks.importKey(keyAlias, options, (err, data) => { 2798}); 2799``` 2800 2801## huks.importKey<sup>(deprecated)</sup> 2802 2803importKey(keyAlias: string, options: HuksOptions) : Promise\<HuksResult> 2804 2805导入明文密钥,使用Promise方式异步返回结果。 2806 2807> **说明:** 2808> 2809> 从API version 9开始废弃,建议使用[huks.importKeyItem<sup>9+</sup>](#huksimportkeyitem9-1)替代。 2810 2811**系统能力**:SystemCapability.Security.Huks.Extension 2812 2813**参数:** 2814 2815| 参数名 | 类型 | 必填 | 说明 | 2816| -------- | ----------- | ---- | ------------------------------------ | 2817| keyAlias | string | 是 | 密钥别名。 | 2818| options | [HuksOptions](#huksoptions) | 是 | 用于导入时所需TAG和需要导入的密钥。 | 2819 2820**返回值:** 2821 2822| 类型 | 说明 | 2823| ----------------------------------- | -------------------------------------------------- | 2824| Promise\<[HuksResult](#huksresultdeprecated)> | Promise对象。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 | 2825 2826**示例:** 2827 2828```ts 2829import huks from '@ohos.security.huks'; 2830/* 以导入AES128为例 */ 2831class HuksProperties { 2832 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 2833 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | 2834 huks.HuksKeyPadding | huks.HuksCipherMode = huks.HuksKeyAlg.HUKS_ALG_ECC 2835} 2836let plainTextSize32 = makeRandomArr(32); 2837function makeRandomArr(size: number) { 2838 let arr = new Uint8Array(size); 2839 for (let i = 0; i < size; i++) { 2840 arr[i] = Math.floor(Math.random() * 10); 2841 } 2842 return arr; 2843}; 2844/*第一步:生成密钥*/ 2845let keyAlias = 'keyAlias'; 2846let properties: HuksProperties[] = [ 2847 { 2848 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 2849 value: huks.HuksKeyAlg.HUKS_ALG_AES 2850 }, 2851 { 2852 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 2853 value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128 2854 }, 2855 { 2856 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 2857 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT 2858 }, 2859 { 2860 tag: huks.HuksTag.HUKS_TAG_PADDING, 2861 value: huks.HuksKeyPadding.HUKS_PADDING_PKCS7 2862 }, 2863 { 2864 tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, 2865 value: huks.HuksCipherMode.HUKS_MODE_ECB 2866 } 2867]; 2868let huksoptions: huks.HuksOptions = { 2869 properties: properties, 2870 inData: plainTextSize32 2871}; 2872let result = huks.importKey(keyAlias, huksoptions); 2873``` 2874 2875## huks.exportKey<sup>(deprecated)</sup> 2876 2877exportKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void 2878 2879导出密钥,使用Callback方式回调异步返回的结果。 2880 2881> **说明:** 2882> 2883> 从API version 9开始废弃,建议使用[huks.exportKeyItem<sup>9+</sup>](#huksexportkeyitem9)替代。 2884 2885**系统能力**:SystemCapability.Security.Huks.Extension 2886 2887**参数:** 2888 2889| 参数名 | 类型 | 必填 | 说明 | 2890| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2891| keyAlias | string | 是 | 密钥别名,应与所用密钥生成时使用的别名相同。 | 2892| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 2893| callback | AsyncCallback\<[HuksResult](#huksresultdeprecated)> | 是 | 回调函数。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。outData:返回从密钥中导出的公钥。 | 2894 2895**示例:** 2896 2897```ts 2898import huks from '@ohos.security.huks'; 2899/* 此处options选择emptyOptions来传空 */ 2900let keyAlias = 'keyAlias'; 2901let emptyOptions: huks.HuksOptions = { 2902 properties: [] 2903}; 2904huks.exportKey(keyAlias, emptyOptions, (err, data) => { 2905}); 2906``` 2907 2908## huks.exportKey<sup>(deprecated)</sup> 2909 2910exportKey(keyAlias: string, options: HuksOptions) : Promise\<HuksResult> 2911 2912导出密钥,使用Promise方式回调异步返回的结果。 2913 2914> **说明:** 2915> 2916> 从API version 9开始废弃,建议使用[huks.exportKeyItem<sup>9+</sup>](#huksexportkeyitem9-1)替代。 2917 2918**系统能力**:SystemCapability.Security.Huks.Extension 2919 2920**参数:** 2921 2922| 参数名 | 类型 | 必填 | 说明 | 2923| -------- | ----------- | ---- | ------------------------------------------------------------ | 2924| keyAlias | string | 是 | 密钥别名,应与所用密钥生成时使用的别名相同。 | 2925| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 2926 2927**返回值:** 2928 2929| 类型 | 说明 | 2930| ----------------------------------- | ------------------------------------------------------------ | 2931| Promise\<[HuksResult](#huksresultdeprecated)> | Promise对象。返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。outData:返回从密钥中导出的公钥。 | 2932 2933**示例:** 2934 2935```ts 2936import huks from '@ohos.security.huks'; 2937/* 此处options选择emptyOptions来传空 */ 2938let keyAlias = 'keyAlias'; 2939let emptyOptions: huks.HuksOptions = { 2940 properties: [] 2941}; 2942let result = huks.exportKey(keyAlias, emptyOptions); 2943``` 2944 2945## huks.getKeyProperties<sup>(deprecated)</sup> 2946 2947getKeyProperties(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void 2948 2949获取密钥属性,使用Callback回调异步返回结果。 2950 2951> **说明:** 2952> 2953> 从API version 9开始废弃,建议使用[huks.getKeyItemProperties<sup>9+</sup>](#huksgetkeyitemproperties9)替代。 2954 2955**系统能力**:SystemCapability.Security.Huks.Extension 2956 2957**参数:** 2958 2959| 参数名 | 类型 | 必填 | 说明 | 2960| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2961| keyAlias | string | 是 | 密钥别名,应与所用密钥生成时使用的别名相同。 | 2962| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 2963| callback | AsyncCallback\<[HuksResult](#huksresultdeprecated)> | 是 | 回调函数。errorCode:返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。 | 2964 2965**示例:** 2966 2967```ts 2968import huks from '@ohos.security.huks'; 2969/* 此处options选择emptyOptions来传空 */ 2970let keyAlias = 'keyAlias'; 2971let emptyOptions: huks.HuksOptions = { 2972 properties: [] 2973}; 2974huks.getKeyProperties(keyAlias, emptyOptions, (err, data) => { 2975}); 2976``` 2977 2978## huks.getKeyProperties<sup>(deprecated)</sup> 2979 2980getKeyProperties(keyAlias: string, options: HuksOptions) : Promise\<HuksResult> 2981 2982获取密钥属性,使用Promise回调异步返回结果。 2983 2984> **说明:** 2985> 2986> 从API version 9开始废弃,建议使用[huks.getKeyItemProperties<sup>9+</sup>](#huksgetkeyitemproperties9-1)替代。 2987 2988**系统能力**:SystemCapability.Security.Huks.Extension 2989 2990**参数:** 2991 2992| 参数名 | 类型 | 必填 | 说明 | 2993| -------- | ----------- | ---- | ------------------------------------------------------------ | 2994| keyAlias | string | 是 | 密钥别名,应与所用密钥生成时使用的别名相同。 | 2995| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 2996 2997**返回值:** 2998 2999| 类型 | 说明 | 3000| ------------------ | ------------------------------------------------------------ | 3001| Promise\<[HuksResult](#huksoptions)> | Promise对象。errorCode:返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。properties:返回值为生成密钥时所需参数。 | 3002 3003**示例:** 3004 3005```ts 3006import huks from '@ohos.security.huks'; 3007/* 此处options选择emptyOptions来传空 */ 3008let keyAlias = 'keyAlias'; 3009let emptyOptions: huks.HuksOptions = { 3010 properties: [] 3011}; 3012let result = huks.getKeyProperties(keyAlias, emptyOptions); 3013``` 3014 3015## huks.isKeyExist<sup>(deprecated)</sup> 3016 3017isKeyExist(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<boolean>) : void 3018 3019判断密钥是否存在,使用Callback回调异步返回结果 。 3020 3021> **说明:** 3022> 3023> 从API version 9开始废弃,建议使用[huks.isKeyItemExist<sup>9+</sup>](#huksiskeyitemexist9)替代。 3024 3025**系统能力**:SystemCapability.Security.Huks.Extension 3026 3027**参数:** 3028 3029| 参数名 | 类型 | 必填 | 说明 | 3030| -------- | ---------------------- | ---- | ------------------------------------- | 3031| keyAlias | string | 是 | 所需查找的密钥的别名。 | 3032| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 3033| callback | AsyncCallback\<boolean> | 是 | 回调函数。false代表密钥不存在,true代表密钥存在。 | 3034 3035**示例:** 3036 3037```ts 3038import huks from '@ohos.security.huks'; 3039/* 此处options选择emptyOptions来传空 */ 3040let keyAlias = 'keyAlias'; 3041let emptyOptions: huks.HuksOptions = { 3042 properties: [] 3043}; 3044huks.isKeyExist(keyAlias, emptyOptions, (err, data) => { 3045}); 3046``` 3047 3048## huks.isKeyExist<sup>(deprecated)</sup> 3049 3050isKeyExist(keyAlias: string, options: HuksOptions) : Promise\<boolean> 3051 3052判断密钥是否存在,使用Promise回调异步返回结果 。 3053 3054> **说明:** 3055> 3056> 从API version 9开始废弃,建议使用[huks.isKeyItemExist<sup>9+</sup>](#huksiskeyitemexist9-1)替代。 3057 3058**系统能力**:SystemCapability.Security.Huks.Extension 3059 3060**参数:** 3061 3062| 参数名 | 类型 | 必填 | 说明 | 3063| -------- | ----------- | ---- | -------------------------------- | 3064| keyAlias | string | 是 | 所需查找的密钥的别名。 | 3065| options | [HuksOptions](#huksoptions) | 是 | 空对象(此处传空即可)。 | 3066 3067**返回值:** 3068 3069| 类型 | 说明 | 3070| ----------------- | --------------------------------------- | 3071| Promise\<boolean> | Promise对象。false代表密钥不存在,true代表密钥存在。 | 3072 3073**示例:** 3074 3075```ts 3076import huks from '@ohos.security.huks'; 3077/* 此处options选择emptyOptions来传空 */ 3078let keyAlias = 'keyAlias'; 3079let emptyOptions: huks.HuksOptions = { 3080 properties: [] 3081}; 3082let result = huks.isKeyExist(keyAlias, emptyOptions); 3083``` 3084 3085## huks.init<sup>(deprecated)</sup> 3086 3087init(keyAlias: string, options: HuksOptions, callback: AsyncCallback\<HuksHandle>) : void 3088 3089init操作密钥接口,使用Callback回调异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。 3090 3091> **说明:** 3092> 3093> 从API version 9开始废弃,建议使用[huks.initSession<sup>9+</sup>](#huksinitsession9-1)替代。 3094 3095**系统能力**:SystemCapability.Security.Huks.Extension 3096 3097**参数:** 3098 3099| 参数名 | 类型 | 必填 | 说明 | 3100| -------- | ---------------------- | ---- | ------------------------------------- | 3101| keyAlias | string | 是 | Init操作密钥的别名。 | 3102| options | [HuksOptions](#huksoptions) | 是 | Init操作的参数集合。 | 3103| callback | AsyncCallback\<[HuksHandle](#hukshandledeprecated)> | 是 | 回调函数。将Init操作操作返回的handle添加到密钥管理系统的回调。 | 3104 3105## huks.init<sup>(deprecated)</sup> 3106 3107init(keyAlias: string, options: HuksOptions) : Promise\<HuksHandle> 3108 3109init操作密钥接口,使用Promise方式异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。 3110 3111> **说明:** 3112> 3113> 从API version 9开始废弃,建议使用[huks.initSession<sup>9+</sup>](#huksinitsession9-1)替代。 3114 3115**系统能力**:SystemCapability.Security.Huks.Extension 3116 3117**参数:** 3118 3119| 参数名 | 类型 | 必填 | 说明 | 3120| -------- | ---------------------- | ---- | ------------------------------------- | 3121| keyAlias | string | 是 | Init操作密钥的别名。 | 3122| options | [HuksOptions](#huksoptions) | 是 | Init参数集合。 | 3123 3124**返回值**: 3125 3126| 类型 | 说明 | 3127| ----------------------------------- | -------------------------------------------------- | 3128| Promise\<[HuksHandle](#hukshandledeprecated)> | Promise对象。将Init操作返回的handle添加到密钥管理系统的回调。 | 3129 3130## huks.update<sup>(deprecated)</sup> 3131 3132update(handle: number, token?: Uint8Array, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void 3133 3134update操作密钥接口,使用Callback回调异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。 3135 3136> **说明:** 3137> 3138> 从API version 9开始废弃,建议使用[huks.updateSession<sup>9+</sup>](#huksupdatesession9-1)替代。 3139 3140**系统能力**: SystemCapability.Security.Huks.Extension 3141 3142**参数:** 3143 3144| 参数名 | 类型 | 必填 | 说明 | 3145| -------- | ----------------------------------------- | ---- | -------------------------------------------- | 3146| handle | number | 是 | Update操作的handle。 | 3147| token | Uint8Array | 否 | Update操作的token。 | 3148| options | [HuksOptions](#huksoptions) | 是 | Update操作的参数集合。 | 3149| callback | AsyncCallback\<[HuksResult](#huksresultdeprecated)> | 是 | 回调函数。将Update操作的结果添加到密钥管理系统的回调。 | 3150 3151## huks.update<sup>(deprecated)</sup> 3152 3153update(handle: number, token?: Uint8Array, options: HuksOptions) : Promise\<HuksResult>; 3154 3155update操作密钥接口,使用Promise方式异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。 3156 3157> **说明:** 3158> 3159> 从API version 9开始废弃,建议使用[huks.updateSession<sup>9+</sup>](#huksupdatesession9-2)替代。 3160 3161**系统能力**: SystemCapability.Security.Huks.Extension 3162 3163**参数:** 3164 3165| 参数名 | 类型 | 必填 | 说明 | 3166| ------- | ----------------------------------- | ---- | -------------------------------------------- | 3167| handle | number | 是 | Update操作的handle。 | 3168| token | Uint8Array | 否 | Update操作的token。 | 3169| options | [HuksOptions](#huksoptions) | 是 | Update操作的参数集合。 | 3170 3171**返回值**: 3172 3173| 类型 | 说明 | 3174| ----------------------------------- | -------------------------------------------------- | 3175| Promise\<[HuksResult](#huksresultdeprecated)> | Promise对象。将Update操作的结果添加到密钥管理系统的回调。 | 3176 3177## huks.finish<sup>(deprecated)</sup> 3178 3179finish(handle: number, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void 3180 3181finish操作密钥接口,使用Callback回调异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。 3182 3183> **说明:** 3184> 3185> 从API version 9开始废弃,建议使用[huks.finishSession<sup>9+</sup>](#huksfinishsession9)替代。 3186 3187**系统能力**:SystemCapability.Security.Huks.Extension 3188 3189**参数:** 3190 3191| 参数名 | 类型 | 必填 | 说明 | 3192| -------- | ---------------------- | ---- | ------------------------------------- | 3193| handle | number | 是 | Finish操作的handle。 | 3194| options | [HuksOptions](#huksoptions) | 是 | Finish的参数集合。 | 3195| callback | AsyncCallback\<[HuksResult](#huksresultdeprecated)> | 是 | 回调函数。将Finish操作的结果添加到密钥管理系统的回调。 | 3196 3197## huks.finish<sup>(deprecated)</sup> 3198 3199finish(handle: number, options: HuksOptions) : Promise\<HuksResult> 3200 3201finish操作密钥接口,使用Promise方式异步返回结果。huks.init, huks.update, huks.finish为三段式接口,需要一起使用。 3202 3203> **说明:** 3204> 3205> 从API version 9开始废弃,建议使用[huks.finishSession<sup>9+</sup>](#huksfinishsession9-1)替代。 3206 3207**系统能力**:SystemCapability.Security.Huks.Extension 3208 3209**参数:** 3210 3211| 参数名 | 类型 | 必填 | 说明 | 3212| -------- | ---------------------- | ---- | ------------------------------------- | 3213| handle | number | 是 | Finish操作的handle。 | 3214| options | [HuksOptions](#huksoptions) | 是 | Finish操作的参数集合。 | 3215 3216**返回值**: 3217 3218| 类型 | 说明 | 3219| ----------------------------------- | -------------------------------------------------- | 3220| Promise\<[HuksResult](#huksresultdeprecated)> | Promise对象,用于获取异步返回结果。 | 3221 3222## huks.abort<sup>(deprecated)</sup> 3223 3224abort(handle: number, options: HuksOptions, callback: AsyncCallback\<HuksResult>) : void 3225 3226abort操作密钥接口,使用Callback回调异步返回结果。 3227 3228> **说明:** 3229> 3230> 从API version 9开始废弃,建议使用[huks.abortSession<sup>9+</sup>](#huksabortsession9)替代。 3231 3232**系统能力**:SystemCapability.Security.Huks.Extension 3233 3234**参数:** 3235 3236| 参数名 | 类型 | 必填 | 说明 | 3237| -------- | ---------------------- | ---- | ------------------------------------- | 3238| handle | number | 是 | Abort操作的handle。 | 3239| options | [HuksOptions](#huksoptions) | 是 | Abort操作的参数集合。 | 3240| callback | AsyncCallback\<[HuksResult](#huksresultdeprecated)> | 是 | 回调函数。将Abort操作的结果添加到密钥管理系统的回调。 | 3241 3242**示例:** 3243 3244```ts 3245import huks from '@ohos.security.huks'; 3246import { BusinessError } from '@ohos.base'; 3247/* huks.init, huks.update, huks.finish为三段式接口,需要一起使用,当huks.init和huks.update 3248 * 以及huks.finish操作中的任一阶段发生错误时,都需要调用huks.abort来终止密钥的使用。 3249 * 3250 * 以下以RSA1024密钥的callback操作使用为例 3251 */ 3252class HuksProperties { 3253 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 3254 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | 3255 huks.HuksKeyDigest | huks.HuksKeyPadding = huks.HuksKeyAlg.HUKS_ALG_ECC 3256} 3257let keyalias = "HuksDemoRSA"; 3258let properties: HuksProperties[] = []; 3259let options: huks.HuksOptions = { 3260 properties: properties, 3261 inData: new Uint8Array(0) 3262}; 3263let handle: number = 0; 3264let resultMessage = ""; 3265async function generateKey() { 3266 properties[0] = { 3267 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 3268 value: huks.HuksKeyAlg.HUKS_ALG_RSA 3269 }; 3270 properties[1] = { 3271 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 3272 value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024 3273 }; 3274 properties[2] = { 3275 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 3276 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT 3277 }; 3278 properties[3] = { 3279 tag: huks.HuksTag.HUKS_TAG_PADDING, 3280 value: huks.HuksKeyPadding.HUKS_PADDING_OAEP 3281 }; 3282 properties[4] = { 3283 tag: huks.HuksTag.HUKS_TAG_DIGEST, 3284 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 3285 }; 3286 huks.generateKey(keyalias, options); 3287} 3288function stringToUint8Array(str: string) { 3289 let arr: number[] = []; 3290 for (let i = 0, j = str.length; i < j; ++i) { 3291 arr.push(str.charCodeAt(i)); 3292 } 3293 let tmpUint8Array = new Uint8Array(arr); 3294 return tmpUint8Array; 3295} 3296async function huksInit() { 3297 await huks.init(keyalias, options).then((data) => { 3298 console.log(`test init data: ${JSON.stringify(data)}`); 3299 handle = data.handle; 3300 }).catch((err: BusinessError) => { 3301 console.log("test init err information: " + JSON.stringify(err)) 3302 }) 3303} 3304async function huksUpdate() { 3305 options.inData = stringToUint8Array("huksHmacTest"); 3306 await huks.update(handle, options.inData, options).then((data) => { 3307 if (data.errorCode === 0) { 3308 resultMessage += "update success!"; 3309 } else { 3310 resultMessage += "update fail!"; 3311 } 3312 }); 3313 console.log(resultMessage); 3314} 3315function huksFinish() { 3316 options.inData = stringToUint8Array("HuksDemoHMAC"); 3317 huks.finish(handle, options).then((data) => { 3318 if (data.errorCode === 0) { 3319 resultMessage = "finish success!"; 3320 } else { 3321 resultMessage = "finish fail errorCode: " + data.errorCode; 3322 } 3323 }).catch((err: BusinessError) => { 3324 resultMessage = "finish fail, catch errorMessage:" + JSON.stringify(err) 3325 }); 3326 console.log(resultMessage); 3327} 3328async function huksAbort() { 3329 new Promise<huks.HuksResult>((resolve, reject) => { 3330 huks.abort(handle, options, (err, data) => { 3331 console.log(`Huks_Demo hmac huksAbort1 data ${JSON.stringify(data)}`); 3332 console.log(`Huks_Demo hmac huksAbort1 err ${JSON.stringify(err)}`); 3333 }); 3334 }); 3335} 3336``` 3337 3338## huks.abort<sup>(deprecated)</sup> 3339 3340abort(handle: number, options: HuksOptions) : Promise\<HuksResult>; 3341 3342abort操作密钥接口,使用Promise方式异步返回结果。 3343 3344> **说明:** 3345> 3346> 从API version 9开始废弃,建议使用[huks.abortSession<sup>9+</sup>](#huksabortsession9-1)替代。 3347 3348**系统能力**:SystemCapability.Security.Huks.Extension 3349 3350**参数:** 3351 3352| 参数名 | 类型 | 必填 | 说明 | 3353| -------- | ---------------------- | ---- | ------------------------------------- | 3354| handle | number | 是 | Abort操作的handle。 | 3355| options | [HuksOptions](#huksoptions) | 是 | Abort操作的参数集合。 | 3356 3357**返回值**: 3358 3359| 类型 | 说明 | 3360| ----------------------------------- | -------------------------------------------------- | 3361| Promise\<[HuksResult](#huksresultdeprecated)> | Promise对象。将Abort操作的结果添加到密钥管理系统的回调。 | 3362 3363**示例:** 3364 3365```ts 3366import huks from '@ohos.security.huks'; 3367import { BusinessError } from '@ohos.base'; 3368/* huks.init, huks.update, huks.finish为三段式接口,需要一起使用,当huks.init和huks.update 3369 * 以及huks.finish操作中的任一阶段发生错误时,都需要调用huks.abort来终止密钥的使用。 3370 * 3371 * 以下以RSA1024密钥的promise操作使用为例 3372 */ 3373class HuksProperties { 3374 tag: huks.HuksTag = huks.HuksTag.HUKS_TAG_ALGORITHM 3375 value: huks.HuksKeyAlg | huks.HuksKeySize | huks.HuksKeyPurpose | 3376 huks.HuksKeyPadding | huks.HuksKeyDigest = huks.HuksKeyAlg.HUKS_ALG_ECC 3377} 3378let keyalias = "HuksDemoRSA"; 3379let properties: HuksProperties[] = []; 3380let options: huks.HuksOptions = { 3381 properties: properties, 3382 inData: new Uint8Array(0) 3383}; 3384let handle: number = 0; 3385let resultMessage = ""; 3386 3387function stringToUint8Array(str: string) { 3388 let arr: number[] = []; 3389 for (let i = 0, j = str.length; i < j; ++i) { 3390 arr.push(str.charCodeAt(i)); 3391 } 3392 let tmpUint8Array = new Uint8Array(arr); 3393 return tmpUint8Array; 3394} 3395 3396async function generateKey() { 3397 properties[0] = { 3398 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 3399 value: huks.HuksKeyAlg.HUKS_ALG_RSA 3400 }; 3401 properties[1] = { 3402 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 3403 value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024 3404 }; 3405 properties[2] = { 3406 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 3407 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT 3408 }; 3409 properties[3] = { 3410 tag: huks.HuksTag.HUKS_TAG_PADDING, 3411 value: huks.HuksKeyPadding.HUKS_PADDING_OAEP 3412 }; 3413 properties[4] = { 3414 tag: huks.HuksTag.HUKS_TAG_DIGEST, 3415 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 3416 }; 3417 huks.generateKey(keyalias, options, (err, data) => { 3418 }); 3419} 3420 3421async function huksInit() { 3422 return new Promise<huks.HuksHandle>((resolve, reject) => { 3423 huks.init(keyalias, options, async (err, data) => { 3424 if (data.errorCode === 0) { 3425 resultMessage = "init success!" 3426 handle = data.handle; 3427 } else { 3428 resultMessage = "init fail errorCode: " + data.errorCode 3429 } 3430 }); 3431 }); 3432} 3433 3434async function huksUpdate() { 3435 options.inData = stringToUint8Array("huksHmacTest"); 3436 new Promise<huks.HuksResult>((resolve, reject) => { 3437 huks.update(handle, options.inData, options, (err, data) => { 3438 if (data.errorCode === 0) { 3439 resultMessage += "update success!"; 3440 } else { 3441 resultMessage += "update fail!"; 3442 } 3443 }); 3444 }); 3445 console.log(resultMessage); 3446 3447} 3448 3449async function huksFinish() { 3450 options.inData = stringToUint8Array("0"); 3451 new Promise<huks.HuksResult>((resolve, reject) => { 3452 huks.finish(handle, options, (err, data) => { 3453 if (data.errorCode === 0) { 3454 resultMessage = "finish success!"; 3455 } else { 3456 resultMessage = "finish fail errorCode: " + data.errorCode; 3457 } 3458 }); 3459 }); 3460} 3461 3462function huksAbort() { 3463 huks.abort(handle, options).then((data) => { 3464 if (data.errorCode === 0) { 3465 resultMessage = "abort success!"; 3466 } else { 3467 resultMessage = "abort fail errorCode: " + data.errorCode; 3468 } 3469 }).catch((err: BusinessError) => { 3470 resultMessage = "abort fail, catch errorMessage:" + JSON.stringify(err) 3471 }); 3472 console.log(resultMessage); 3473} 3474``` 3475 3476## HuksHandle<sup>(deprecated)</sup> 3477 3478huks Handle结构体。 3479 3480**系统能力**:SystemCapability.Security.Huks.Extension 3481> **说明:** 3482> 3483> 从API version 9开始废弃,建议使用[HuksSessionHandle<sup>9+</sup>](#hukssessionhandle9)替代。 3484 3485| 名称 | 类型 | 必填 | 说明 | 3486| ---------- | ---------------- | ---- | -------- | 3487| errorCode | number | 是 | 表示错误码。 | 3488| handle | number | 是 | 表示handle值。 | 3489| token | Uint8Array | 否 | 表示[init](#huksinitdeprecated)操作之后获取到的challenge信息。 | 3490 3491## HuksResult<sup>(deprecated)</sup> 3492 3493调用接口返回的result。 3494 3495**系统能力**:SystemCapability.Security.Huks.Extension 3496 3497> **说明:** 3498> 3499> - 从API version 9开始废弃,建议使用[HuksReturnResult<sup>9+</sup>](#huksreturnresult9)替代。 3500> - errorCode的具体信息,请参考[错误码文档](../errorcodes/errorcode-huks.md)。 3501 3502| 名称 | 类型 | 必填 | 说明 | 3503| ---------- | ------------------------------- | ---- | ---------------- | 3504| errorCode | number | 是 | 表示错误码。 | 3505| outData | Uint8Array | 否 | 表示输出数据。 | 3506| properties | Array\<[HuksParam](#huksparam)> | 否 | 表示属性信息。 | 3507| certChains | Array\<string> | 否 | 表示证书链数据。 | 3508 3509## HuksErrorCode<sup>(deprecated)</sup> 3510 3511表示错误码的枚举。 3512 3513**系统能力**:SystemCapability.Security.Huks.Extension 3514> **说明:** 3515> 3516> 从API version 9开始废弃,建议使用[HuksExceptionErrCode<sup>9+</sup>](#huksexceptionerrcode9)替代。 3517 3518| 名称 | 值 | 说明 | 3519| -------------------------- | ----- | ---- | 3520| HUKS_SUCCESS | 0 |表示成功。| 3521| HUKS_FAILURE | -1 |表示失败。| 3522| HUKS_ERROR_BAD_STATE | -2 |表示错误的状态。| 3523| HUKS_ERROR_INVALID_ARGUMENT | -3 |表示无效的数据。| 3524| HUKS_ERROR_NOT_SUPPORTED | -4 |表示不支持。| 3525| HUKS_ERROR_NO_PERMISSION | -5 |表示没有许可。| 3526| HUKS_ERROR_INSUFFICIENT_DATA | -6 |表示数据不足。| 3527| HUKS_ERROR_BUFFER_TOO_SMALL | -7 |表示缓冲区太小。| 3528| HUKS_ERROR_INSUFFICIENT_MEMORY | -8 |表示内存不足。| 3529| HUKS_ERROR_COMMUNICATION_FAILURE | -9 |表示通讯失败。| 3530| HUKS_ERROR_STORAGE_FAILURE | -10 |表示存储故障。| 3531| HUKS_ERROR_HARDWARE_FAILURE | -11 |表示硬件故障。| 3532| HUKS_ERROR_ALREADY_EXISTS | -12 |表示已经存在。| 3533| HUKS_ERROR_NOT_EXIST | -13 |表示不存在。| 3534| HUKS_ERROR_NULL_POINTER | -14 |表示空指针。| 3535| HUKS_ERROR_FILE_SIZE_FAIL | -15 |表示文件大小失败。| 3536| HUKS_ERROR_READ_FILE_FAIL | -16 |表示读取文件失败。| 3537| HUKS_ERROR_INVALID_PUBLIC_KEY | -17 |表示无效的公钥。| 3538| HUKS_ERROR_INVALID_PRIVATE_KEY | -18 |表示无效的私钥。| 3539| HUKS_ERROR_INVALID_KEY_INFO | -19 |表示无效的密钥信息。| 3540| HUKS_ERROR_HASH_NOT_EQUAL | -20 |表示哈希不相等。| 3541| HUKS_ERROR_MALLOC_FAIL | -21 |表示MALLOC 失败。| 3542| HUKS_ERROR_WRITE_FILE_FAIL | -22 |表示写文件失败。| 3543| HUKS_ERROR_REMOVE_FILE_FAIL | -23 |表示删除文件失败。| 3544| HUKS_ERROR_OPEN_FILE_FAIL | -24 |表示打开文件失败。| 3545| HUKS_ERROR_CLOSE_FILE_FAIL | -25 |表示关闭文件失败。| 3546| HUKS_ERROR_MAKE_DIR_FAIL | -26 |表示创建目录失败。| 3547| HUKS_ERROR_INVALID_KEY_FILE | -27 |表示无效的密钥文件。| 3548| HUKS_ERROR_IPC_MSG_FAIL | -28 |表示IPC 信息失败。| 3549| HUKS_ERROR_REQUEST_OVERFLOWS | -29 |表示请求溢出。| 3550| HUKS_ERROR_PARAM_NOT_EXIST | -30 |表示参数不存在。| 3551| HUKS_ERROR_CRYPTO_ENGINE_ERROR | -31 |表示CRYPTO ENGINE错误。| 3552| HUKS_ERROR_COMMUNICATION_TIMEOUT | -32 |表示通讯超时。| 3553| HUKS_ERROR_IPC_INIT_FAIL | -33 |表示IPC 初始化失败。| 3554| HUKS_ERROR_IPC_DLOPEN_FAIL | -34 |表示IPC DLOPEN 失败。| 3555| HUKS_ERROR_EFUSE_READ_FAIL | -35 |表示EFUSE 读取失败。| 3556| HUKS_ERROR_NEW_ROOT_KEY_MATERIAL_EXIST | -36 |表示存在新的根密钥材料。| 3557| HUKS_ERROR_UPDATE_ROOT_KEY_MATERIAL_FAIL | -37 |表示更新根密钥材料失败。| 3558| HUKS_ERROR_VERIFICATION_FAILED | -38 |表示验证证书链失败。| 3559| HUKS_ERROR_CHECK_GET_ALG_FAIL | -100 |表示检查获取 ALG 失败。| 3560| HUKS_ERROR_CHECK_GET_KEY_SIZE_FAIL | -101 |表示检查获取密钥大小失败。| 3561| HUKS_ERROR_CHECK_GET_PADDING_FAIL | -102 |表示检查获取填充失败。| 3562| HUKS_ERROR_CHECK_GET_PURPOSE_FAIL | -103 |表示检查获取目的失败。| 3563| HUKS_ERROR_CHECK_GET_DIGEST_FAIL | -104 |表示检查获取摘要失败。| 3564| HUKS_ERROR_CHECK_GET_MODE_FAIL | -105 |表示检查获取模式失败。| 3565| HUKS_ERROR_CHECK_GET_NONCE_FAIL | -106 |表示检查获取随机数失败。| 3566| HUKS_ERROR_CHECK_GET_AAD_FAIL | -107 |表示检查获取 AAD 失败。| 3567| HUKS_ERROR_CHECK_GET_IV_FAIL | -108 |表示检查 GET IV 失败。| 3568| HUKS_ERROR_CHECK_GET_AE_TAG_FAIL | -109 |表示检查获取 AE 标记失败。| 3569| HUKS_ERROR_CHECK_GET_SALT_FAIL | -110 |表示检查获取SALT失败。| 3570| HUKS_ERROR_CHECK_GET_ITERATION_FAIL | -111 |表示检查获取迭代失败。| 3571| HUKS_ERROR_INVALID_ALGORITHM | -112 |表示无效的算法。| 3572| HUKS_ERROR_INVALID_KEY_SIZE | -113 |表示无效的密钥大小。| 3573| HUKS_ERROR_INVALID_PADDING | -114 |表示无效的填充。| 3574| HUKS_ERROR_INVALID_PURPOSE | -115 |表示无效的目的。| 3575| HUKS_ERROR_INVALID_MODE | -116 |表示无效模式。| 3576| HUKS_ERROR_INVALID_DIGEST | -117 |表示无效的摘要。| 3577| HUKS_ERROR_INVALID_SIGNATURE_SIZE | -118 |表示签名大小无效。| 3578| HUKS_ERROR_INVALID_IV | -119 |表示无效的 IV。| 3579| HUKS_ERROR_INVALID_AAD | -120 |表示无效的 AAD。| 3580| HUKS_ERROR_INVALID_NONCE | -121 |表示无效的随机数。| 3581| HUKS_ERROR_INVALID_AE_TAG | -122 |表示无效的 AE 标签。| 3582| HUKS_ERROR_INVALID_SALT | -123 |表示无效SALT。| 3583| HUKS_ERROR_INVALID_ITERATION | -124 |表示无效的迭代。| 3584| HUKS_ERROR_INVALID_OPERATION | -125 |表示无效操作。| 3585| HUKS_ERROR_INTERNAL_ERROR | -999 |表示内部错误。| 3586| HUKS_ERROR_UNKNOWN_ERROR | -1000 |表示未知错误。|