1e41f4b71Sopenharmony_ci# @ohos.security.cryptoFramework (Crypto Framework) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **cryptoFramework** module shields underlying hardware and algorithm libraries and provides unified APIs for cryptographic operations. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Modules to Import 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Result 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci Enumerates the operation results. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci **System capability**: SystemCapability.Security.CryptoFramework 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci| Name | Value | Description | 22e41f4b71Sopenharmony_ci| ------------------------------------- | -------- | ---------------------------- | 23e41f4b71Sopenharmony_ci| INVALID_PARAMS | 401 | Invalid parameter.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 24e41f4b71Sopenharmony_ci| NOT_SUPPORT | 801 | Unsupported operation.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 25e41f4b71Sopenharmony_ci| ERR_OUT_OF_MEMORY | 17620001 | Memory error.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 26e41f4b71Sopenharmony_ci| ERR_RUNTIME_ERROR | 17620002 | Runtime error.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 27e41f4b71Sopenharmony_ci| ERR_CRYPTO_OPERATION | 17630001 | Cryptographic operation error.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## DataBlob 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciDefines a buffer array of the Binary Large Object (BLOB) type. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci **Atomic service API**: This API can be used in atomic services since API version 11. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci **System capability**: SystemCapability.Security.CryptoFramework 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 38e41f4b71Sopenharmony_ci| ---- | ---------- | ---- | ---- | ------ | 39e41f4b71Sopenharmony_ci| data | Uint8Array | Yes | Yes | Binary data array. | 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci> **NOTE** 42e41f4b71Sopenharmony_ci> 43e41f4b71Sopenharmony_ci> The Uint8Array typed array represents an array of 8-bit unsigned integers. 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci## ParamsSpec 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciEncapsulates the parameters used for encryption or decryption. You need to construct its child class object and pass it to [init()](#init-2) for symmetric encryption or decryption. 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ciIt applies to the symmetric cipher modes that require parameters such as the initialization vector (IV). If the IV is not required (for example, the ECB mode), pass in **null** to [init()](#init-2). 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 56e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 57e41f4b71Sopenharmony_ci| algName | string | Yes | Yes | Algorithm for symmetric encryption or decryption.<br/>Options:<br>- **IvParamsSpec**: applicable to the CBC, CTR, OFB, and CFB modes.<br>- **GcmParamsSpec**: applicable to the GCM mode.<br>- **CcmParamsSpec**: applicable to the CCM mode. | 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci> **NOTE** 60e41f4b71Sopenharmony_ci> 61e41f4b71Sopenharmony_ci> The **params** parameter in [init()](#init-2) is of the **ParamsSpec** type (parent class). However, a child class object (such as **IvParamsSpec**) needs to be passed in. When constructing the child class object, you need to set **algName** for its parent class **ParamsSpec** to specify the child class object to be passed to **init()**. 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci## IvParamsSpec 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ciDefines the child class of [ParamsSpec](#paramsspec). It is a parameter of [init()](#init-2) for symmetric encryption or decryption. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**IvParamsSpec** applies to the cipher modes such as CBC, CTR, OFB, and CFB, which use only the IV. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 74e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | ---- | ------------------------------------------------------------ | 75e41f4b71Sopenharmony_ci| iv | [DataBlob](#datablob) | Yes | Yes | IV for encryption or decryption.<br/>Options:<br>- AES CBC, CTR, OFB, or CFB mode: 16-byte IV<br>- 3DES CBC, OFB, or CFB mode: 8-byte IV<br>- SM4<sup>10+</sup> CBC, CTR, OFB, or CFB mode: 16-byte IV | 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci> **NOTE** 78e41f4b71Sopenharmony_ci> 79e41f4b71Sopenharmony_ci> Before passing **IvParamsSpec** to [init()](#init-2), specify **algName** for its parent class [ParamsSpec](#paramsspec). 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci## GcmParamsSpec 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ciDefines the child class of [ParamsSpec](#paramsspec). It is a parameter of [init()](#init-2) for symmetric encryption or decryption. 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci**GcmParamsSpec** applies to the GCM mode. 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 92e41f4b71Sopenharmony_ci| ------- | --------------------- | ---- | ---- | ------------------------------------------------------------ | 93e41f4b71Sopenharmony_ci| iv | [DataBlob](#datablob) | Yes | Yes | IV, which is of 1 to 16 bytes. A 12-byte IV is commonly used. | 94e41f4b71Sopenharmony_ci| aad | [DataBlob](#datablob) | Yes | Yes | Additional authentication data (AAD), which is of 0 to INT_MAX bytes. A 16-byte AAD is commonly used. | 95e41f4b71Sopenharmony_ci| authTag | [DataBlob](#datablob) | Yes | Yes | Authentication tag, which is of 16 bytes.<br>If the GCM mode is used for encryption, **authTag** in the parameter **GcmParamsSpec** of [init()](#init-2) or [initSync()](#initsync12) is the last 16 bytes of [DataBlob](#datablob) output by [doFinal()](#dofinal-2) or [doFinalSync()](#dofinalsync12). | 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci> **NOTE** 98e41f4b71Sopenharmony_ci> 99e41f4b71Sopenharmony_ci> - Before passing **GcmParamsSpec** to [init()](#init-2), specify **algName** for its parent class [ParamsSpec](#paramsspec). 100e41f4b71Sopenharmony_ci> - The IV to use is not length bound. However, the operation result depends on whether the underlying OpenSSL supports the IV. 101e41f4b71Sopenharmony_ci> - If **aad** is not required or the length of **aad** is **0**, you can set **aad** to an empty Uint8Array, that is, **aad: { data: new Uint8Array() }**. 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci## CcmParamsSpec 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ciDefines the child class of [ParamsSpec](#paramsspec). It is a parameter of [init()](#init-2) for symmetric encryption or decryption. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**CcmParamsSpec** applies to the CCM mode. 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 114e41f4b71Sopenharmony_ci| ------- | --------------------- | ---- | ---- | ------------------------------------------------------------ | 115e41f4b71Sopenharmony_ci| iv | [DataBlob](#datablob) | Yes | Yes | IV, which is of 7 bytes. | 116e41f4b71Sopenharmony_ci| aad | [DataBlob](#datablob) | Yes | Yes | AAD, which is of 8 bytes. | 117e41f4b71Sopenharmony_ci| authTag | [DataBlob](#datablob) | Yes | Yes | Authentication tag, which is of 12 bytes.<br>If the CCM mode is used for encryption, **authTag** in the parameter [CcmParamsSpec](#ccmparamsspec) of [init()](#init-2) or [initSync()](#initsync12) is the last 12 bytes of [DataBlob](#datablob) output by [doFinal()](#dofinal-2) or [doFinalSync()](#dofinalsync12). | 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci> **NOTE** 120e41f4b71Sopenharmony_ci> 121e41f4b71Sopenharmony_ci> Before passing **CcmParamsSpec** to [init()](#init-2), specify **algName** for its parent class [ParamsSpec](#paramsspec). 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci## CryptoMode 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ciEnumerates the cryptographic operations. 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci| Name | Value | Description | 132e41f4b71Sopenharmony_ci| ------------ | ---- | ------------------ | 133e41f4b71Sopenharmony_ci| ENCRYPT_MODE | 0 | Encryption. | 134e41f4b71Sopenharmony_ci| DECRYPT_MODE | 1 | Decryption. | 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci## AsyKeySpecItem<sup>10+</sup> 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ciEnumerates the asymmetric key parameters. 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci| Name | Value | Description | 145e41f4b71Sopenharmony_ci| ------------ | ---- | ---------------- | 146e41f4b71Sopenharmony_ci| DSA_P_BN | 101 | Prime modulus **p** in the DSA algorithm. | 147e41f4b71Sopenharmony_ci| DSA_Q_BN | 102 | Parameter **q**, prime factor of (p – 1) in the DSA algorithm. | 148e41f4b71Sopenharmony_ci| DSA_G_BN | 103 | Parameter **g** in the DSA algorithm. | 149e41f4b71Sopenharmony_ci| DSA_SK_BN | 104 | Private key **sk** in the DSA algorithm. | 150e41f4b71Sopenharmony_ci| DSA_PK_BN | 105 | Public key **pk** in the DSA algorithm. | 151e41f4b71Sopenharmony_ci| ECC_FP_P_BN | 201 | Prime number **p** in the **Fp** field of the elliptic curve in the ECC algorithm. | 152e41f4b71Sopenharmony_ci| ECC_A_BN | 202 | First coefficient **a** of the elliptic curve in the ECC algorithm. | 153e41f4b71Sopenharmony_ci| ECC_B_BN | 203 | Second coefficient **b** of the elliptic curve in the ECC algorithm. | 154e41f4b71Sopenharmony_ci| ECC_G_X_BN | 204 | X coordinate of the base point **g** in the ECC algorithm. | 155e41f4b71Sopenharmony_ci| ECC_G_Y_BN | 205 | Y coordinate of the base point **g** in the ECC algorithm. | 156e41f4b71Sopenharmony_ci| ECC_N_BN | 206 | Order **n** of the base point **g** in the ECC algorithm. | 157e41f4b71Sopenharmony_ci| ECC_H_NUM | 207 | Cofactor **h** in the ECC algorithm. | 158e41f4b71Sopenharmony_ci| ECC_SK_BN | 208 | Private key **sk** in the ECC algorithm. | 159e41f4b71Sopenharmony_ci| ECC_PK_X_BN | 209 | X coordinate of the public key **pk** (a point on the elliptic curve) in the ECC algorithm. | 160e41f4b71Sopenharmony_ci| ECC_PK_Y_BN | 210 | Y coordinate of the public key **pk** (a point on the elliptic curve) in the ECC algorithm. | 161e41f4b71Sopenharmony_ci| ECC_FIELD_TYPE_STR | 211 | Elliptic curve field type in the ECC algorithm. Currently, only the **Fp** field is supported. | 162e41f4b71Sopenharmony_ci| ECC_FIELD_SIZE_NUM | 212 | Size of the field in the ECC algorithm, in bits.<br>**NOTE**: The size of the **Fp** field is the length of the prime **p**, in bits. | 163e41f4b71Sopenharmony_ci| ECC_CURVE_NAME_STR | 213 | Standards for Efficient Cryptography Group (SECG) curve name in the ECC algorithm. | 164e41f4b71Sopenharmony_ci| RSA_N_BN | 301 | Modulus **n** in the RSA algorithm. | 165e41f4b71Sopenharmony_ci| RSA_SK_BN | 302 | Private key **sk** (private key exponent **d**) in the RSA algorithm. | 166e41f4b71Sopenharmony_ci| RSA_PK_BN | 303 | Public key **pk** (public key exponent **e**) in the RSA algorithm. | 167e41f4b71Sopenharmony_ci| DH_P_BN<sup>11+</sup> | 401 | Prime **p** in the DH algorithm. | 168e41f4b71Sopenharmony_ci| DH_G_BN<sup>11+</sup> | 402 | Parameter **g** in the DH algorithm. | 169e41f4b71Sopenharmony_ci| DH_L_NUM<sup>11+</sup> | 403 | Length of the private key in the DH algorithm, in bits. | 170e41f4b71Sopenharmony_ci| DH_SK_BN<sup>11+</sup> | 404 | Private key **sk** in the DH algorithm. | 171e41f4b71Sopenharmony_ci| DH_PK_BN<sup>11+</sup> | 405 | Public key **pk** in the DH algorithm. | 172e41f4b71Sopenharmony_ci| ED25519_SK_BN<sup>11+</sup> | 501 | Private key **sk** in the Ed25519 algorithm. | 173e41f4b71Sopenharmony_ci| ED25519_PK_BN<sup>11+</sup> | 502 | Public key **pk** in the Ed25519 algorithm. | 174e41f4b71Sopenharmony_ci| X25519_SK_BN<sup>11+</sup> | 601 | Private key **sk** in the X25519 algorithm. | 175e41f4b71Sopenharmony_ci| X25519_PK_BN<sup>11+</sup> | 602 | Public key **pk** in the X25519 algorithm. | 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci## AsyKeySpecType<sup>10+</sup> 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ciEnumerates the key parameter types. 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci| Name | Value | Description | 186e41f4b71Sopenharmony_ci| ------------ | ---- | ---------------- | 187e41f4b71Sopenharmony_ci| COMMON_PARAMS_SPEC | 0 | Common parameter of the public and private keys. You can use [generateKeyPair()](#generatekeypair-2) to randomly generate a key pair based on the parameters of this type. | 188e41f4b71Sopenharmony_ci| PRIVATE_KEY_SPEC | 1 | Parameter of the private key. You can use [generatePriKey()](#generateprikey) to generate a private key based on the parameters of this type. | 189e41f4b71Sopenharmony_ci| PUBLIC_KEY_SPEC | 2 | Parameter of the public key. You can use [generatePubKey()](#generatepubkey) to generate a public key based on the parameters of this type. | 190e41f4b71Sopenharmony_ci| KEY_PAIR_SPEC | 3 | Full parameters of the public and private keys. You can use [generateKeyPair](#generatekeypair-2) to generate a key pair based on the parameters of this type. | 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci## CipherSpecItem<sup>10+</sup> 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ciEnumerates the cipher parameters. You can use [setCipherSpec](#setcipherspec10) to set cipher parameters, and use [getCipherSpec](#getcipherspec10) to obtain cipher parameters. 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ciCurrently, only RSA and SM2 are supported. Since API version 11, the **SM2_MD_NAME_STR** parameter is supported. For details, see [Asymmetric Key Encryption and Decryption Algorithm Specifications](../../security/CryptoArchitectureKit/crypto-asym-encrypt-decrypt-spec.md). 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci| Name | Value | Description | 203e41f4b71Sopenharmony_ci| ------------ | ---- | ---------------- | 204e41f4b71Sopenharmony_ci| OAEP_MD_NAME_STR | 100 | Message digest (MD) algorithm used with the PKCS1_OAEP padding mode in RSA. | 205e41f4b71Sopenharmony_ci| OAEP_MGF_NAME_STR | 101 | Mask generation algorithm used with the PKCS1_OAEP padding mode in RSA. Currently, only MGF1 is supported. | 206e41f4b71Sopenharmony_ci| OAEP_MGF1_MD_STR | 102 | MD algorithm for the MGF1 mask generation used with the PKCS1_OAEP padding mode in RSA. | 207e41f4b71Sopenharmony_ci| OAEP_MGF1_PSRC_UINT8ARR | 103 | **pSource** byte stream used with the PKCS1_OAEP padding mode in RSA. | 208e41f4b71Sopenharmony_ci| SM2_MD_NAME_STR<sup>11+</sup> | 104 | MD algorithm used in SM2. | 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci## SignSpecItem<sup>10+</sup> 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ciEnumerates the parameters for signing and signature verification. You can use [setSignSpec](#setsignspec10) and [setVerifySpec](#setverifyspec10) to set these parameters, and use [getSignSpec](#getsignspec10) and [getVerifySpec](#getverifyspec10) to obtain the parameters. 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ciCurrently, only RSA and SM2 are supported. Since API version 11, the **SM2_USER_ID_UINT8ARR** parameter is supported. For details, see [Signing and Signature Verification Overview and Algorithm Specifications](../../security/CryptoArchitectureKit/crypto-sign-sig-verify-overview.md). 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci| Name | Value | Description | 221e41f4b71Sopenharmony_ci| ------------ | ---- | ---------------- | 222e41f4b71Sopenharmony_ci| PSS_MD_NAME_STR | 100 | MD algorithm used with the PSS padding mode in RSA. | 223e41f4b71Sopenharmony_ci| PSS_MGF_NAME_STR | 101 | Mask generation algorithm used with the PSS padding mode in RSA. Currently, only MGF1 is supported. | 224e41f4b71Sopenharmony_ci| PSS_MGF1_MD_STR | 102 | MD parameters for the MGF1 mask generation used with the PSS padding mode in RSA. | 225e41f4b71Sopenharmony_ci| PSS_SALT_LEN_NUM | 103 | Length of the salt in bytes used with the PSS padding mode in RSA. | 226e41f4b71Sopenharmony_ci| PSS_TRAILER_FIELD_NUM | 104 | Trailer field used in the encoding operation when PSS padding mode is used in RSA. The value is **1**. | 227e41f4b71Sopenharmony_ci| SM2_USER_ID_UINT8ARR<sup>11+</sup> | 105 | User ID field in SM2. | 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci## AsyKeySpec<sup>10+</sup> 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ciDefines the asymmetric key parameters for creating a key generator. You need to construct a child class object and pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. When constructing the child class object, all the parameters of the bigint type must be positive numbers in big-endian format. 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 238e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 239e41f4b71Sopenharmony_ci| algName | string | Yes | Yes | Asymmetric key algorithm, for example, **RSA**, **DSA**, **ECC**, **SM2**, **Ed25519**, **X25519**, or **DH**. | 240e41f4b71Sopenharmony_ci| specType | [AsyKeySpecType](#asykeyspectype10) | Yes | Yes | Key parameter type, which is used to distinguish public and private key parameters. | 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci## DSACommonParamsSpec<sup>10+</sup> 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the common parameters of the public and private keys in the DSA algorithm. It can be used to randomly generate a public or private key. 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 253e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 254e41f4b71Sopenharmony_ci| p | bigint | Yes | Yes | Prime modulus **p** in the DSA algorithm. | 255e41f4b71Sopenharmony_ci| q | bigint | Yes | Yes | Parameter **q**, prime factor of (**p** – 1) in the DSA algorithm. | 256e41f4b71Sopenharmony_ci| g | bigint | Yes | Yes | Parameter **g** in the DSA algorithm. | 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_ci## DSAPubKeySpec<sup>10+</sup> 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the public key in the DSA algorithm. 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 269e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 270e41f4b71Sopenharmony_ci| params | [DSACommonParamsSpec](#dsacommonparamsspec10) | Yes | Yes | Common parameters of the public and private keys in the DSA algorithm. | 271e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key in the DSA algorithm. | 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci## DSAKeyPairSpec<sup>10+</sup> 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify full parameters of the public and private keys in the DSA algorithm. 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 284e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 285e41f4b71Sopenharmony_ci| params | [DSACommonParamsSpec](#dsacommonparamsspec10) | Yes | Yes | Common parameters of the public and private keys in the DSA algorithm. | 286e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the DSA algorithm. | 287e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key **pk** in the DSA algorithm. | 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci## ECField<sup>10+</sup> 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ciDefines an elliptic curve field. Currently, only the **Fp** field is supported. 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 298e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 299e41f4b71Sopenharmony_ci| fieldType | string | Yes | Yes | Type of the elliptic curve field. Currently, only **Fp** is supported. | 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci## ECFieldFp<sup>10+</sup> 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ciDefines the prime field of the elliptic curve. It is a child class of [ECField](#ecfield10). 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 310e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 311e41f4b71Sopenharmony_ci| p | bigint | Yes | Yes | Prime **p**. | 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci## Point<sup>10+</sup> 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ciDefines a point on the elliptic curve. 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 322e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 323e41f4b71Sopenharmony_ci| x | bigint | Yes | Yes | X coordinate of the point on an elliptic curve. | 324e41f4b71Sopenharmony_ci| y | bigint | Yes | Yes | Y coordinate of the point on an elliptic curve. | 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci## ECCCommonParamsSpec<sup>10+</sup> 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the common parameters of the public and private keys in the ECC algorithm. It can be used to randomly generate a public or private key. 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 337e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 338e41f4b71Sopenharmony_ci| field | [ECField](#ecfield10) | Yes | Yes | Field of the elliptic curve. Currently, only **Fp** is supported. | 339e41f4b71Sopenharmony_ci| a | bigint | Yes | Yes | First coefficient **a** of the elliptic curve. | 340e41f4b71Sopenharmony_ci| b | bigint | Yes | Yes | Second coefficient **b** of the elliptic curve. | 341e41f4b71Sopenharmony_ci| g | [Point](#point10) | Yes | Yes | Base point g. | 342e41f4b71Sopenharmony_ci| n | bigint | Yes | Yes | Order **n** of the base point **g**. | 343e41f4b71Sopenharmony_ci| h | number | Yes | Yes | Cofactor **h**. | 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci## ECCPriKeySpec<sup>10+</sup> 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the private key in the ECC algorithm. 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 356e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 357e41f4b71Sopenharmony_ci| params | [ECCCommonParamsSpec](#ecccommonparamsspec10) | Yes | Yes | Common parameters of the public and private keys in the ECC algorithm. | 358e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the ECC algorithm. | 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ci## ECCPubKeySpec<sup>10+</sup> 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the public key in the ECC algorithm. 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 371e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 372e41f4b71Sopenharmony_ci| params | [ECCCommonParamsSpec](#ecccommonparamsspec10) | Yes | Yes | Common parameters of the public and private keys in the ECC algorithm. | 373e41f4b71Sopenharmony_ci| pk | [Point](#point10) | Yes | Yes | Public key **pk** in the ECC algorithm. | 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci## ECCKeyPairSpec<sup>10+</sup> 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify full parameters of the public and private keys in the ECC algorithm. 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 386e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 387e41f4b71Sopenharmony_ci| params | [ECCCommonParamsSpec](#ecccommonparamsspec10) | Yes | Yes | Common parameters of the public and private keys in the ECC algorithm. | 388e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the ECC algorithm. | 389e41f4b71Sopenharmony_ci| pk | [Point](#point10) | Yes | Yes | Public key **pk** in the ECC algorithm. | 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci## RSACommonParamsSpec<sup>10+</sup> 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the common parameters of the public and private keys in the RSA algorithm. It can be used to randomly generate a public or private key. 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 402e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 403e41f4b71Sopenharmony_ci| n | bigint | Yes | Yes | Modulus **n**. | 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci## RSAPubKeySpec<sup>10+</sup> 406e41f4b71Sopenharmony_ci 407e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the public key in the RSA algorithm. 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 410e41f4b71Sopenharmony_ci 411e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 412e41f4b71Sopenharmony_ci 413e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 416e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 417e41f4b71Sopenharmony_ci| params | [RSACommonParamsSpec](#rsacommonparamsspec10) | Yes | Yes | Common parameters of the public and private keys in the RSA algorithm. | 418e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key **pk** in the RSA algorithm. | 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci## RSAKeyPairSpec<sup>10+</sup> 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify full parameters of the public and private keys in the RSA algorithm. 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 431e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 432e41f4b71Sopenharmony_ci| params | [RSACommonParamsSpec](#rsacommonparamsspec10) | Yes | Yes | Common parameters of the public and private keys in the RSA algorithm. | 433e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the RSA algorithm. | 434e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key **pk** in the RSA algorithm. | 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci## ED25519PriKeySpec<sup>11+</sup> 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the private key in the Ed25519 algorithm. 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 447e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------------------------- | 448e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the Ed25519 algorithm. | 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci## ED25519PubKeySpec<sup>11+</sup> 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the public key in the Ed25519 algorithm. 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 457e41f4b71Sopenharmony_ci 458e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 461e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------------------------- | 462e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key **pk** in the Ed25519 algorithm. | 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci## ED25519KeyPairSpec<sup>11+</sup> 465e41f4b71Sopenharmony_ci 466e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify full parameters of the public and private keys in the Ed25519 algorithm. 467e41f4b71Sopenharmony_ci 468e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 471e41f4b71Sopenharmony_ci 472e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 475e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------------------------- | 476e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the Ed25519 algorithm. | 477e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key **pk** in the Ed25519 algorithm. | 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci## X25519PriKeySpec<sup>11+</sup> 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the private key in the X25519 algorithm. 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 484e41f4b71Sopenharmony_ci 485e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 490e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------------------------ | 491e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the X25519 algorithm. | 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci## X25519PubKeySpec<sup>11+</sup> 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the public key in the X25519 algorithm. 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 504e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------------------------ | 505e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key **pk** in the X25519 algorithm. | 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ci## X25519KeyPairSpec<sup>11+</sup> 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify full parameters of the public and private keys in the X25519 algorithm. 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 518e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------------------------ | 519e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the X25519 algorithm. | 520e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key **pk** in the X25519 algorithm. | 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci## DHCommonParamsSpec<sup>11+</sup> 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the public and private keys in the DH algorithm. 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 533e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ----------------------------------- | 534e41f4b71Sopenharmony_ci| p | bigint | Yes | Yes | Large prime **p** in the DH algorithm. | 535e41f4b71Sopenharmony_ci| g | bigint | Yes | Yes | Parameter **g** in the DH algorithm. | 536e41f4b71Sopenharmony_ci| l | number | Yes | Yes | Length of the private key in the DH algorithm, in bits. | 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ci## DHPriKeySpec<sup>11+</sup> 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the private key in the DH algorithm. 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 549e41f4b71Sopenharmony_ci| ------ | ------------------ | ---- | ---- | ------------------------------------ | 550e41f4b71Sopenharmony_ci| params | [DHCommonParamsSpec](#dhcommonparamsspec11) | Yes | Yes | Common parameters of the public and private keys in the DH algorithm. | 551e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the DH algorithm. | 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ci## DHPubKeySpec<sup>11+</sup> 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify the parameters of the public key in the DH algorithm. 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 558e41f4b71Sopenharmony_ci 559e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 562e41f4b71Sopenharmony_ci 563e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 564e41f4b71Sopenharmony_ci| ------ | ------------------ | ---- | ---- | ------------------------------------ | 565e41f4b71Sopenharmony_ci| params | [DHCommonParamsSpec](#dhcommonparamsspec11) | Yes | Yes | Common parameters of the public and private keys in the DH algorithm. | 566e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key **pk** in the DH algorithm. | 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci## DHKeyPairSpec<sup>11+</sup> 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ciDefines a child class of [AsyKeySpec](#asykeyspec10) used to specify full parameters of the public and private keys in the DH algorithm. 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ciTo generate a key based on key parameters, pass it to [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create a key generator. 573e41f4b71Sopenharmony_ci 574e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 575e41f4b71Sopenharmony_ci 576e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 577e41f4b71Sopenharmony_ci 578e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 579e41f4b71Sopenharmony_ci| ------ | ------------------ | ---- | ---- | ------------------------------------ | 580e41f4b71Sopenharmony_ci| params | [DHCommonParamsSpec](#dhcommonparamsspec11) | Yes | Yes | Common parameters of the public and private keys in the DH algorithm. | 581e41f4b71Sopenharmony_ci| sk | bigint | Yes | Yes | Private key **sk** in the DH algorithm. | 582e41f4b71Sopenharmony_ci| pk | bigint | Yes | Yes | Public key **pk** in the DH algorithm. | 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ci## KdfSpec<sup>11+</sup> 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ciDefines the parameters of the key derivation function. When the key derivation function is used to derive a key, you need to construct and pass in a child class object of **KdfSpec**. 587e41f4b71Sopenharmony_ci 588e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 589e41f4b71Sopenharmony_ci 590e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 593e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 594e41f4b71Sopenharmony_ci| algName | string | Yes | Yes | Algorithm of the key derivation function, for example, **PBKDF2**. | 595e41f4b71Sopenharmony_ci 596e41f4b71Sopenharmony_ci## PBKDF2Spec<sup>11+</sup> 597e41f4b71Sopenharmony_ci 598e41f4b71Sopenharmony_ciDefines the child class of [KdfSpec](#kdfspec11). It is used as a parameter for PBKDF2 key derivation. 599e41f4b71Sopenharmony_ci 600e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 601e41f4b71Sopenharmony_ci 602e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 603e41f4b71Sopenharmony_ci 604e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 605e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 606e41f4b71Sopenharmony_ci| password | string \| Uint8Array | Yes | Yes | Original password entered by the user.| 607e41f4b71Sopenharmony_ci| salt | Uint8Array | Yes | Yes | Salt value. | 608e41f4b71Sopenharmony_ci| iterations | number | Yes | Yes | Number of iterations. The value must be a positive integer. | 609e41f4b71Sopenharmony_ci| keySize | number | Yes | Yes | Length of the derived key, in bytes. | 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_ci> **NOTE** 612e41f4b71Sopenharmony_ci> 613e41f4b71Sopenharmony_ci> **password** specifies the original password. If **password** is of the string type, pass in the data used for key derivation rather than a string of the HexString or Base64 type. In addition, the string must be in utf-8 format. Otherwise, the key derived may be different from the one expected. 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ci## HKDFSpec<sup>12+</sup> 616e41f4b71Sopenharmony_ci 617e41f4b71Sopenharmony_ciDefines the child class of [KdfSpec](#kdfspec11). It is a parameter for HKDF key derivation. 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 624e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 625e41f4b71Sopenharmony_ci| key | string \| Uint8Array | Yes | Yes | Key material.| 626e41f4b71Sopenharmony_ci| salt | Uint8Array | Yes | Yes | Salt value. | 627e41f4b71Sopenharmony_ci| info | Uint8Array | Yes | Yes | Information used to expand the key. | 628e41f4b71Sopenharmony_ci| keySize | number | Yes | Yes | Length of the key to derive, in bytes.| 629e41f4b71Sopenharmony_ci 630e41f4b71Sopenharmony_ci> **NOTE** 631e41f4b71Sopenharmony_ci> 632e41f4b71Sopenharmony_ci> **key** is the original key material entered by the user. **info** and **salt** are optional. An empty string can be passed in based on the mode. 633e41f4b71Sopenharmony_ci> 634e41f4b71Sopenharmony_ci> For example, if the mode is **EXTRACT_AND_EXPAND**, all parameter values must be passed in. If the mode is **EXTRACT_ONLY**, **info** can be empty. When **HKDFspec** is constructed, pass in **null** to **info**. 635e41f4b71Sopenharmony_ci> 636e41f4b71Sopenharmony_ci> The default mode is **EXTRACT_AND_EXPAND**. The value **HKDF|SHA256|EXTRACT_AND_EXPAND** is equivalent to **HKDF|SHA256**. 637e41f4b71Sopenharmony_ci 638e41f4b71Sopenharmony_ci## SM2CipherTextSpec<sup>12+</sup> 639e41f4b71Sopenharmony_ci 640e41f4b71Sopenharmony_ciRepresents the SM2 ciphertext parameters. You can use this object to generate SM2 ciphertext in ASN.1 format or obtain SM2 parameters from the SM2 ciphertext in ASN.1 format. 641e41f4b71Sopenharmony_ci 642e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 643e41f4b71Sopenharmony_ci 644e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 647e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------------------------------------ | 648e41f4b71Sopenharmony_ci| xCoordinate | bigint | Yes | Yes | Coordinate X.| 649e41f4b71Sopenharmony_ci| yCoordinate | bigint | Yes | Yes | Coordinate Y. | 650e41f4b71Sopenharmony_ci| cipherTextData | Uint8Array | Yes | Yes | Ciphertext.| 651e41f4b71Sopenharmony_ci| hashData | Uint8Array | Yes | Yes | Hash value. | 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci> **NOTE** 654e41f4b71Sopenharmony_ci> 655e41f4b71Sopenharmony_ci> **hashData** is a value obtained by applying the SM3 algorithm to the plaintext. It has a fixed length of 256 bits. **cipherTextData** is the ciphertext with the same length as the plaintext. 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ci## Key 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ciProvides APIs for key operations. Before performing cryptographic operations (such as encryption and decryption), you need to construct a child class object of **Key** and pass it to [init()](#init-2) of the [Cipher](#cipher) instance. 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ciKeys can be generated by a key generator. 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ci### Attributes 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 670e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ---------------------------- | 671e41f4b71Sopenharmony_ci| format | string | Yes | No | Format of the key. | 672e41f4b71Sopenharmony_ci| algName | string | Yes | No | Algorithm to use. This parameter contains the key length if the key is a symmetric key. | 673e41f4b71Sopenharmony_ci 674e41f4b71Sopenharmony_ci### getEncoded 675e41f4b71Sopenharmony_ci 676e41f4b71Sopenharmony_cigetEncoded(): DataBlob 677e41f4b71Sopenharmony_ci 678e41f4b71Sopenharmony_ciObtains the byte stream of the key data. This API returns the result synchronously. The key can be a symmetric key, public key, or private key. The public key must be in DER encoding format and comply with the ASN.1 syntax and X.509 specifications. The private key must be in DER encoding format and comply with the ASN.1 syntax and PKCS#8 specifications. 679e41f4b71Sopenharmony_ci 680e41f4b71Sopenharmony_ci> **NOTE** 681e41f4b71Sopenharmony_ci> 682e41f4b71Sopenharmony_ci> When a key parameter is used to generate an RSA private key, the private key object does not support **getEncoded()**. 683e41f4b71Sopenharmony_ci 684e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 685e41f4b71Sopenharmony_ci 686e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_ci**Return value** 689e41f4b71Sopenharmony_ci 690e41f4b71Sopenharmony_ci| Type | Description | 691e41f4b71Sopenharmony_ci| --------------------- | ------------------------ | 692e41f4b71Sopenharmony_ci| [DataBlob](#datablob) | Key obtained. | 693e41f4b71Sopenharmony_ci 694e41f4b71Sopenharmony_ci**Error codes** 695e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 696e41f4b71Sopenharmony_ci 697e41f4b71Sopenharmony_ci| ID | Error Message | 698e41f4b71Sopenharmony_ci| -------- | ---------------------- | 699e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 700e41f4b71Sopenharmony_ci| 17620001 | memory error. | 701e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 702e41f4b71Sopenharmony_ci 703e41f4b71Sopenharmony_ci**Example** 704e41f4b71Sopenharmony_ci 705e41f4b71Sopenharmony_ci```ts 706e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 707e41f4b71Sopenharmony_ci 708e41f4b71Sopenharmony_ciasync function testGenerateAesKey() { 709e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES256'); 710e41f4b71Sopenharmony_ci let symKey = await symKeyGenerator.generateSymKey(); 711e41f4b71Sopenharmony_ci let encodedKey = symKey.getEncoded(); 712e41f4b71Sopenharmony_ci console.info('key hex:' + encodedKey.data); 713e41f4b71Sopenharmony_ci} 714e41f4b71Sopenharmony_ci``` 715e41f4b71Sopenharmony_ci 716e41f4b71Sopenharmony_ci## SymKey 717e41f4b71Sopenharmony_ci 718e41f4b71Sopenharmony_ciProvides APIs for symmetric key operations. It is a child class of [Key](#key). Its objects need to be passed to [init()](#init-2) of the [Cipher](#cipher) instance in symmetric encryption and decryption. 719e41f4b71Sopenharmony_ci 720e41f4b71Sopenharmony_ciSymmetric keys can be generated by a [SymKeyGenerator](#symkeygenerator). 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ci### clearMem 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ciclearMem(): void 725e41f4b71Sopenharmony_ci 726e41f4b71Sopenharmony_ciClears the keys in the memory. This API returns the result synchronously. You are advised to use this API when symmetric key instances are no longer used. 727e41f4b71Sopenharmony_ci 728e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 729e41f4b71Sopenharmony_ci 730e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 731e41f4b71Sopenharmony_ci 732e41f4b71Sopenharmony_ci**Example** 733e41f4b71Sopenharmony_ci 734e41f4b71Sopenharmony_ci<!--code_no_check--> 735e41f4b71Sopenharmony_ci```ts 736e41f4b71Sopenharmony_cilet key: cryptoFramework.SymKey; // The key is generated by a symKeyGenerator. The generation process is omitted here. 737e41f4b71Sopenharmony_cilet encodedKey = key.getEncoded(); 738e41f4b71Sopenharmony_ciconsole.info('key blob: '+ encodedKey.data); // Display key content. 739e41f4b71Sopenharmony_cikey.clearMem(); 740e41f4b71Sopenharmony_ciencodedKey = key.getEncoded(); 741e41f4b71Sopenharmony_ciconsole.info('key blob: ' + encodedKey.data); // Display all 0s. 742e41f4b71Sopenharmony_ci``` 743e41f4b71Sopenharmony_ci 744e41f4b71Sopenharmony_ci## PubKey 745e41f4b71Sopenharmony_ci 746e41f4b71Sopenharmony_ciProvides APIs for public key operations. **PubKey** is a child class of [Key](#key). It needs to be passed in during asymmetric encryption and decryption, signature verification, and key agreement. 747e41f4b71Sopenharmony_ci 748e41f4b71Sopenharmony_ciThe public key can be generated by using the asymmetric key generator [AsyKeyGenerator](#asykeygenerator) or [AsyKeyGeneratorBySpec](#asykeygeneratorbyspec10). 749e41f4b71Sopenharmony_ci 750e41f4b71Sopenharmony_ci### getAsyKeySpec<sup>10+</sup> 751e41f4b71Sopenharmony_ci 752e41f4b71Sopenharmony_cigetAsyKeySpec(itemType: AsyKeySpecItem): bigint | string | number 753e41f4b71Sopenharmony_ci 754e41f4b71Sopenharmony_ciObtains a key parameter. This API returns the result synchronously. 755e41f4b71Sopenharmony_ci 756e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 757e41f4b71Sopenharmony_ci 758e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 759e41f4b71Sopenharmony_ci 760e41f4b71Sopenharmony_ci**Parameters** 761e41f4b71Sopenharmony_ci 762e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 763e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | -------------------- | 764e41f4b71Sopenharmony_ci| itemType | [AsyKeySpecItem](#asykeyspecitem10) | Yes | Key parameter to obtain. | 765e41f4b71Sopenharmony_ci 766e41f4b71Sopenharmony_ci**Return value** 767e41f4b71Sopenharmony_ci 768e41f4b71Sopenharmony_ci| Type | Description | 769e41f4b71Sopenharmony_ci| --------------------------- | --------------------------------- | 770e41f4b71Sopenharmony_ci| bigint \| string \| number | Content of the key parameter obtained. | 771e41f4b71Sopenharmony_ci 772e41f4b71Sopenharmony_ci**Error codes** 773e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 774e41f4b71Sopenharmony_ci 775e41f4b71Sopenharmony_ci| ID | Error Message | 776e41f4b71Sopenharmony_ci| -------- | ---------------------- | 777e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 778e41f4b71Sopenharmony_ci| 17620001 | memory error. | 779e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_ci**Example** 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ci<!--code_no_check--> 784e41f4b71Sopenharmony_ci```ts 785e41f4b71Sopenharmony_cilet key: cryptoFramework.PubKey; // key is a public key object. The generation process is omitted here. 786e41f4b71Sopenharmony_cilet p = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FP_P_BN); 787e41f4b71Sopenharmony_ciconsole.info('ecc item --- p: ' + p.toString(16)); 788e41f4b71Sopenharmony_ci``` 789e41f4b71Sopenharmony_ci 790e41f4b71Sopenharmony_ci### getEncodedDer<sup>12+</sup> 791e41f4b71Sopenharmony_ci 792e41f4b71Sopenharmony_cigetEncodedDer(format: string): DataBlob 793e41f4b71Sopenharmony_ci 794e41f4b71Sopenharmony_ciObtains the public key data that complies with the ASN.1 syntax and DER encoding based on the specified format (such as the specification to use and whether to compress the key). Currently, only compressed and uncompressed ECC public key data can be obtained. 795e41f4b71Sopenharmony_ci 796e41f4b71Sopenharmony_ci> **NOTE** 797e41f4b71Sopenharmony_ci> 798e41f4b71Sopenharmony_ci> The difference between [Key.getEncoded()](#getencoded) and this API is as follows:<br> 799e41f4b71Sopenharmony_ci> 800e41f4b71Sopenharmony_ci> You can specify the format of the data to obtain in this API. 801e41f4b71Sopenharmony_ci> 802e41f4b71Sopenharmony_ci> The format of the key to obtain cannot be specified in [Key.getEncoded()](#getencoded). That is, the format of the data obtained must be the same as that of the original data. The original data format is the format of the key object generated by [convertKey](#convertkey-3). 803e41f4b71Sopenharmony_ci 804e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 805e41f4b71Sopenharmony_ci 806e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 807e41f4b71Sopenharmony_ci 808e41f4b71Sopenharmony_ci**Parameters** 809e41f4b71Sopenharmony_ci 810e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 811e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | -------------------- | 812e41f4b71Sopenharmony_ci| format | string | Yes | Format of the key. The value can be **X509\|COMPRESSED** or **X509\|UNCOMPRESSED** only. | 813e41f4b71Sopenharmony_ci 814e41f4b71Sopenharmony_ci**Return value** 815e41f4b71Sopenharmony_ci 816e41f4b71Sopenharmony_ci| Type | Description | 817e41f4b71Sopenharmony_ci| --------------------------- | --------------------------------- | 818e41f4b71Sopenharmony_ci| [DataBlob](#datablob) | Public key data in the specified format. | 819e41f4b71Sopenharmony_ci 820e41f4b71Sopenharmony_ci**Error codes** 821e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 822e41f4b71Sopenharmony_ci 823e41f4b71Sopenharmony_ci| ID | Error Message | 824e41f4b71Sopenharmony_ci| -------- | ---------------------- | 825e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 826e41f4b71Sopenharmony_ci| 17620001 | memory error. | 827e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ci**Example** 830e41f4b71Sopenharmony_ci 831e41f4b71Sopenharmony_ci<!--code_no_check--> 832e41f4b71Sopenharmony_ci```ts 833e41f4b71Sopenharmony_cilet key: cryptoFramework.PubKey; // Key is a public key object. The generation process is omitted here. 834e41f4b71Sopenharmony_cilet returnBlob = key.getEncodedDer('X509|UNCOMPRESSED'); 835e41f4b71Sopenharmony_ciconsole.info('returnBlob data: ' + returnBlob.data); 836e41f4b71Sopenharmony_ci``` 837e41f4b71Sopenharmony_ci 838e41f4b71Sopenharmony_ci### getEncodedPem<sup>12+</sup> 839e41f4b71Sopenharmony_ci 840e41f4b71Sopenharmony_cigetEncodedPem(format: string): string 841e41f4b71Sopenharmony_ci 842e41f4b71Sopenharmony_ciObtains the key data. This API returns the result synchronously. The key can be an RSA public or private key. The public key must comply with the X.509 specifications, PKCS #1 specifications, and PEM encoding format. 843e41f4b71Sopenharmony_ci 844e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 845e41f4b71Sopenharmony_ci 846e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 847e41f4b71Sopenharmony_ci 848e41f4b71Sopenharmony_ci**Parameters** 849e41f4b71Sopenharmony_ci 850e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 851e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | -------------------- | 852e41f4b71Sopenharmony_ci| format | string | Yes | Encoding format of the key data to obtain. The format for a public key can be **'PKCS1'** or **'X509'**.| 853e41f4b71Sopenharmony_ci 854e41f4b71Sopenharmony_ci**Return value** 855e41f4b71Sopenharmony_ci 856e41f4b71Sopenharmony_ci| Type | Description | 857e41f4b71Sopenharmony_ci| --------------------------- | --------------------------------- | 858e41f4b71Sopenharmony_ci| string | Key data obtained. | 859e41f4b71Sopenharmony_ci 860e41f4b71Sopenharmony_ci**Error codes** 861e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 862e41f4b71Sopenharmony_ci 863e41f4b71Sopenharmony_ci| ID | Error Message | 864e41f4b71Sopenharmony_ci| -------- | ---------------------- | 865e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 866e41f4b71Sopenharmony_ci| 17620001 | memory error. | 867e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ci**Example** 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ci```ts 872e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 873e41f4b71Sopenharmony_ci 874e41f4b71Sopenharmony_cilet publicPkcs1Str1024: string = 875e41f4b71Sopenharmony_ci "-----BEGIN RSA PUBLIC KEY-----\n" 876e41f4b71Sopenharmony_ci + "MIGJAoGBALAg3eavbX433pOjGdWdpL7HIr1w1EAeIcaCtuMfDpECPdX6X5ZjrwiE\n" 877e41f4b71Sopenharmony_ci + "h7cO51WXMT2gyN45DCQySr/8cLE2UiUVHo7qlrSatdLA9ETtgob3sJ4qTaBg5Lxg\n" 878e41f4b71Sopenharmony_ci + "SHy2gC+bvEpuIuRe64yXGuM/aP+ZvmIj9QBIVI9mJD8jLEOvQBBpAgMBAAE=\n" 879e41f4b71Sopenharmony_ci + "-----END RSA PUBLIC KEY-----\n"; 880e41f4b71Sopenharmony_ci 881e41f4b71Sopenharmony_cifunction TestPubKeyPkcs1ToX509BySync1024() { 882e41f4b71Sopenharmony_ci let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 883e41f4b71Sopenharmony_ci let keyPair = rsaGenerator.convertPemKeySync(publicPkcs1Str1024, null); 884e41f4b71Sopenharmony_ci let pubPemKey = keyPair.pubKey; 885e41f4b71Sopenharmony_ci let pubString = pubPemKey.getEncodedPem('X509'); 886e41f4b71Sopenharmony_ci console.info("[sync]TestPubKeyPkcs1ToX509BySync1024 pubString output is " + pubString); 887e41f4b71Sopenharmony_ci} 888e41f4b71Sopenharmony_ci``` 889e41f4b71Sopenharmony_ci 890e41f4b71Sopenharmony_ci## PriKey 891e41f4b71Sopenharmony_ci 892e41f4b71Sopenharmony_ciProvides APIs for private key operations. **PriKey** is a child class of [Key](#key). It needs to be passed in during asymmetric encryption and decryption, signing, and key agreement. 893e41f4b71Sopenharmony_ci 894e41f4b71Sopenharmony_ciThe private key can be generated by using the asymmetric key generator [AsyKeyGenerator](#asykeygenerator) or [AsyKeyGeneratorBySpec](#asykeygeneratorbyspec10). 895e41f4b71Sopenharmony_ci 896e41f4b71Sopenharmony_ci### clearMem 897e41f4b71Sopenharmony_ci 898e41f4b71Sopenharmony_ciclearMem(): void 899e41f4b71Sopenharmony_ci 900e41f4b71Sopenharmony_ciClears the private keys in the memory. This API returns the result synchronously. 901e41f4b71Sopenharmony_ci 902e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 903e41f4b71Sopenharmony_ci 904e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 905e41f4b71Sopenharmony_ci 906e41f4b71Sopenharmony_ci**Example** 907e41f4b71Sopenharmony_ci 908e41f4b71Sopenharmony_ci<!--code_no_check--> 909e41f4b71Sopenharmony_ci```ts 910e41f4b71Sopenharmony_cilet key: cryptoFramework.PriKey; // The key is a private key generated by the asymmetric key generator. The generation process is omitted here. 911e41f4b71Sopenharmony_cikey.clearMem(); // For the asymmetric private key, clearMem() releases the internal key struct. After clearMem is executed, getEncoded() is not supported. 912e41f4b71Sopenharmony_ci``` 913e41f4b71Sopenharmony_ci 914e41f4b71Sopenharmony_ci### getAsyKeySpec<sup>10+</sup> 915e41f4b71Sopenharmony_ci 916e41f4b71Sopenharmony_cigetAsyKeySpec(itemType: AsyKeySpecItem): bigint | string | number 917e41f4b71Sopenharmony_ci 918e41f4b71Sopenharmony_ciObtains a key parameter. This API returns the result synchronously. 919e41f4b71Sopenharmony_ci 920e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 921e41f4b71Sopenharmony_ci 922e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 923e41f4b71Sopenharmony_ci 924e41f4b71Sopenharmony_ci**Parameters** 925e41f4b71Sopenharmony_ci 926e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 927e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | -------------------- | 928e41f4b71Sopenharmony_ci| itemType | [AsyKeySpecItem](#asykeyspecitem10) | Yes | Key parameter to obtain. | 929e41f4b71Sopenharmony_ci 930e41f4b71Sopenharmony_ci**Return value** 931e41f4b71Sopenharmony_ci 932e41f4b71Sopenharmony_ci| Type | Description | 933e41f4b71Sopenharmony_ci| --------------------------- | --------------------------------- | 934e41f4b71Sopenharmony_ci| bigint \| string \| number | Content of the key parameter obtained. | 935e41f4b71Sopenharmony_ci 936e41f4b71Sopenharmony_ci**Error codes** 937e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 938e41f4b71Sopenharmony_ci 939e41f4b71Sopenharmony_ci| ID | Error Message | 940e41f4b71Sopenharmony_ci| -------- | ---------------------- | 941e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 942e41f4b71Sopenharmony_ci| 17620001 | memory error. | 943e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 944e41f4b71Sopenharmony_ci 945e41f4b71Sopenharmony_ci**Example** 946e41f4b71Sopenharmony_ci 947e41f4b71Sopenharmony_ci<!--code_no_check--> 948e41f4b71Sopenharmony_ci```ts 949e41f4b71Sopenharmony_cilet key: cryptoFramework.PriKey; // key is a private key object. The generation process is omitted here. 950e41f4b71Sopenharmony_cilet p = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FP_P_BN); 951e41f4b71Sopenharmony_ciconsole.info('ecc item --- p: ' + p.toString(16)); 952e41f4b71Sopenharmony_ci``` 953e41f4b71Sopenharmony_ci### getEncodedDer<sup>12+</sup> 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_cigetEncodedDer(format: string): DataBlob 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ciObtains the private key data that complies with the ASN.1 syntax and DER encoding based on the specified format (such as the key specifications). Currently, only the ECC private key data in PKCS #8 format can be obtained. 958e41f4b71Sopenharmony_ci 959e41f4b71Sopenharmony_ci> **NOTE** 960e41f4b71Sopenharmony_ci> 961e41f4b71Sopenharmony_ci> The difference between [Key.getEncoded()](#getencoded) and this API is as follows:<br> 962e41f4b71Sopenharmony_ci> You can specify the format of the key data to be obtained in this API. Currently, the ECC private key data in PKCS #8 format is supported. 963e41f4b71Sopenharmony_ci> The format of the key data to be obtained cannot be specified in [Key.getEncoded()](#getencoded). 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 966e41f4b71Sopenharmony_ci 967e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 968e41f4b71Sopenharmony_ci 969e41f4b71Sopenharmony_ci**Parameters** 970e41f4b71Sopenharmony_ci 971e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 972e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | -------------------- | 973e41f4b71Sopenharmony_ci| format | string | Yes | Format of the key. Currently, only **PKCS8** is supported. | 974e41f4b71Sopenharmony_ci 975e41f4b71Sopenharmony_ci**Return value** 976e41f4b71Sopenharmony_ci 977e41f4b71Sopenharmony_ci| Type | Description | 978e41f4b71Sopenharmony_ci| --------------------------- | --------------------------------- | 979e41f4b71Sopenharmony_ci| [DataBlob](#datablob) | Private key data of the specified format obtained. | 980e41f4b71Sopenharmony_ci 981e41f4b71Sopenharmony_ci**Error codes** 982e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 983e41f4b71Sopenharmony_ci 984e41f4b71Sopenharmony_ci| ID | Error Message | 985e41f4b71Sopenharmony_ci| -------- | ---------------------- | 986e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 987e41f4b71Sopenharmony_ci| 17620001 | memory error. | 988e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 989e41f4b71Sopenharmony_ci 990e41f4b71Sopenharmony_ci**Example** 991e41f4b71Sopenharmony_ci 992e41f4b71Sopenharmony_ci<!--code_no_check--> 993e41f4b71Sopenharmony_ci```ts 994e41f4b71Sopenharmony_cilet key: cryptoFramework.PriKey; // key is a private key object. The generation process is omitted here. 995e41f4b71Sopenharmony_cilet returnBlob = key.getEncodedDer('PKCS8'); 996e41f4b71Sopenharmony_ciconsole.info('returnBlob data: ' + returnBlob.data); 997e41f4b71Sopenharmony_ci``` 998e41f4b71Sopenharmony_ci 999e41f4b71Sopenharmony_ci### getEncodedPem<sup>12+</sup> 1000e41f4b71Sopenharmony_ci 1001e41f4b71Sopenharmony_cigetEncodedPem(format: string): string 1002e41f4b71Sopenharmony_ci 1003e41f4b71Sopenharmony_ciObtains the key data. This API returns the result synchronously. The key can be an RSA public or private key. The private key must comply with PKCS #8 or PKCS #1 specifications and PEM encoding format. 1004e41f4b71Sopenharmony_ci 1005e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1006e41f4b71Sopenharmony_ci 1007e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1008e41f4b71Sopenharmony_ci 1009e41f4b71Sopenharmony_ci**Parameters** 1010e41f4b71Sopenharmony_ci 1011e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1012e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | -------------------- | 1013e41f4b71Sopenharmony_ci| format | string | Yes | Encoding format of the key data to obtain. The format of a private key can be **PKCS1** or **'PKCS8'**.| 1014e41f4b71Sopenharmony_ci 1015e41f4b71Sopenharmony_ci**Return value** 1016e41f4b71Sopenharmony_ci 1017e41f4b71Sopenharmony_ci| Type | Description | 1018e41f4b71Sopenharmony_ci| --------------------------- | --------------------------------- | 1019e41f4b71Sopenharmony_ci| string | Key data obtained. | 1020e41f4b71Sopenharmony_ci 1021e41f4b71Sopenharmony_ci**Error codes** 1022e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1023e41f4b71Sopenharmony_ci 1024e41f4b71Sopenharmony_ci| ID | Error Message | 1025e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1026e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1027e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1028e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 1029e41f4b71Sopenharmony_ci 1030e41f4b71Sopenharmony_ci**Example** 1031e41f4b71Sopenharmony_ci 1032e41f4b71Sopenharmony_ci```ts 1033e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1034e41f4b71Sopenharmony_ci 1035e41f4b71Sopenharmony_cilet priKeyPkcs1Str1024: string = 1036e41f4b71Sopenharmony_ci "-----BEGIN RSA PRIVATE KEY-----\n" 1037e41f4b71Sopenharmony_ci + "MIICXQIBAAKBgQCwIN3mr21+N96ToxnVnaS+xyK9cNRAHiHGgrbjHw6RAj3V+l+W\n" 1038e41f4b71Sopenharmony_ci + "Y68IhIe3DudVlzE9oMjeOQwkMkq//HCxNlIlFR6O6pa0mrXSwPRE7YKG97CeKk2g\n" 1039e41f4b71Sopenharmony_ci + "YOS8YEh8toAvm7xKbiLkXuuMlxrjP2j/mb5iI/UASFSPZiQ/IyxDr0AQaQIDAQAB\n" 1040e41f4b71Sopenharmony_ci + "AoGAEvBFzBNa+7J4PXnRQlYEK/tvsd0bBZX33ceacMubHl6WVZbphltLq+fMTBPP\n" 1041e41f4b71Sopenharmony_ci + "LjXmtpC+aJ7Lvmyl+wTi/TsxE9vxW5JnbuRT48rnZ/Xwq0eozDeEeIBRrpsr7Rvr\n" 1042e41f4b71Sopenharmony_ci + "7ctrgzr4m4yMHq9aDgpxj8IR7oHkfwnmWr0wM3FuiVlj650CQQDineeNZ1hUTkj4\n" 1043e41f4b71Sopenharmony_ci + "D3O+iCi3mxEVEeJrpqrmSFolRMb+iozrIRKuJlgcOs+Gqi2fHfOTTL7LkpYe8SVg\n" 1044e41f4b71Sopenharmony_ci + "e3JxUdVLAkEAxvcZXk+byMFoetrnlcMR13VHUpoVeoV9qkv6CAWLlbMdgf7uKmgp\n" 1045e41f4b71Sopenharmony_ci + "a1Yp3QPDNQQqkPvrqtfR19JWZ4uy1qREmwJALTU3BjyBoH/liqb6fh4HkWk75Som\n" 1046e41f4b71Sopenharmony_ci + "MzeSjFIOubSYxhq5tgZpBZjcpvUMhV7Zrw54kwASZ+YcUJvmyvKViAm9NQJBAKF7\n" 1047e41f4b71Sopenharmony_ci + "DyXSKrem8Ws0m1ybM7HQx5As6l3EVhePDmDQT1eyRbKp+xaD74nkJpnwYdB3jyyY\n" 1048e41f4b71Sopenharmony_ci + "qc7A1tj5J5NmeEFolR0CQQCn76Xp8HCjGgLHw9vg7YyIL28y/XyfFyaZAzzK+Yia\n" 1049e41f4b71Sopenharmony_ci + "akNwQ6NeGtXSsuGCcyyfpacHp9xy8qXQNKSkw03/5vDO\n" 1050e41f4b71Sopenharmony_ci + "-----END RSA PRIVATE KEY-----\n"; 1051e41f4b71Sopenharmony_ci 1052e41f4b71Sopenharmony_cifunction TestPriKeyPkcs1ToPkcs8BySync1024() { 1053e41f4b71Sopenharmony_ci let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 1054e41f4b71Sopenharmony_ci let keyPair = rsaGenerator.convertPemKeySync(null, priKeyPkcs1Str1024); 1055e41f4b71Sopenharmony_ci let priPemKey = keyPair.priKey; 1056e41f4b71Sopenharmony_ci let priString = priPemKey.getEncodedPem('PKCS8'); 1057e41f4b71Sopenharmony_ci console.info("[sync]TestPriKeyPkcs1ToPkcs8BySync1024 priString output is " + priString); 1058e41f4b71Sopenharmony_ci} 1059e41f4b71Sopenharmony_ci``` 1060e41f4b71Sopenharmony_ci 1061e41f4b71Sopenharmony_ci## KeyPair 1062e41f4b71Sopenharmony_ci 1063e41f4b71Sopenharmony_ciDefines an asymmetric key pair, which includes a public key and a private key. 1064e41f4b71Sopenharmony_ci 1065e41f4b71Sopenharmony_ciThe asymmetric key pair can be generated by using the asymmetric key generator [AsyKeyGenerator](#asykeygenerator) or [AsyKeyGeneratorBySpec](#asykeygeneratorbyspec10). 1066e41f4b71Sopenharmony_ci 1067e41f4b71Sopenharmony_ci> **NOTE** 1068e41f4b71Sopenharmony_ci> 1069e41f4b71Sopenharmony_ci> The **pubKey** and **priKey** objects in the **KeyPair** object exist as one parameter in the **KeyPair** object. When **KeyPair** leaves the scope, its internal objects can be destructed. 1070e41f4b71Sopenharmony_ci> 1071e41f4b71Sopenharmony_ci> The service must reference the **KeyPair** object instead of the internal **pubKey** or **priKey** object. 1072e41f4b71Sopenharmony_ci 1073e41f4b71Sopenharmony_ci### Attributes 1074e41f4b71Sopenharmony_ci 1075e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1076e41f4b71Sopenharmony_ci 1077e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1078e41f4b71Sopenharmony_ci 1079e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 1080e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------ | 1081e41f4b71Sopenharmony_ci| priKey | [PriKey](#prikey) | Yes | No | Private key. | 1082e41f4b71Sopenharmony_ci| pubKey | [PubKey](#pubkey) | Yes | No | Public key. | 1083e41f4b71Sopenharmony_ci 1084e41f4b71Sopenharmony_ci## cryptoFramework.createSymKeyGenerator 1085e41f4b71Sopenharmony_ci 1086e41f4b71Sopenharmony_cicreateSymKeyGenerator(algName: string): SymKeyGenerator 1087e41f4b71Sopenharmony_ci 1088e41f4b71Sopenharmony_ciCreates a **symKeyGenerator** instance based on the specified algorithm. 1089e41f4b71Sopenharmony_ci 1090e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Symmetric Key Generation and Conversion Specifications](../../security/CryptoArchitectureKit/crypto-sym-key-generation-conversion-spec.md). 1091e41f4b71Sopenharmony_ci 1092e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1093e41f4b71Sopenharmony_ci 1094e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1095e41f4b71Sopenharmony_ci 1096e41f4b71Sopenharmony_ci**Parameters** 1097e41f4b71Sopenharmony_ci 1098e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1099e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 1100e41f4b71Sopenharmony_ci| algName | string | Yes | Algorithm used to create the **symKeyGenerator** instance.<br>For details, see **String Parameter** in [Symmetric Key Generation and Conversion Specifications](../../security/CryptoArchitectureKit/crypto-sym-key-generation-conversion-spec.md). | 1101e41f4b71Sopenharmony_ci 1102e41f4b71Sopenharmony_ci**Return value** 1103e41f4b71Sopenharmony_ci 1104e41f4b71Sopenharmony_ci| Type | Description | 1105e41f4b71Sopenharmony_ci| ----------------------------------- | -------------------------- | 1106e41f4b71Sopenharmony_ci| [SymKeyGenerator](#symkeygenerator) | **symKeyGenerator** instance created. | 1107e41f4b71Sopenharmony_ci 1108e41f4b71Sopenharmony_ci**Error codes** 1109e41f4b71Sopenharmony_ci 1110e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1111e41f4b71Sopenharmony_ci| ID | Error Message | 1112e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1113e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1114e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 1115e41f4b71Sopenharmony_ci 1116e41f4b71Sopenharmony_ci**Example** 1117e41f4b71Sopenharmony_ci 1118e41f4b71Sopenharmony_ci```ts 1119e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1120e41f4b71Sopenharmony_ci 1121e41f4b71Sopenharmony_cilet symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); 1122e41f4b71Sopenharmony_ci``` 1123e41f4b71Sopenharmony_ci 1124e41f4b71Sopenharmony_ci## SymKeyGenerator 1125e41f4b71Sopenharmony_ci 1126e41f4b71Sopenharmony_ciProvides APIs for using the **symKeyGenerator**. 1127e41f4b71Sopenharmony_ci 1128e41f4b71Sopenharmony_ciBefore using any API of the **SymKeyGenerator** class, you must create a **SymKeyGenerator** instance by using [createSymKeyGenerator](#cryptoframeworkcreatesymkeygenerator). 1129e41f4b71Sopenharmony_ci 1130e41f4b71Sopenharmony_ci### Attributes 1131e41f4b71Sopenharmony_ci 1132e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1133e41f4b71Sopenharmony_ci 1134e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1135e41f4b71Sopenharmony_ci 1136e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 1137e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ------------------------------ | 1138e41f4b71Sopenharmony_ci| algName | string | Yes | No | Algorithm used by the **symKeyGenerator**. | 1139e41f4b71Sopenharmony_ci 1140e41f4b71Sopenharmony_ci### generateSymKey 1141e41f4b71Sopenharmony_ci 1142e41f4b71Sopenharmony_cigenerateSymKey(callback: AsyncCallback\<SymKey>): void 1143e41f4b71Sopenharmony_ci 1144e41f4b71Sopenharmony_ciGenerates a key randomly. This API uses an asynchronous callback to return the result. 1145e41f4b71Sopenharmony_ci 1146e41f4b71Sopenharmony_ciThis API can be used only after a **symKeyGenerator** instance is created by using [createSymKeyGenerator](#cryptoframeworkcreatesymkeygenerator). 1147e41f4b71Sopenharmony_ci 1148e41f4b71Sopenharmony_ci**RAND_priv_bytes()** of OpenSSL can be used to generate random keys. 1149e41f4b71Sopenharmony_ci 1150e41f4b71Sopenharmony_ci> **NOTE** 1151e41f4b71Sopenharmony_ci> 1152e41f4b71Sopenharmony_ci> For the symmetric key used with the HMAC algorithm, if the hash algorithm (for example, **HMAC|SHA256**) is specified when the symmetric key generator is created, a binary key with the same length as the hash value will be randomly generated. For example, if **HMAC|SHA256** is specified, a 256-bit key will be randomly generated.<br>If no hash algorithm is specified when the symmetric key generator is created (for example, only HMAC is specified), symmetric key data cannot be randomly generated. In this case, you can use [convertKey](#convertkey) to generate symmetric key data. 1153e41f4b71Sopenharmony_ci 1154e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1155e41f4b71Sopenharmony_ci 1156e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1157e41f4b71Sopenharmony_ci 1158e41f4b71Sopenharmony_ci**Parameters** 1159e41f4b71Sopenharmony_ci 1160e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1161e41f4b71Sopenharmony_ci| -------- | --------------------------------- | ---- | ------------------------------------------------------------ | 1162e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[SymKey](#symkey)> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined** and **data** is the symmetric key generated. Otherwise, **err** is an error object. | 1163e41f4b71Sopenharmony_ci 1164e41f4b71Sopenharmony_ci**Error codes** 1165e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1166e41f4b71Sopenharmony_ci 1167e41f4b71Sopenharmony_ci| ID | Error Message | 1168e41f4b71Sopenharmony_ci| -------- | ------------- | 1169e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1170e41f4b71Sopenharmony_ci 1171e41f4b71Sopenharmony_ci**Example** 1172e41f4b71Sopenharmony_ci 1173e41f4b71Sopenharmony_ci```ts 1174e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1175e41f4b71Sopenharmony_ci 1176e41f4b71Sopenharmony_cilet symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); 1177e41f4b71Sopenharmony_ci symKeyGenerator.generateSymKey((err, symKey) => { 1178e41f4b71Sopenharmony_ci console.info('Generate symKey success, algName: ' + symKey.algName); 1179e41f4b71Sopenharmony_ci }); 1180e41f4b71Sopenharmony_ci``` 1181e41f4b71Sopenharmony_ci 1182e41f4b71Sopenharmony_ci### generateSymKey 1183e41f4b71Sopenharmony_ci 1184e41f4b71Sopenharmony_cigenerateSymKey(): Promise\<SymKey> 1185e41f4b71Sopenharmony_ci 1186e41f4b71Sopenharmony_ciGenerates a key randomly. This API uses a promise to return the result. 1187e41f4b71Sopenharmony_ci 1188e41f4b71Sopenharmony_ciThis API can be used only after a **symKeyGenerator** instance is created by using [createSymKeyGenerator](#cryptoframeworkcreatesymkeygenerator). 1189e41f4b71Sopenharmony_ci 1190e41f4b71Sopenharmony_ci**RAND_priv_bytes()** of OpenSSL can be used to generate random keys. 1191e41f4b71Sopenharmony_ci 1192e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1193e41f4b71Sopenharmony_ci 1194e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1195e41f4b71Sopenharmony_ci 1196e41f4b71Sopenharmony_ci**Return value** 1197e41f4b71Sopenharmony_ci 1198e41f4b71Sopenharmony_ci| Type | Description | 1199e41f4b71Sopenharmony_ci| --------------------------- | --------------------------------- | 1200e41f4b71Sopenharmony_ci| Promise\<[SymKey](#symkey)> | Promise used to return the symmetric key generated. | 1201e41f4b71Sopenharmony_ci 1202e41f4b71Sopenharmony_ci**Error codes** 1203e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1204e41f4b71Sopenharmony_ci 1205e41f4b71Sopenharmony_ci| ID | Error Message | 1206e41f4b71Sopenharmony_ci| -------- | ------------- | 1207e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1208e41f4b71Sopenharmony_ci 1209e41f4b71Sopenharmony_ci**Example** 1210e41f4b71Sopenharmony_ci 1211e41f4b71Sopenharmony_ci```ts 1212e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1213e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1214e41f4b71Sopenharmony_ci 1215e41f4b71Sopenharmony_cilet symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 1216e41f4b71Sopenharmony_ci symKeyGenerator.generateSymKey() 1217e41f4b71Sopenharmony_ci .then(symKey => { 1218e41f4b71Sopenharmony_ci console.info('Generate symKey success, algName: ' + symKey.algName); 1219e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 1220e41f4b71Sopenharmony_ci console.error(`Generate symKey failed, ${error.code}, ${error.message}`); 1221e41f4b71Sopenharmony_ci }); 1222e41f4b71Sopenharmony_ci``` 1223e41f4b71Sopenharmony_ci 1224e41f4b71Sopenharmony_ci### generateSymKeySync<sup>12+</sup> 1225e41f4b71Sopenharmony_ci 1226e41f4b71Sopenharmony_cigenerateSymKeySync(): SymKey 1227e41f4b71Sopenharmony_ci 1228e41f4b71Sopenharmony_ciGenerates a symmetric key randomly. This API returns the result synchronously. 1229e41f4b71Sopenharmony_ci 1230e41f4b71Sopenharmony_ciThis API can be used only after a **symKeyGenerator** instance is created by using [createSymKeyGenerator](#cryptoframeworkcreatesymkeygenerator). 1231e41f4b71Sopenharmony_ci 1232e41f4b71Sopenharmony_ci**RAND_priv_bytes()** of OpenSSL can be used to generate random keys. 1233e41f4b71Sopenharmony_ci 1234e41f4b71Sopenharmony_ci> **NOTE** 1235e41f4b71Sopenharmony_ci> 1236e41f4b71Sopenharmony_ci> For the symmetric key used with the HMAC algorithm, if the hash algorithm (for example, **HMAC|SHA256**) is specified when the symmetric key generator is created, a binary key with the same length as the hash value will be randomly generated. For example, if **HMAC|SHA256** is specified, a 256-bit key will be randomly generated.<br>If no hash algorithm is specified when the symmetric key generator is created (for example, only **HMAC** is specified), symmetric key data cannot be randomly generated. In this case, you can use [convertKeySync](#convertkeysync12) to generate symmetric key data. 1237e41f4b71Sopenharmony_ci 1238e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1239e41f4b71Sopenharmony_ci 1240e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1241e41f4b71Sopenharmony_ci 1242e41f4b71Sopenharmony_ci**Error codes** 1243e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1244e41f4b71Sopenharmony_ci 1245e41f4b71Sopenharmony_ci| ID | Error Message | 1246e41f4b71Sopenharmony_ci| -------- | ------------- | 1247e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1248e41f4b71Sopenharmony_ci 1249e41f4b71Sopenharmony_ci**Example** 1250e41f4b71Sopenharmony_ci 1251e41f4b71Sopenharmony_ci```ts 1252e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1253e41f4b71Sopenharmony_ci 1254e41f4b71Sopenharmony_cifunction testGenerateSymKeySync() { 1255e41f4b71Sopenharmony_ci // Create a SymKeyGenerator instance. 1256e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES256'); 1257e41f4b71Sopenharmony_ci // Use SymKeyGenerator to randomly generate a symmetric key. 1258e41f4b71Sopenharmony_ci let key = symKeyGenerator.generateSymKeySync(); 1259e41f4b71Sopenharmony_ci let encodedKey = key.getEncoded(); 1260e41f4b71Sopenharmony_ci console.info('key hex:' + encodedKey.data); 1261e41f4b71Sopenharmony_ci} 1262e41f4b71Sopenharmony_ci``` 1263e41f4b71Sopenharmony_ci 1264e41f4b71Sopenharmony_ci### convertKey 1265e41f4b71Sopenharmony_ci 1266e41f4b71Sopenharmony_ciconvertKey(key: DataBlob, callback: AsyncCallback\<SymKey>): void 1267e41f4b71Sopenharmony_ci 1268e41f4b71Sopenharmony_ciConverts data into a symmetric key. This API uses an asynchronous callback to return the result. 1269e41f4b71Sopenharmony_ci 1270e41f4b71Sopenharmony_ciThis API can be used only after a **symKeyGenerator** instance is created by using [createSymKeyGenerator](#cryptoframeworkcreatesymkeygenerator). 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_ci> **NOTE** 1273e41f4b71Sopenharmony_ci> 1274e41f4b71Sopenharmony_ci> For the symmetric key used with the HMAC algorithm, if the hash algorithm (for example, **HMAC|SHA256**) is specified when the symmetric key generator is created, the binary key data to be passed in must be of the same length as the hash. For example, if **HMAC|SHA256** is specified, a 256-bit key must be passed in.<br>If no hash algorithm is specified when the symmetric key generator is created (for example, only HMAC is specified), the length of the binary key data is in the range of [1,4096], in bytes. 1275e41f4b71Sopenharmony_ci 1276e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1277e41f4b71Sopenharmony_ci 1278e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1279e41f4b71Sopenharmony_ci 1280e41f4b71Sopenharmony_ci**Parameters** 1281e41f4b71Sopenharmony_ci 1282e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1283e41f4b71Sopenharmony_ci| -------- | ------------------- | ---- | ---------------------| 1284e41f4b71Sopenharmony_ci| key | [DataBlob](#datablob) | Yes | Data to convert. | 1285e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[SymKey](#symkey)> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined** and **data** is the symmetric key generated. Otherwise, **err** is an error object. | 1286e41f4b71Sopenharmony_ci 1287e41f4b71Sopenharmony_ci**Error codes** 1288e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1289e41f4b71Sopenharmony_ci 1290e41f4b71Sopenharmony_ci| ID | Error Message | 1291e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------- | 1292e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1293e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1294e41f4b71Sopenharmony_ci 1295e41f4b71Sopenharmony_ci**Example** 1296e41f4b71Sopenharmony_ci 1297e41f4b71Sopenharmony_ci```ts 1298e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1299e41f4b71Sopenharmony_ci 1300e41f4b71Sopenharmony_cifunction genKeyMaterialBlob(): cryptoFramework.DataBlob { 1301e41f4b71Sopenharmony_ci let arr = [ 1302e41f4b71Sopenharmony_ci 0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 1303e41f4b71Sopenharmony_ci 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c, 1304e41f4b71Sopenharmony_ci 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // keyLen = 192 (24 bytes) 1305e41f4b71Sopenharmony_ci let keyMaterial = new Uint8Array(arr); 1306e41f4b71Sopenharmony_ci return { data: keyMaterial }; 1307e41f4b71Sopenharmony_ci} 1308e41f4b71Sopenharmony_ci 1309e41f4b71Sopenharmony_cifunction testConvertKey() { 1310e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); 1311e41f4b71Sopenharmony_ci let keyMaterialBlob = genKeyMaterialBlob(); 1312e41f4b71Sopenharmony_ci symKeyGenerator.convertKey(keyMaterialBlob, (err, symKey) => { 1313e41f4b71Sopenharmony_ci console.info('Convert symKey success, algName: ' + symKey.algName); 1314e41f4b71Sopenharmony_ci }); 1315e41f4b71Sopenharmony_ci} 1316e41f4b71Sopenharmony_ci``` 1317e41f4b71Sopenharmony_ci 1318e41f4b71Sopenharmony_ci### convertKey 1319e41f4b71Sopenharmony_ci 1320e41f4b71Sopenharmony_ciconvertKey(key: DataBlob): Promise\<SymKey> 1321e41f4b71Sopenharmony_ci 1322e41f4b71Sopenharmony_ciConverts data into a symmetric key. This API uses a promise to return the result. 1323e41f4b71Sopenharmony_ci 1324e41f4b71Sopenharmony_ciThis API can be used only after a **symKeyGenerator** instance is created by using [createSymKeyGenerator](#cryptoframeworkcreatesymkeygenerator). 1325e41f4b71Sopenharmony_ci 1326e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1327e41f4b71Sopenharmony_ci 1328e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1329e41f4b71Sopenharmony_ci 1330e41f4b71Sopenharmony_ci**Parameters** 1331e41f4b71Sopenharmony_ci 1332e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1333e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | -------------------- | 1334e41f4b71Sopenharmony_ci| key | [DataBlob](#datablob) | Yes | Data to convert. | 1335e41f4b71Sopenharmony_ci 1336e41f4b71Sopenharmony_ci**Return value** 1337e41f4b71Sopenharmony_ci 1338e41f4b71Sopenharmony_ci| Type | Description | 1339e41f4b71Sopenharmony_ci| --------------------------- | --------------------------------- | 1340e41f4b71Sopenharmony_ci| Promise\<[SymKey](#symkey)> | Promise used to return the symmetric key generated. | 1341e41f4b71Sopenharmony_ci 1342e41f4b71Sopenharmony_ci**Error codes** 1343e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1344e41f4b71Sopenharmony_ci 1345e41f4b71Sopenharmony_ci| ID | Error Message | 1346e41f4b71Sopenharmony_ci| -------- | --------------------------------------------- | 1347e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1348e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1349e41f4b71Sopenharmony_ci 1350e41f4b71Sopenharmony_ci**Example** 1351e41f4b71Sopenharmony_ci 1352e41f4b71Sopenharmony_ci```ts 1353e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1354e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1355e41f4b71Sopenharmony_ci 1356e41f4b71Sopenharmony_cifunction genKeyMaterialBlob(): cryptoFramework.DataBlob { 1357e41f4b71Sopenharmony_ci let arr = [ 1358e41f4b71Sopenharmony_ci 0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 1359e41f4b71Sopenharmony_ci 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c, 1360e41f4b71Sopenharmony_ci 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // keyLen = 192 (24 bytes) 1361e41f4b71Sopenharmony_ci let keyMaterial = new Uint8Array(arr); 1362e41f4b71Sopenharmony_ci return { data: keyMaterial }; 1363e41f4b71Sopenharmony_ci} 1364e41f4b71Sopenharmony_ci 1365e41f4b71Sopenharmony_cifunction testConvertKey() { 1366e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); 1367e41f4b71Sopenharmony_ci let keyMaterialBlob = genKeyMaterialBlob(); 1368e41f4b71Sopenharmony_ci symKeyGenerator.convertKey(keyMaterialBlob) 1369e41f4b71Sopenharmony_ci .then(symKey => { 1370e41f4b71Sopenharmony_ci console.info('Convert symKey success, algName: ' + symKey.algName); 1371e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 1372e41f4b71Sopenharmony_ci console.error(`Convert symKey failed, ${error.code}, ${error.message}`); 1373e41f4b71Sopenharmony_ci }); 1374e41f4b71Sopenharmony_ci} 1375e41f4b71Sopenharmony_ci``` 1376e41f4b71Sopenharmony_ci 1377e41f4b71Sopenharmony_ci### convertKeySync<sup>12+</sup> 1378e41f4b71Sopenharmony_ci 1379e41f4b71Sopenharmony_ciconvertKeySync(key: DataBlob): SymKey 1380e41f4b71Sopenharmony_ci 1381e41f4b71Sopenharmony_ciConverts data into a symmetric key. This API returns the result synchronously. 1382e41f4b71Sopenharmony_ci 1383e41f4b71Sopenharmony_ciThis API can be used only after a **symKeyGenerator** instance is created by using [createSymKeyGenerator](#cryptoframeworkcreatesymkeygenerator). 1384e41f4b71Sopenharmony_ci 1385e41f4b71Sopenharmony_ci> **NOTE** 1386e41f4b71Sopenharmony_ci> 1387e41f4b71Sopenharmony_ci> For the symmetric key used with the HMAC algorithm, if the hash algorithm (for example, **HMAC|SHA256**) is specified when the symmetric key generator is created, the binary key data to be passed in must be of the same length as the hash. For example, if **HMAC|SHA256** is specified, a 256-bit key must be passed in.<br>If no hash algorithm is specified when the symmetric key generator is created (for example, only HMAC is specified), the length of the binary key data is in the range of [1,4096], in bytes. 1388e41f4b71Sopenharmony_ci 1389e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1390e41f4b71Sopenharmony_ci 1391e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1392e41f4b71Sopenharmony_ci 1393e41f4b71Sopenharmony_ci**Parameters** 1394e41f4b71Sopenharmony_ci 1395e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1396e41f4b71Sopenharmony_ci| -------- | ------------------- | ---- | ---------------------| 1397e41f4b71Sopenharmony_ci| key | [DataBlob](#datablob) | Yes | Data to convert. | 1398e41f4b71Sopenharmony_ci 1399e41f4b71Sopenharmony_ci**Error codes** 1400e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1401e41f4b71Sopenharmony_ci 1402e41f4b71Sopenharmony_ci| ID | Error Message | 1403e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------- | 1404e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1405e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1406e41f4b71Sopenharmony_ci 1407e41f4b71Sopenharmony_ci**Example** 1408e41f4b71Sopenharmony_ci 1409e41f4b71Sopenharmony_ci```ts 1410e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1411e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 1412e41f4b71Sopenharmony_ci 1413e41f4b71Sopenharmony_cifunction testConvertKeySync() { 1414e41f4b71Sopenharmony_ci // The symmetric key length is 64 bytes, that is, 512 bits. 1415e41f4b71Sopenharmony_ci let keyMessage = '87654321abcdefgh87654321abcdefgh87654321abcdefgh87654321abcdefgh'; 1416e41f4b71Sopenharmony_ci let keyBlob: cryptoFramework.DataBlob = { 1417e41f4b71Sopenharmony_ci data : new Uint8Array(buffer.from(keyMessage, 'utf-8').buffer) 1418e41f4b71Sopenharmony_ci } 1419e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('HMAC'); 1420e41f4b71Sopenharmony_ci let key = symKeyGenerator.convertKeySync(keyBlob); 1421e41f4b71Sopenharmony_ci let encodedKey = key.getEncoded(); 1422e41f4b71Sopenharmony_ci console.info('key encoded data: ' + encodedKey.data); 1423e41f4b71Sopenharmony_ci} 1424e41f4b71Sopenharmony_ci``` 1425e41f4b71Sopenharmony_ci 1426e41f4b71Sopenharmony_ci## cryptoFramework.createAsyKeyGenerator 1427e41f4b71Sopenharmony_ci 1428e41f4b71Sopenharmony_cicreateAsyKeyGenerator(algName: string): AsyKeyGenerator 1429e41f4b71Sopenharmony_ci 1430e41f4b71Sopenharmony_ciCreates an **AsyKeyGenerator** instance based on the specified algorithm. 1431e41f4b71Sopenharmony_ci 1432e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Asymmetric Key Generation and Conversion Specifications](../../security/CryptoArchitectureKit/crypto-asym-key-generation-conversion-spec.md). 1433e41f4b71Sopenharmony_ci 1434e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1435e41f4b71Sopenharmony_ci 1436e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1437e41f4b71Sopenharmony_ci 1438e41f4b71Sopenharmony_ci**Parameters** 1439e41f4b71Sopenharmony_ci 1440e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1441e41f4b71Sopenharmony_ci| ------- | ------ | ---- | -------------------------------- | 1442e41f4b71Sopenharmony_ci| algName | string | Yes | Algorithm used to create the **symkeyGenerator**. | 1443e41f4b71Sopenharmony_ci 1444e41f4b71Sopenharmony_ci**Return value** 1445e41f4b71Sopenharmony_ci 1446e41f4b71Sopenharmony_ci| Type | Description | 1447e41f4b71Sopenharmony_ci| --------------- | ---------------------------- | 1448e41f4b71Sopenharmony_ci| [AsyKeyGenerator](#asykeygenerator) | **AsyKeyGenerator** instance created. | 1449e41f4b71Sopenharmony_ci 1450e41f4b71Sopenharmony_ci**Error codes** 1451e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1452e41f4b71Sopenharmony_ci 1453e41f4b71Sopenharmony_ci| ID | Error Message | 1454e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1455e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1456e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 1457e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1458e41f4b71Sopenharmony_ci 1459e41f4b71Sopenharmony_ci**Example** 1460e41f4b71Sopenharmony_ci 1461e41f4b71Sopenharmony_ci```ts 1462e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1463e41f4b71Sopenharmony_ci 1464e41f4b71Sopenharmony_cilet asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 1465e41f4b71Sopenharmony_ci``` 1466e41f4b71Sopenharmony_ci 1467e41f4b71Sopenharmony_ci## AsyKeyGenerator 1468e41f4b71Sopenharmony_ci 1469e41f4b71Sopenharmony_ciProvides APIs for using the **AsKeyGenerator**. Before using any API of the **AsKeyGenerator** class, you must create an **AsyKeyGenerator** instance by using **createAsyKeyGenerator()**. 1470e41f4b71Sopenharmony_ci 1471e41f4b71Sopenharmony_ci### Attributes 1472e41f4b71Sopenharmony_ci 1473e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1474e41f4b71Sopenharmony_ci 1475e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1476e41f4b71Sopenharmony_ci 1477e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 1478e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | -------------------------------- | 1479e41f4b71Sopenharmony_ci| algName | string | Yes | No | Algorithm used by the **AsKeyGenerator**. | 1480e41f4b71Sopenharmony_ci 1481e41f4b71Sopenharmony_ci### generateKeyPair 1482e41f4b71Sopenharmony_ci 1483e41f4b71Sopenharmony_cigenerateKeyPair(callback: AsyncCallback\<KeyPair>): void 1484e41f4b71Sopenharmony_ci 1485e41f4b71Sopenharmony_ciGenerates a key pair randomly. This API uses an asynchronous callback to return the result. 1486e41f4b71Sopenharmony_ci 1487e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1488e41f4b71Sopenharmony_ci 1489e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1490e41f4b71Sopenharmony_ci 1491e41f4b71Sopenharmony_ci**Parameters** 1492e41f4b71Sopenharmony_ci 1493e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1494e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | ------------------------------ | 1495e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[KeyPair](#keypair)> | Yes | Callback invoked to return the key pair obtained. | 1496e41f4b71Sopenharmony_ci 1497e41f4b71Sopenharmony_ci**Error codes** 1498e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1499e41f4b71Sopenharmony_ci 1500e41f4b71Sopenharmony_ci| ID | Error Message | 1501e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1502e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>Incorrect parameter types;| 1503e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1504e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 1505e41f4b71Sopenharmony_ci 1506e41f4b71Sopenharmony_ci**Example** 1507e41f4b71Sopenharmony_ci 1508e41f4b71Sopenharmony_ci```ts 1509e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1510e41f4b71Sopenharmony_ci 1511e41f4b71Sopenharmony_cilet asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 1512e41f4b71Sopenharmony_ciasyKeyGenerator.generateKeyPair((err, keyPair) => { 1513e41f4b71Sopenharmony_ci if (err) { 1514e41f4b71Sopenharmony_ci console.error("generateKeyPair: error."); 1515e41f4b71Sopenharmony_ci return; 1516e41f4b71Sopenharmony_ci } 1517e41f4b71Sopenharmony_ci console.info('generateKeyPair: success.'); 1518e41f4b71Sopenharmony_ci}) 1519e41f4b71Sopenharmony_ci``` 1520e41f4b71Sopenharmony_ci 1521e41f4b71Sopenharmony_ci### generateKeyPair 1522e41f4b71Sopenharmony_ci 1523e41f4b71Sopenharmony_cigenerateKeyPair(): Promise\<KeyPair> 1524e41f4b71Sopenharmony_ci 1525e41f4b71Sopenharmony_ciGenerates a key pair randomly. This API uses a promise to return the result. 1526e41f4b71Sopenharmony_ci 1527e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1528e41f4b71Sopenharmony_ci 1529e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1530e41f4b71Sopenharmony_ci 1531e41f4b71Sopenharmony_ci**Return value** 1532e41f4b71Sopenharmony_ci 1533e41f4b71Sopenharmony_ci| Type | Description | 1534e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 1535e41f4b71Sopenharmony_ci| Promise\<[KeyPair](#keypair)> | Promise used to return the key pair generated. | 1536e41f4b71Sopenharmony_ci 1537e41f4b71Sopenharmony_ci**Error codes** 1538e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1539e41f4b71Sopenharmony_ci 1540e41f4b71Sopenharmony_ci| ID | Error Message | 1541e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1542e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1543e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1544e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 1545e41f4b71Sopenharmony_ci 1546e41f4b71Sopenharmony_ci**Example** 1547e41f4b71Sopenharmony_ci 1548e41f4b71Sopenharmony_ci```ts 1549e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1550e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1551e41f4b71Sopenharmony_ci 1552e41f4b71Sopenharmony_cilet asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 1553e41f4b71Sopenharmony_cilet keyGenPromise = asyKeyGenerator.generateKeyPair(); 1554e41f4b71Sopenharmony_cikeyGenPromise.then(keyPair => { 1555e41f4b71Sopenharmony_ci console.info('generateKeyPair success.'); 1556e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 1557e41f4b71Sopenharmony_ci console.error("generateKeyPair error."); 1558e41f4b71Sopenharmony_ci}); 1559e41f4b71Sopenharmony_ci``` 1560e41f4b71Sopenharmony_ci 1561e41f4b71Sopenharmony_ci### generateKeyPairSync<sup>12+</sup> 1562e41f4b71Sopenharmony_ci 1563e41f4b71Sopenharmony_cigenerateKeyPairSync(): KeyPair 1564e41f4b71Sopenharmony_ci 1565e41f4b71Sopenharmony_ciGenerates a key pair randomly. This API returns the result synchronously. 1566e41f4b71Sopenharmony_ci 1567e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1568e41f4b71Sopenharmony_ci 1569e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1570e41f4b71Sopenharmony_ci 1571e41f4b71Sopenharmony_ci**Return value** 1572e41f4b71Sopenharmony_ci 1573e41f4b71Sopenharmony_ci| Type | Description | 1574e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 1575e41f4b71Sopenharmony_ci| [KeyPair](#keypair) | Asymmetric key pair generated. | 1576e41f4b71Sopenharmony_ci 1577e41f4b71Sopenharmony_ci**Error codes** 1578e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1579e41f4b71Sopenharmony_ci 1580e41f4b71Sopenharmony_ci| ID | Error Message | 1581e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1582e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1583e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1584e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 1585e41f4b71Sopenharmony_ci 1586e41f4b71Sopenharmony_ci**Example** 1587e41f4b71Sopenharmony_ci 1588e41f4b71Sopenharmony_ci```ts 1589e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1590e41f4b71Sopenharmony_ci 1591e41f4b71Sopenharmony_cilet asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 1592e41f4b71Sopenharmony_citry { 1593e41f4b71Sopenharmony_ci let keyPairData = asyKeyGenerator.generateKeyPairSync(); 1594e41f4b71Sopenharmony_ci if (keyPairData != null) { 1595e41f4b71Sopenharmony_ci console.info('[Sync]: key pair success'); 1596e41f4b71Sopenharmony_ci } else { 1597e41f4b71Sopenharmony_ci console.error("[Sync]: get key pair result fail!"); 1598e41f4b71Sopenharmony_ci } 1599e41f4b71Sopenharmony_ci} catch (e) { 1600e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 1601e41f4b71Sopenharmony_ci} 1602e41f4b71Sopenharmony_ci``` 1603e41f4b71Sopenharmony_ci 1604e41f4b71Sopenharmony_ci### convertKey 1605e41f4b71Sopenharmony_ci 1606e41f4b71Sopenharmony_ciconvertKey(pubKey: DataBlob | null, priKey: DataBlob | null, callback: AsyncCallback\<KeyPair\>): void 1607e41f4b71Sopenharmony_ci 1608e41f4b71Sopenharmony_ciConverts data into an asymmetric key. This API uses an asynchronous callback to return the result. For details, see **Key Conversion**. 1609e41f4b71Sopenharmony_ci 1610e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1611e41f4b71Sopenharmony_ci 1612e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1613e41f4b71Sopenharmony_ci 1614e41f4b71Sopenharmony_ci**Parameters** 1615e41f4b71Sopenharmony_ci 1616e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1617e41f4b71Sopenharmony_ci| -------- | ----------- | ---- | ------------------------------ | 1618e41f4b71Sopenharmony_ci| pubKey | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Public key material to convert. If no public key is required, set this parameter to **null**. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 1619e41f4b71Sopenharmony_ci| priKey | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Private key material to convert. If no private key is required, set this parameter to **null**. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 1620e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[KeyPair](#keypair)> | Yes | Callback invoked to return the key pair obtained. | 1621e41f4b71Sopenharmony_ci 1622e41f4b71Sopenharmony_ci**Error codes** 1623e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1624e41f4b71Sopenharmony_ci 1625e41f4b71Sopenharmony_ci| ID | Error Message | 1626e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1627e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1628e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1629e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 1630e41f4b71Sopenharmony_ci 1631e41f4b71Sopenharmony_ci**Example** 1632e41f4b71Sopenharmony_ci 1633e41f4b71Sopenharmony_ci```ts 1634e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1635e41f4b71Sopenharmony_ci 1636e41f4b71Sopenharmony_cilet pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]); 1637e41f4b71Sopenharmony_cilet priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]); 1638e41f4b71Sopenharmony_cilet pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; // Binary data of the public key. 1639e41f4b71Sopenharmony_cilet priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; // Binary data of the private key. 1640e41f4b71Sopenharmony_cilet asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 1641e41f4b71Sopenharmony_ciasyKeyGenerator.convertKey(pubKeyBlob, priKeyBlob, (err, keyPair) => { 1642e41f4b71Sopenharmony_ci if (err) { 1643e41f4b71Sopenharmony_ci console.error("convertKey: error."); 1644e41f4b71Sopenharmony_ci return; 1645e41f4b71Sopenharmony_ci } 1646e41f4b71Sopenharmony_ci console.info('convertKey: success.'); 1647e41f4b71Sopenharmony_ci}); 1648e41f4b71Sopenharmony_ci``` 1649e41f4b71Sopenharmony_ci 1650e41f4b71Sopenharmony_ci### convertKey 1651e41f4b71Sopenharmony_ci 1652e41f4b71Sopenharmony_ciconvertKey(pubKey: DataBlob | null, priKey: DataBlob | null): Promise\<KeyPair> 1653e41f4b71Sopenharmony_ci 1654e41f4b71Sopenharmony_ciConverts data into an asymmetric key. This API uses a promise to return the result. For details, see **Key Conversion**. 1655e41f4b71Sopenharmony_ci 1656e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1657e41f4b71Sopenharmony_ci 1658e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1659e41f4b71Sopenharmony_ci 1660e41f4b71Sopenharmony_ci**Parameters** 1661e41f4b71Sopenharmony_ci 1662e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1663e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------------- | 1664e41f4b71Sopenharmony_ci| pubKey | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Public key material to convert. If no public key is required, set this parameter to **null**. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 1665e41f4b71Sopenharmony_ci| priKey | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Private key material to convert. If no private key is required, set this parameter to **null**. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 1666e41f4b71Sopenharmony_ci 1667e41f4b71Sopenharmony_ci**Return value** 1668e41f4b71Sopenharmony_ci 1669e41f4b71Sopenharmony_ci| Type | Description | 1670e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 1671e41f4b71Sopenharmony_ci| Promise\<[KeyPair](#keypair)> | Promise used to return the key pair generated. | 1672e41f4b71Sopenharmony_ci 1673e41f4b71Sopenharmony_ci**Error codes** 1674e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1675e41f4b71Sopenharmony_ci 1676e41f4b71Sopenharmony_ci| ID | Error Message | 1677e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1678e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1679e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1680e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 1681e41f4b71Sopenharmony_ci 1682e41f4b71Sopenharmony_ci**Example** 1683e41f4b71Sopenharmony_ci 1684e41f4b71Sopenharmony_ci```ts 1685e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1686e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1687e41f4b71Sopenharmony_ci 1688e41f4b71Sopenharmony_cilet pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]); 1689e41f4b71Sopenharmony_cilet priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]); 1690e41f4b71Sopenharmony_cilet pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; // Binary data of the public key. 1691e41f4b71Sopenharmony_cilet priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; // Binary data of the private key. 1692e41f4b71Sopenharmony_cilet asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 1693e41f4b71Sopenharmony_cilet keyGenPromise = asyKeyGenerator.convertKey(pubKeyBlob, priKeyBlob); 1694e41f4b71Sopenharmony_cikeyGenPromise.then(keyPair => { 1695e41f4b71Sopenharmony_ci console.info('convertKey success.'); 1696e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 1697e41f4b71Sopenharmony_ci console.error("convertKey error."); 1698e41f4b71Sopenharmony_ci}); 1699e41f4b71Sopenharmony_ci``` 1700e41f4b71Sopenharmony_ci 1701e41f4b71Sopenharmony_ci### convertKeySync<sup>12+</sup> 1702e41f4b71Sopenharmony_ci 1703e41f4b71Sopenharmony_ciconvertKeySync(pubKey: DataBlob | null, priKey: DataBlob | null): KeyPair 1704e41f4b71Sopenharmony_ci 1705e41f4b71Sopenharmony_ciConverts data into an asymmetric key pair. This API returns the result synchronously. For details, see **Key Conversion**. 1706e41f4b71Sopenharmony_ci 1707e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1708e41f4b71Sopenharmony_ci 1709e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1710e41f4b71Sopenharmony_ci 1711e41f4b71Sopenharmony_ci**Parameters** 1712e41f4b71Sopenharmony_ci 1713e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1714e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------------- | 1715e41f4b71Sopenharmony_ci| pubKey | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Public key material to convert. If no public key is required, set this parameter to **null**. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 1716e41f4b71Sopenharmony_ci| priKey | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Private key material to convert. If no private key is required, set this parameter to **null**. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 1717e41f4b71Sopenharmony_ci 1718e41f4b71Sopenharmony_ci**Return value** 1719e41f4b71Sopenharmony_ci 1720e41f4b71Sopenharmony_ci| Type | Description | 1721e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 1722e41f4b71Sopenharmony_ci| [KeyPair](#keypair) | Asymmetric key pair generated. | 1723e41f4b71Sopenharmony_ci 1724e41f4b71Sopenharmony_ci**Error codes** 1725e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1726e41f4b71Sopenharmony_ci 1727e41f4b71Sopenharmony_ci| ID | Error Message | 1728e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1729e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1730e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1731e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 1732e41f4b71Sopenharmony_ci 1733e41f4b71Sopenharmony_ci**Example** 1734e41f4b71Sopenharmony_ci 1735e41f4b71Sopenharmony_ci```ts 1736e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1737e41f4b71Sopenharmony_ci 1738e41f4b71Sopenharmony_cilet pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]); 1739e41f4b71Sopenharmony_cilet priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]); 1740e41f4b71Sopenharmony_cilet pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyArray }; // Binary data of the public key. 1741e41f4b71Sopenharmony_cilet priKeyBlob: cryptoFramework.DataBlob = { data: priKeyArray }; // Binary data of the private key. 1742e41f4b71Sopenharmony_cilet asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('ECC256'); 1743e41f4b71Sopenharmony_citry { 1744e41f4b71Sopenharmony_ci let keyPairData = asyKeyGenerator.convertKeySync(pubKeyBlob, priKeyBlob); 1745e41f4b71Sopenharmony_ci if (keyPairData != null) { 1746e41f4b71Sopenharmony_ci console.info('[Sync]: key pair success'); 1747e41f4b71Sopenharmony_ci } else { 1748e41f4b71Sopenharmony_ci console.error("[Sync]: convert key pair result fail!"); 1749e41f4b71Sopenharmony_ci } 1750e41f4b71Sopenharmony_ci} catch (e) { 1751e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 1752e41f4b71Sopenharmony_ci} 1753e41f4b71Sopenharmony_ci``` 1754e41f4b71Sopenharmony_ci 1755e41f4b71Sopenharmony_ci**Key Conversion** 1756e41f4b71Sopenharmony_ci 1757e41f4b71Sopenharmony_ci1. When **getEncoded()** is used to convert an asymmetric key pair (RSA, ECC, or DSA) into binary data, the public key returned is in X.509 format, and the private key is in PKCS #8 format. For an ECC private key, it is in the format defined in RFC 5915. These key data can be transferred across applications and stored persistently. 1758e41f4b71Sopenharmony_ci2. When **convertKey()** is used to convert binary data into an asymmetric key object defined by the Crypto framework, the public key must comply with the ASN.1 syntax, X.509 specifications, and DER encoding format, and the private key must comply with the ASN.1 syntax, PKCS #8 specifications, and DER encoding format. 1759e41f4b71Sopenharmony_ci3. In **convertKey()**, you can pass in either **pubKey** or **priKey**, or both of them. If one of them is passed in, the returned **KeyPair** instance contains only the key converted from the data you passed in. 1760e41f4b71Sopenharmony_ci4. When **convertKey** or **convertKeySync** is used, the system does not verify whether the specifications of the generated key object are the same as the key specifications specified for the asymmetric key generator. 1761e41f4b71Sopenharmony_ci 1762e41f4b71Sopenharmony_ci### convertPemKey<sup>12+</sup> 1763e41f4b71Sopenharmony_ci 1764e41f4b71Sopenharmony_ciconvertPemKey(pubKey: string | null, priKey: string | null): Promise\<KeyPair> 1765e41f4b71Sopenharmony_ci 1766e41f4b71Sopenharmony_ciConverts data into an asymmetric key. This API uses a promise to return the result. 1767e41f4b71Sopenharmony_ci 1768e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1769e41f4b71Sopenharmony_ci 1770e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1771e41f4b71Sopenharmony_ci 1772e41f4b71Sopenharmony_ci**Parameters** 1773e41f4b71Sopenharmony_ci 1774e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1775e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------------- | 1776e41f4b71Sopenharmony_ci| pubKey | string \| null | Yes | Public key material to convert. If no public key is required, set this parameter to **null**.| 1777e41f4b71Sopenharmony_ci| priKey | string \| null | Yes | Private key material to convert. If no private key is required, set this parameter to **null**. <br>**NOTE**: **pubKey** and **priKey** cannot be **null** at the same time.| 1778e41f4b71Sopenharmony_ci 1779e41f4b71Sopenharmony_ci**Return value** 1780e41f4b71Sopenharmony_ci 1781e41f4b71Sopenharmony_ci| Type | Description | 1782e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 1783e41f4b71Sopenharmony_ci| Promise\<[KeyPair](#keypair)> | Promise used to return the key pair generated. | 1784e41f4b71Sopenharmony_ci 1785e41f4b71Sopenharmony_ci**Error codes** 1786e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1787e41f4b71Sopenharmony_ci 1788e41f4b71Sopenharmony_ci| ID | Error Message | 1789e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1790e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1791e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1792e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 1793e41f4b71Sopenharmony_ci 1794e41f4b71Sopenharmony_ci**Example** 1795e41f4b71Sopenharmony_ci 1796e41f4b71Sopenharmony_ci```ts 1797e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1798e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1799e41f4b71Sopenharmony_ci 1800e41f4b71Sopenharmony_cilet priKeyPkcs1Str1024: string = 1801e41f4b71Sopenharmony_ci "-----BEGIN RSA PRIVATE KEY-----\n" 1802e41f4b71Sopenharmony_ci + "MIICXQIBAAKBgQCwIN3mr21+N96ToxnVnaS+xyK9cNRAHiHGgrbjHw6RAj3V+l+W\n" 1803e41f4b71Sopenharmony_ci + "Y68IhIe3DudVlzE9oMjeOQwkMkq//HCxNlIlFR6O6pa0mrXSwPRE7YKG97CeKk2g\n" 1804e41f4b71Sopenharmony_ci + "YOS8YEh8toAvm7xKbiLkXuuMlxrjP2j/mb5iI/UASFSPZiQ/IyxDr0AQaQIDAQAB\n" 1805e41f4b71Sopenharmony_ci + "AoGAEvBFzBNa+7J4PXnRQlYEK/tvsd0bBZX33ceacMubHl6WVZbphltLq+fMTBPP\n" 1806e41f4b71Sopenharmony_ci + "LjXmtpC+aJ7Lvmyl+wTi/TsxE9vxW5JnbuRT48rnZ/Xwq0eozDeEeIBRrpsr7Rvr\n" 1807e41f4b71Sopenharmony_ci + "7ctrgzr4m4yMHq9aDgpxj8IR7oHkfwnmWr0wM3FuiVlj650CQQDineeNZ1hUTkj4\n" 1808e41f4b71Sopenharmony_ci + "D3O+iCi3mxEVEeJrpqrmSFolRMb+iozrIRKuJlgcOs+Gqi2fHfOTTL7LkpYe8SVg\n" 1809e41f4b71Sopenharmony_ci + "e3JxUdVLAkEAxvcZXk+byMFoetrnlcMR13VHUpoVeoV9qkv6CAWLlbMdgf7uKmgp\n" 1810e41f4b71Sopenharmony_ci + "a1Yp3QPDNQQqkPvrqtfR19JWZ4uy1qREmwJALTU3BjyBoH/liqb6fh4HkWk75Som\n" 1811e41f4b71Sopenharmony_ci + "MzeSjFIOubSYxhq5tgZpBZjcpvUMhV7Zrw54kwASZ+YcUJvmyvKViAm9NQJBAKF7\n" 1812e41f4b71Sopenharmony_ci + "DyXSKrem8Ws0m1ybM7HQx5As6l3EVhePDmDQT1eyRbKp+xaD74nkJpnwYdB3jyyY\n" 1813e41f4b71Sopenharmony_ci + "qc7A1tj5J5NmeEFolR0CQQCn76Xp8HCjGgLHw9vg7YyIL28y/XyfFyaZAzzK+Yia\n" 1814e41f4b71Sopenharmony_ci + "akNwQ6NeGtXSsuGCcyyfpacHp9xy8qXQNKSkw03/5vDO\n" 1815e41f4b71Sopenharmony_ci + "-----END RSA PRIVATE KEY-----\n"; 1816e41f4b71Sopenharmony_ci let publicPkcs1Str1024: string = 1817e41f4b71Sopenharmony_ci "-----BEGIN RSA PUBLIC KEY-----\n" 1818e41f4b71Sopenharmony_ci + "MIGJAoGBALAg3eavbX433pOjGdWdpL7HIr1w1EAeIcaCtuMfDpECPdX6X5ZjrwiE\n" 1819e41f4b71Sopenharmony_ci + "h7cO51WXMT2gyN45DCQySr/8cLE2UiUVHo7qlrSatdLA9ETtgob3sJ4qTaBg5Lxg\n" 1820e41f4b71Sopenharmony_ci + "SHy2gC+bvEpuIuRe64yXGuM/aP+ZvmIj9QBIVI9mJD8jLEOvQBBpAgMBAAE=\n" 1821e41f4b71Sopenharmony_ci + "-----END RSA PUBLIC KEY-----\n"; 1822e41f4b71Sopenharmony_ciasync function TestConvertPemKeyByPromise() { 1823e41f4b71Sopenharmony_ci let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 1824e41f4b71Sopenharmony_ci let keyGenPromise = asyKeyGenerator.convertPemKey(publicPkcs1Str1024, priKeyPkcs1Str1024); 1825e41f4b71Sopenharmony_ci keyGenPromise.then(keyPair => { 1826e41f4b71Sopenharmony_ci console.info('convertPemKey success.'); 1827e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 1828e41f4b71Sopenharmony_ci console.error("convertPemKey error."); 1829e41f4b71Sopenharmony_ci }); 1830e41f4b71Sopenharmony_ci} 1831e41f4b71Sopenharmony_ci``` 1832e41f4b71Sopenharmony_ci 1833e41f4b71Sopenharmony_ci### convertPemKeySync<sup>12+</sup> 1834e41f4b71Sopenharmony_ci 1835e41f4b71Sopenharmony_ciconvertPemKeySync(pubKey: string | null, priKey: string | null): KeyPair 1836e41f4b71Sopenharmony_ci 1837e41f4b71Sopenharmony_ciConverts data into an asymmetric key pair. This API returns the result synchronously. 1838e41f4b71Sopenharmony_ci 1839e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1840e41f4b71Sopenharmony_ci 1841e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1842e41f4b71Sopenharmony_ci 1843e41f4b71Sopenharmony_ci**Parameters** 1844e41f4b71Sopenharmony_ci 1845e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1846e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------------- | 1847e41f4b71Sopenharmony_ci| pubKey | string \| null| Yes | Public key material to convert. If no public key is required, set this parameter to **null**.| 1848e41f4b71Sopenharmony_ci| priKey | string \| null| Yes | Private key material to convert. If no private key is required, set this parameter to **null**. <br>**NOTE**: **pubKey** and **priKey** cannot be **null** at the same time.| 1849e41f4b71Sopenharmony_ci 1850e41f4b71Sopenharmony_ci**Return value** 1851e41f4b71Sopenharmony_ci 1852e41f4b71Sopenharmony_ci| Type | Description | 1853e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 1854e41f4b71Sopenharmony_ci| [KeyPair](#keypair) | Asymmetric key pair generated. | 1855e41f4b71Sopenharmony_ci 1856e41f4b71Sopenharmony_ci**Error codes** 1857e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1858e41f4b71Sopenharmony_ci 1859e41f4b71Sopenharmony_ci| ID | Error Message | 1860e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1861e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1862e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1863e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 1864e41f4b71Sopenharmony_ci 1865e41f4b71Sopenharmony_ci**Example** 1866e41f4b71Sopenharmony_ci 1867e41f4b71Sopenharmony_ci```ts 1868e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1869e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1870e41f4b71Sopenharmony_ci 1871e41f4b71Sopenharmony_cilet priKeyPkcs1Str1024: string = 1872e41f4b71Sopenharmony_ci "-----BEGIN RSA PRIVATE KEY-----\n" 1873e41f4b71Sopenharmony_ci + "MIICXQIBAAKBgQCwIN3mr21+N96ToxnVnaS+xyK9cNRAHiHGgrbjHw6RAj3V+l+W\n" 1874e41f4b71Sopenharmony_ci + "Y68IhIe3DudVlzE9oMjeOQwkMkq//HCxNlIlFR6O6pa0mrXSwPRE7YKG97CeKk2g\n" 1875e41f4b71Sopenharmony_ci + "YOS8YEh8toAvm7xKbiLkXuuMlxrjP2j/mb5iI/UASFSPZiQ/IyxDr0AQaQIDAQAB\n" 1876e41f4b71Sopenharmony_ci + "AoGAEvBFzBNa+7J4PXnRQlYEK/tvsd0bBZX33ceacMubHl6WVZbphltLq+fMTBPP\n" 1877e41f4b71Sopenharmony_ci + "LjXmtpC+aJ7Lvmyl+wTi/TsxE9vxW5JnbuRT48rnZ/Xwq0eozDeEeIBRrpsr7Rvr\n" 1878e41f4b71Sopenharmony_ci + "7ctrgzr4m4yMHq9aDgpxj8IR7oHkfwnmWr0wM3FuiVlj650CQQDineeNZ1hUTkj4\n" 1879e41f4b71Sopenharmony_ci + "D3O+iCi3mxEVEeJrpqrmSFolRMb+iozrIRKuJlgcOs+Gqi2fHfOTTL7LkpYe8SVg\n" 1880e41f4b71Sopenharmony_ci + "e3JxUdVLAkEAxvcZXk+byMFoetrnlcMR13VHUpoVeoV9qkv6CAWLlbMdgf7uKmgp\n" 1881e41f4b71Sopenharmony_ci + "a1Yp3QPDNQQqkPvrqtfR19JWZ4uy1qREmwJALTU3BjyBoH/liqb6fh4HkWk75Som\n" 1882e41f4b71Sopenharmony_ci + "MzeSjFIOubSYxhq5tgZpBZjcpvUMhV7Zrw54kwASZ+YcUJvmyvKViAm9NQJBAKF7\n" 1883e41f4b71Sopenharmony_ci + "DyXSKrem8Ws0m1ybM7HQx5As6l3EVhePDmDQT1eyRbKp+xaD74nkJpnwYdB3jyyY\n" 1884e41f4b71Sopenharmony_ci + "qc7A1tj5J5NmeEFolR0CQQCn76Xp8HCjGgLHw9vg7YyIL28y/XyfFyaZAzzK+Yia\n" 1885e41f4b71Sopenharmony_ci + "akNwQ6NeGtXSsuGCcyyfpacHp9xy8qXQNKSkw03/5vDO\n" 1886e41f4b71Sopenharmony_ci + "-----END RSA PRIVATE KEY-----\n"; 1887e41f4b71Sopenharmony_ci let publicPkcs1Str1024: string = 1888e41f4b71Sopenharmony_ci "-----BEGIN RSA PUBLIC KEY-----\n" 1889e41f4b71Sopenharmony_ci + "MIGJAoGBALAg3eavbX433pOjGdWdpL7HIr1w1EAeIcaCtuMfDpECPdX6X5ZjrwiE\n" 1890e41f4b71Sopenharmony_ci + "h7cO51WXMT2gyN45DCQySr/8cLE2UiUVHo7qlrSatdLA9ETtgob3sJ4qTaBg5Lxg\n" 1891e41f4b71Sopenharmony_ci + "SHy2gC+bvEpuIuRe64yXGuM/aP+ZvmIj9QBIVI9mJD8jLEOvQBBpAgMBAAE=\n" 1892e41f4b71Sopenharmony_ci + "-----END RSA PUBLIC KEY-----\n"; 1893e41f4b71Sopenharmony_cifunction TestConvertPemKeyBySync() { 1894e41f4b71Sopenharmony_ci let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 1895e41f4b71Sopenharmony_ci try { 1896e41f4b71Sopenharmony_ci let keyPairData = asyKeyGenerator.convertPemKeySync(publicPkcs1Str1024, priKeyPkcs1Str1024); 1897e41f4b71Sopenharmony_ci if (keyPairData != null) { 1898e41f4b71Sopenharmony_ci console.info('[Sync]: convert pem key pair success'); 1899e41f4b71Sopenharmony_ci } else { 1900e41f4b71Sopenharmony_ci console.error("[Sync]: convert pem key pair result fail!"); 1901e41f4b71Sopenharmony_ci } 1902e41f4b71Sopenharmony_ci } catch (e) { 1903e41f4b71Sopenharmony_ci console.error(`Sync error, ${e.code}, ${e.message}`); 1904e41f4b71Sopenharmony_ci } 1905e41f4b71Sopenharmony_ci} 1906e41f4b71Sopenharmony_ci``` 1907e41f4b71Sopenharmony_ci 1908e41f4b71Sopenharmony_ci## cryptoFramework.createAsyKeyGeneratorBySpec<sup>10+</sup> 1909e41f4b71Sopenharmony_ci 1910e41f4b71Sopenharmony_cicreateAsyKeyGeneratorBySpec(asyKeySpec: AsyKeySpec): AsyKeyGeneratorBySpec 1911e41f4b71Sopenharmony_ci 1912e41f4b71Sopenharmony_ciCreates an **AsyKeyGenerator** instance based on the specified key parameter. 1913e41f4b71Sopenharmony_ci 1914e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Asymmetric Key Generation and Conversion Specifications](../../security/CryptoArchitectureKit/crypto-asym-key-generation-conversion-spec.md). 1915e41f4b71Sopenharmony_ci 1916e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1917e41f4b71Sopenharmony_ci 1918e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1919e41f4b71Sopenharmony_ci 1920e41f4b71Sopenharmony_ci**Parameters** 1921e41f4b71Sopenharmony_ci 1922e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1923e41f4b71Sopenharmony_ci| ------- | ------ | ---- | -------------------------------- | 1924e41f4b71Sopenharmony_ci| asyKeySpec | [AsyKeySpec](#asykeyspec10) | Yes | Key parameters. The **AsyKeyGenerator** generates the public/private key based on the specified parameters. | 1925e41f4b71Sopenharmony_ci 1926e41f4b71Sopenharmony_ci**Return value** 1927e41f4b71Sopenharmony_ci 1928e41f4b71Sopenharmony_ci| Type | Description | 1929e41f4b71Sopenharmony_ci| ----------------------------------------------- | -------------------------- | 1930e41f4b71Sopenharmony_ci| [AsyKeyGeneratorBySpec](#asykeygeneratorbyspec10) | Returns the **AsyKeyGenerator** instance created. | 1931e41f4b71Sopenharmony_ci 1932e41f4b71Sopenharmony_ci**Error codes** 1933e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 1934e41f4b71Sopenharmony_ci 1935e41f4b71Sopenharmony_ci| ID | Error Message | 1936e41f4b71Sopenharmony_ci| -------- | ---------------------- | 1937e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 1938e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 1939e41f4b71Sopenharmony_ci| 17620001 | memory error. | 1940e41f4b71Sopenharmony_ci 1941e41f4b71Sopenharmony_ci**Example** 1942e41f4b71Sopenharmony_ci 1943e41f4b71Sopenharmony_ci```ts 1944e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 1945e41f4b71Sopenharmony_ci 1946e41f4b71Sopenharmony_ci// Set the common parameters of the DSA1024 public and private keys. 1947e41f4b71Sopenharmony_cifunction genDsa1024CommonSpecBigE() { 1948e41f4b71Sopenharmony_ci let dsaCommonSpec: cryptoFramework.DSACommonParamsSpec = { 1949e41f4b71Sopenharmony_ci algName: "DSA", 1950e41f4b71Sopenharmony_ci specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC, 1951e41f4b71Sopenharmony_ci p: BigInt("0xed1501551b8ab3547f6355ffdc2913856ddeca198833dbd04f020e5f25e47c50e0b3894f7690a0d2ea5ed3a7be25c54292a698e1f086eb3a97deb4dbf04fcad2dafd94a9f35c3ae338ab35477e16981ded6a5b13d5ff20bf55f1b262303ad3a80af71aa6aa2354d20e9c82647664bdb6b333b7bea0a5f49d55ca40bc312a1729"), 1952e41f4b71Sopenharmony_ci q: BigInt("0xd23304044019d5d382cfeabf351636c7ab219694ac845051f60b047b"), 1953e41f4b71Sopenharmony_ci g: BigInt("0x2cc266d8bd33c3009bd67f285a257ba74f0c3a7e12b722864632a0ac3f2c17c91c2f3f67eb2d57071ef47aaa8f8e17a21ad2c1072ee1ce281362aad01dcbcd3876455cd17e1dd55d4ed36fa011db40f0bbb8cba01d066f392b5eaa9404bfcb775f2196a6bc20eeec3db32d54e94d87ecdb7a0310a5a017c5cdb8ac78597778bd"), 1954e41f4b71Sopenharmony_ci } 1955e41f4b71Sopenharmony_ci return dsaCommonSpec; 1956e41f4b71Sopenharmony_ci} 1957e41f4b71Sopenharmony_ci 1958e41f4b71Sopenharmony_ci// Set full parameters of the DSA1024 key pair. 1959e41f4b71Sopenharmony_cifunction genDsa1024KeyPairSpecBigE() { 1960e41f4b71Sopenharmony_ci let dsaCommonSpec = genDsa1024CommonSpecBigE(); 1961e41f4b71Sopenharmony_ci let dsaKeyPairSpec: cryptoFramework.DSAKeyPairSpec = { 1962e41f4b71Sopenharmony_ci algName: "DSA", 1963e41f4b71Sopenharmony_ci specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC, 1964e41f4b71Sopenharmony_ci params: dsaCommonSpec, 1965e41f4b71Sopenharmony_ci sk: BigInt("0xa2dd2adb2d11392c2541930f61f1165c370aabd2d78d00342e0a2fd9"), 1966e41f4b71Sopenharmony_ci pk: BigInt("0xae6b5d5042e758f3fc9a02d009d896df115811a75b5f7b382d8526270dbb3c029403fafb8573ba4ef0314ea86f09d01e82a14d1ebb67b0c331f41049bd6b1842658b0592e706a5e4d20c14b67977e17df7bdd464cce14b5f13bae6607760fcdf394e0b73ac70aaf141fa4dafd736bd0364b1d6e6c0d7683a5de6b9221e7f2d6b"), 1967e41f4b71Sopenharmony_ci } 1968e41f4b71Sopenharmony_ci return dsaKeyPairSpec; 1969e41f4b71Sopenharmony_ci} 1970e41f4b71Sopenharmony_ci 1971e41f4b71Sopenharmony_cilet asyKeyPairSpec = genDsa1024KeyPairSpecBigE(); // The JS input must be a positive number in big-endian format. 1972e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 1973e41f4b71Sopenharmony_ci``` 1974e41f4b71Sopenharmony_ci 1975e41f4b71Sopenharmony_ci## AsyKeyGeneratorBySpec<sup>10+</sup> 1976e41f4b71Sopenharmony_ci 1977e41f4b71Sopenharmony_ciProvides APIs for using the **AsKeyGenerator**. Before using the APIs of this class, you need to use [createAsyKeyGeneratorBySpec()](#cryptoframeworkcreateasykeygeneratorbyspec10) to create an **AsyKeyGeneratorBySpec** instance. 1978e41f4b71Sopenharmony_ci 1979e41f4b71Sopenharmony_ci### Attributes 1980e41f4b71Sopenharmony_ci 1981e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1982e41f4b71Sopenharmony_ci 1983e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 1984e41f4b71Sopenharmony_ci 1985e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 1986e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | -------------------------- | 1987e41f4b71Sopenharmony_ci| algName | string | Yes | No | Algorithm used by the asymmetric key generator. | 1988e41f4b71Sopenharmony_ci 1989e41f4b71Sopenharmony_ci### generateKeyPair 1990e41f4b71Sopenharmony_ci 1991e41f4b71Sopenharmony_cigenerateKeyPair(callback: AsyncCallback\<KeyPair>): void 1992e41f4b71Sopenharmony_ci 1993e41f4b71Sopenharmony_ciGenerates an asymmetric key pair. This API uses an asynchronous callback to return the result. 1994e41f4b71Sopenharmony_ci 1995e41f4b71Sopenharmony_ciIf a key parameter of the [COMMON_PARAMS_SPEC](#asykeyspectype10) type is used to create the key generator, a key pair will be randomly generated. If a key parameter of the [KEY_PAIR_SPEC](#asykeyspectype10) type is used to create the key generator, you can obtain a key pair that is consistent with the specified key parameters. 1996e41f4b71Sopenharmony_ci 1997e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1998e41f4b71Sopenharmony_ci 1999e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2000e41f4b71Sopenharmony_ci 2001e41f4b71Sopenharmony_ci**Parameters** 2002e41f4b71Sopenharmony_ci 2003e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2004e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | ------------------------------ | 2005e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[KeyPair](#keypair)> | Yes | Callback invoked to return the key pair obtained. | 2006e41f4b71Sopenharmony_ci 2007e41f4b71Sopenharmony_ci**Error codes** 2008e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2009e41f4b71Sopenharmony_ci 2010e41f4b71Sopenharmony_ci| ID | Error Message | 2011e41f4b71Sopenharmony_ci| -------- | ----------------------- | 2012e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>Incorrect parameter types; | 2013e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2014e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2015e41f4b71Sopenharmony_ci 2016e41f4b71Sopenharmony_ci**Example** 2017e41f4b71Sopenharmony_ci 2018e41f4b71Sopenharmony_ci<!--code_no_check--> 2019e41f4b71Sopenharmony_ci```ts 2020e41f4b71Sopenharmony_cilet asyKeyPairSpec: cryptoFramework.DSAKeyPairSpec; // Use DSA as an example. asyKeyPairSpec specifies full parameters of the private and public keys. The generation process is omitted here. 2021e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 2022e41f4b71Sopenharmony_ciasyKeyGeneratorBySpec.generateKeyPair((err, keyPair) => { 2023e41f4b71Sopenharmony_ci if (err) { 2024e41f4b71Sopenharmony_ci console.error("generateKeyPair: error."); 2025e41f4b71Sopenharmony_ci return; 2026e41f4b71Sopenharmony_ci } 2027e41f4b71Sopenharmony_ci console.info('generateKeyPair: success.'); 2028e41f4b71Sopenharmony_ci}) 2029e41f4b71Sopenharmony_ci``` 2030e41f4b71Sopenharmony_ci 2031e41f4b71Sopenharmony_ci### generateKeyPair 2032e41f4b71Sopenharmony_ci 2033e41f4b71Sopenharmony_cigenerateKeyPair(): Promise\<KeyPair> 2034e41f4b71Sopenharmony_ci 2035e41f4b71Sopenharmony_ciGenerates an asymmetric key pair. This API uses a promise to return the result. 2036e41f4b71Sopenharmony_ci 2037e41f4b71Sopenharmony_ciIf a key parameter of the [COMMON_PARAMS_SPEC](#asykeyspectype10) type is used to create the key generator, a key pair will be randomly generated. If a key parameter of the [KEY_PAIR_SPEC](#asykeyspectype10) type is used to create the key generator, you can obtain a key pair that is consistent with the specified key parameters. 2038e41f4b71Sopenharmony_ci 2039e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2040e41f4b71Sopenharmony_ci 2041e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2042e41f4b71Sopenharmony_ci 2043e41f4b71Sopenharmony_ci**Return value** 2044e41f4b71Sopenharmony_ci 2045e41f4b71Sopenharmony_ci| Type | Description | 2046e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2047e41f4b71Sopenharmony_ci| Promise\<[KeyPair](#keypair)> | Promise used to return the key pair generated. | 2048e41f4b71Sopenharmony_ci 2049e41f4b71Sopenharmony_ci**Error codes** 2050e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2051e41f4b71Sopenharmony_ci 2052e41f4b71Sopenharmony_ci| ID | Error Message | 2053e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2054e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2055e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2056e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2057e41f4b71Sopenharmony_ci 2058e41f4b71Sopenharmony_ci**Example** 2059e41f4b71Sopenharmony_ci 2060e41f4b71Sopenharmony_ci<!--code_no_check--> 2061e41f4b71Sopenharmony_ci```ts 2062e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2063e41f4b71Sopenharmony_ci 2064e41f4b71Sopenharmony_cilet asyKeyPairSpec: cryptoFramework.DSAKeyPairSpec; // Use DSA as an example. asyKeyPairSpec specifies full parameters of the private and public keys. The generation process is omitted here. 2065e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 2066e41f4b71Sopenharmony_cilet keyGenPromise = asyKeyGeneratorBySpec.generateKeyPair(); 2067e41f4b71Sopenharmony_cikeyGenPromise.then(keyPair => { 2068e41f4b71Sopenharmony_ci console.info('generateKeyPair success.'); 2069e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 2070e41f4b71Sopenharmony_ci console.error("generateKeyPair error."); 2071e41f4b71Sopenharmony_ci}); 2072e41f4b71Sopenharmony_ci``` 2073e41f4b71Sopenharmony_ci 2074e41f4b71Sopenharmony_ci### generateKeyPairSync<sup>12+</sup> 2075e41f4b71Sopenharmony_ci 2076e41f4b71Sopenharmony_cigenerateKeyPairSync(): KeyPair 2077e41f4b71Sopenharmony_ci 2078e41f4b71Sopenharmony_ciGenerates an asymmetric key pair. This API returns the result synchronously. 2079e41f4b71Sopenharmony_ci 2080e41f4b71Sopenharmony_ciIf a key parameter of the [COMMON_PARAMS_SPEC](#asykeyspectype10) type is used to create the key generator, a key pair will be randomly generated. If a key parameter of the [KEY_PAIR_SPEC](#asykeyspectype10) type is used to create the key generator, you can obtain a key pair that is consistent with the specified key parameters. 2081e41f4b71Sopenharmony_ci 2082e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2083e41f4b71Sopenharmony_ci 2084e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2085e41f4b71Sopenharmony_ci 2086e41f4b71Sopenharmony_ci**Return value** 2087e41f4b71Sopenharmony_ci 2088e41f4b71Sopenharmony_ci| Type | Description | 2089e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2090e41f4b71Sopenharmony_ci| [KeyPair](#keypair) | Asymmetric key pair generated. | 2091e41f4b71Sopenharmony_ci 2092e41f4b71Sopenharmony_ci**Error codes** 2093e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2094e41f4b71Sopenharmony_ci 2095e41f4b71Sopenharmony_ci| ID | Error Message | 2096e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2097e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2098e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2099e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2100e41f4b71Sopenharmony_ci 2101e41f4b71Sopenharmony_ci**Example** 2102e41f4b71Sopenharmony_ci 2103e41f4b71Sopenharmony_ci<!--code_no_check--> 2104e41f4b71Sopenharmony_ci```ts 2105e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2106e41f4b71Sopenharmony_ci 2107e41f4b71Sopenharmony_cilet asyKeyPairSpec: cryptoFramework.DSAKeyPairSpec; // Use DSA as an example. asyKeyPairSpec specifies full parameters of the private and public keys. The generation process is omitted here. 2108e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 2109e41f4b71Sopenharmony_citry { 2110e41f4b71Sopenharmony_ci let keyPairData = asyKeyGeneratorBySpec.generateKeyPairSync(); 2111e41f4b71Sopenharmony_ci if (keyPairData != null) { 2112e41f4b71Sopenharmony_ci console.info('[Sync]: key pair success'); 2113e41f4b71Sopenharmony_ci } else { 2114e41f4b71Sopenharmony_ci console.error("[Sync]: get key pair result fail!"); 2115e41f4b71Sopenharmony_ci } 2116e41f4b71Sopenharmony_ci} catch (error) { 2117e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 2118e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 2119e41f4b71Sopenharmony_ci} 2120e41f4b71Sopenharmony_ci``` 2121e41f4b71Sopenharmony_ci 2122e41f4b71Sopenharmony_ci### generatePriKey 2123e41f4b71Sopenharmony_ci 2124e41f4b71Sopenharmony_cigeneratePriKey(callback: AsyncCallback\<PriKey>): void 2125e41f4b71Sopenharmony_ci 2126e41f4b71Sopenharmony_ciGenerates an asymmetric key pair. This API uses an asynchronous callback to return the result. 2127e41f4b71Sopenharmony_ci 2128e41f4b71Sopenharmony_ciIf a key parameter of the [PRIVATE_KEY_SPEC](#asykeyspectype10) type is used to create the key generator, the specified private key can be obtained. If a key parameter of the [KEY_PAIR_SPEC](#asykeyspectype10) type is used to create the key generator, you can obtain the specified private key from the key pair generated. 2129e41f4b71Sopenharmony_ci 2130e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2131e41f4b71Sopenharmony_ci 2132e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2133e41f4b71Sopenharmony_ci 2134e41f4b71Sopenharmony_ci**Parameters** 2135e41f4b71Sopenharmony_ci 2136e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2137e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | ------------------------------ | 2138e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[PriKey](#prikey)> | Yes | Callback invoked to return the key pair obtained. | 2139e41f4b71Sopenharmony_ci 2140e41f4b71Sopenharmony_ci**Error codes** 2141e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2142e41f4b71Sopenharmony_ci 2143e41f4b71Sopenharmony_ci| ID | Error Message | 2144e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2145e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>Mandatory parameters are left unspecified; | 2146e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2147e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2148e41f4b71Sopenharmony_ci 2149e41f4b71Sopenharmony_ci**Example** 2150e41f4b71Sopenharmony_ci 2151e41f4b71Sopenharmony_ci<!--code_no_check--> 2152e41f4b71Sopenharmony_ci```ts 2153e41f4b71Sopenharmony_cilet asyKeyPairSpec: cryptoFramework.DSAKeyPairSpec; // Use DSA as an example. asyKeyPairSpec specifies full parameters of the private and public keys. The generation process is omitted here. 2154e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 2155e41f4b71Sopenharmony_ciasyKeyGeneratorBySpec.generatePriKey((err, prikey) => { 2156e41f4b71Sopenharmony_ci if (err) { 2157e41f4b71Sopenharmony_ci console.error("generatePriKey: error."); 2158e41f4b71Sopenharmony_ci return; 2159e41f4b71Sopenharmony_ci } 2160e41f4b71Sopenharmony_ci console.info('generatePriKey: success.'); 2161e41f4b71Sopenharmony_ci}) 2162e41f4b71Sopenharmony_ci``` 2163e41f4b71Sopenharmony_ci 2164e41f4b71Sopenharmony_ci### generatePriKey 2165e41f4b71Sopenharmony_ci 2166e41f4b71Sopenharmony_cigeneratePriKey(): Promise\<PriKey> 2167e41f4b71Sopenharmony_ci 2168e41f4b71Sopenharmony_ciGenerates an asymmetric key pair. This API uses a promise to return the result. 2169e41f4b71Sopenharmony_ci 2170e41f4b71Sopenharmony_ciIf a key parameter of the [PRIVATE_KEY_SPEC](#asykeyspectype10) type is used to create the key generator, the specified private key can be obtained. If a key parameter of the [KEY_PAIR_SPEC](#asykeyspectype10) type is used to create the key generator, you can obtain the specified private key from the key pair generated. 2171e41f4b71Sopenharmony_ci 2172e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2173e41f4b71Sopenharmony_ci 2174e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2175e41f4b71Sopenharmony_ci 2176e41f4b71Sopenharmony_ci**Return value** 2177e41f4b71Sopenharmony_ci 2178e41f4b71Sopenharmony_ci| Type | Description | 2179e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2180e41f4b71Sopenharmony_ci| Promise\<[PriKey](#prikey)> | Promise used to return the key pair generated. | 2181e41f4b71Sopenharmony_ci 2182e41f4b71Sopenharmony_ci**Error codes** 2183e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2184e41f4b71Sopenharmony_ci 2185e41f4b71Sopenharmony_ci| ID | Error Message | 2186e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2187e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2188e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2189e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2190e41f4b71Sopenharmony_ci 2191e41f4b71Sopenharmony_ci**Example** 2192e41f4b71Sopenharmony_ci 2193e41f4b71Sopenharmony_ci<!--code_no_check--> 2194e41f4b71Sopenharmony_ci```ts 2195e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2196e41f4b71Sopenharmony_ci 2197e41f4b71Sopenharmony_cilet asyKeyPairSpec: cryptoFramework.DSAKeyPairSpec; // Use DSA as an example. asyKeyPairSpec specifies full parameters of the private and public keys. The generation process is omitted here. 2198e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 2199e41f4b71Sopenharmony_cilet keyGenPromise = asyKeyGeneratorBySpec.generatePriKey(); 2200e41f4b71Sopenharmony_cikeyGenPromise.then(priKey => { 2201e41f4b71Sopenharmony_ci console.info('generatePriKey success.'); 2202e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 2203e41f4b71Sopenharmony_ci console.error("generatePriKey error."); 2204e41f4b71Sopenharmony_ci}); 2205e41f4b71Sopenharmony_ci``` 2206e41f4b71Sopenharmony_ci 2207e41f4b71Sopenharmony_ci### generatePriKeySync<sup>12+</sup> 2208e41f4b71Sopenharmony_ci 2209e41f4b71Sopenharmony_cigeneratePriKeySync(): PriKey 2210e41f4b71Sopenharmony_ci 2211e41f4b71Sopenharmony_ciGenerates a private key randomly. This API returns the result synchronously. 2212e41f4b71Sopenharmony_ci 2213e41f4b71Sopenharmony_ciIf a key parameter of the [PRIVATE_KEY_SPEC](#asykeyspectype10) type is used to create the key generator, a private key can be obtained. If a key parameter of the [KEY_PAIR_SPEC](#asykeyspectype10) type is used to create the key generator, you can obtain the private key from the key pair generated. 2214e41f4b71Sopenharmony_ci 2215e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2216e41f4b71Sopenharmony_ci 2217e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2218e41f4b71Sopenharmony_ci 2219e41f4b71Sopenharmony_ci**Return value** 2220e41f4b71Sopenharmony_ci 2221e41f4b71Sopenharmony_ci| Type | Description | 2222e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2223e41f4b71Sopenharmony_ci| [PriKey](#prikey) | Private key generated. | 2224e41f4b71Sopenharmony_ci 2225e41f4b71Sopenharmony_ci**Error codes** 2226e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2227e41f4b71Sopenharmony_ci 2228e41f4b71Sopenharmony_ci| ID | Error Message | 2229e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2230e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2231e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2232e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2233e41f4b71Sopenharmony_ci 2234e41f4b71Sopenharmony_ci**Example** 2235e41f4b71Sopenharmony_ci 2236e41f4b71Sopenharmony_ci<!--code_no_check--> 2237e41f4b71Sopenharmony_ci```ts 2238e41f4b71Sopenharmony_cilet asyKeyPairSpec: cryptoFramework.DSAKeyPairSpec; // Use DSA as an example. asyKeyPairSpec specifies full parameters of the private and public keys. The generation process is omitted here. 2239e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 2240e41f4b71Sopenharmony_citry { 2241e41f4b71Sopenharmony_ci let priKeyData = asyKeyGeneratorBySpec.generatePriKeySync(); 2242e41f4b71Sopenharmony_ci if (priKeyData != null) { 2243e41f4b71Sopenharmony_ci console.info('[Sync]: pri key success'); 2244e41f4b71Sopenharmony_ci } else { 2245e41f4b71Sopenharmony_ci console.error("[Sync]: get pri key result fail!"); 2246e41f4b71Sopenharmony_ci } 2247e41f4b71Sopenharmony_ci} catch (e) { 2248e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 2249e41f4b71Sopenharmony_ci} 2250e41f4b71Sopenharmony_ci``` 2251e41f4b71Sopenharmony_ci 2252e41f4b71Sopenharmony_ci### generatePubKey 2253e41f4b71Sopenharmony_ci 2254e41f4b71Sopenharmony_cigeneratePubKey(callback: AsyncCallback\<PubKey>): void 2255e41f4b71Sopenharmony_ci 2256e41f4b71Sopenharmony_ciGenerates an asymmetric key pair. This API uses an asynchronous callback to return the result. 2257e41f4b71Sopenharmony_ci 2258e41f4b71Sopenharmony_ciIf a key parameter of the [PUBLIC_KEY_SPEC](#asykeyspectype10) type is used to create the key generator, the specified public key can be obtained. If a key parameter of the [KEY_PAIR_SPEC](#asykeyspectype10) type is used to create the key generator, you can obtain the specified public key from the key pair generated. 2259e41f4b71Sopenharmony_ci 2260e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2261e41f4b71Sopenharmony_ci 2262e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2263e41f4b71Sopenharmony_ci 2264e41f4b71Sopenharmony_ci**Parameters** 2265e41f4b71Sopenharmony_ci 2266e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2267e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | ------------------------------ | 2268e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[PubKey](#pubkey)> | Yes | Callback invoked to return the key pair obtained. | 2269e41f4b71Sopenharmony_ci 2270e41f4b71Sopenharmony_ci**Error codes** 2271e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2272e41f4b71Sopenharmony_ci 2273e41f4b71Sopenharmony_ci| ID | Error Message | 2274e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2275e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes:<br> Incorrect parameter types; | 2276e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2277e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2278e41f4b71Sopenharmony_ci 2279e41f4b71Sopenharmony_ci**Example** 2280e41f4b71Sopenharmony_ci 2281e41f4b71Sopenharmony_ci<!--code_no_check--> 2282e41f4b71Sopenharmony_ci```ts 2283e41f4b71Sopenharmony_cilet asyKeyPairSpec: cryptoFramework.DSAKeyPairSpec; // Use DSA as an example. asyKeyPairSpec specifies full parameters of the private and public keys. The generation process is omitted here. 2284e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 2285e41f4b71Sopenharmony_ciasyKeyGeneratorBySpec.generatePubKey((err, pubKey) => { 2286e41f4b71Sopenharmony_ci if (err) { 2287e41f4b71Sopenharmony_ci console.error("generatePubKey: error."); 2288e41f4b71Sopenharmony_ci return; 2289e41f4b71Sopenharmony_ci } 2290e41f4b71Sopenharmony_ci console.info('generatePubKey: success.'); 2291e41f4b71Sopenharmony_ci}) 2292e41f4b71Sopenharmony_ci``` 2293e41f4b71Sopenharmony_ci 2294e41f4b71Sopenharmony_ci### generatePubKey 2295e41f4b71Sopenharmony_ci 2296e41f4b71Sopenharmony_cigeneratePubKey(): Promise\<PubKey> 2297e41f4b71Sopenharmony_ci 2298e41f4b71Sopenharmony_ciGenerates an asymmetric key pair. This API uses a promise to return the result. 2299e41f4b71Sopenharmony_ci 2300e41f4b71Sopenharmony_ciIf a key parameter of the [PUBLIC_KEY_SPEC](#asykeyspectype10) type is used to create the key generator, the specified public key can be obtained. If a key parameter of the [KEY_PAIR_SPEC](#asykeyspectype10) type is used to create the key generator, you can obtain the specified public key from the key pair generated. 2301e41f4b71Sopenharmony_ci 2302e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2303e41f4b71Sopenharmony_ci 2304e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2305e41f4b71Sopenharmony_ci 2306e41f4b71Sopenharmony_ci**Return value** 2307e41f4b71Sopenharmony_ci 2308e41f4b71Sopenharmony_ci| Type | Description | 2309e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2310e41f4b71Sopenharmony_ci| Promise\<[PubKey](#pubkey)> | Promise used to return the key pair generated. | 2311e41f4b71Sopenharmony_ci 2312e41f4b71Sopenharmony_ci**Error codes** 2313e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2314e41f4b71Sopenharmony_ci 2315e41f4b71Sopenharmony_ci| ID | Error Message | 2316e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2317e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2318e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2319e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2320e41f4b71Sopenharmony_ci 2321e41f4b71Sopenharmony_ci**Example** 2322e41f4b71Sopenharmony_ci 2323e41f4b71Sopenharmony_ci<!--code_no_check--> 2324e41f4b71Sopenharmony_ci```ts 2325e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2326e41f4b71Sopenharmony_ci 2327e41f4b71Sopenharmony_cilet asyKeyPairSpec: cryptoFramework.DSAKeyPairSpec; // Use DSA as an example. asyKeyPairSpec specifies full parameters of the private and public keys. The generation process is omitted here. 2328e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 2329e41f4b71Sopenharmony_cilet keyGenPromise = asyKeyGeneratorBySpec.generatePubKey(); 2330e41f4b71Sopenharmony_cikeyGenPromise.then(pubKey => { 2331e41f4b71Sopenharmony_ci console.info('generatePubKey success.'); 2332e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 2333e41f4b71Sopenharmony_ci console.error("generatePubKey error."); 2334e41f4b71Sopenharmony_ci}); 2335e41f4b71Sopenharmony_ci``` 2336e41f4b71Sopenharmony_ci 2337e41f4b71Sopenharmony_ci### generatePubKeySync<sup>12+</sup> 2338e41f4b71Sopenharmony_ci 2339e41f4b71Sopenharmony_cigeneratePubKeySync(): PubKey 2340e41f4b71Sopenharmony_ci 2341e41f4b71Sopenharmony_ciGenerates a public key. This API returns the result synchronously. 2342e41f4b71Sopenharmony_ci 2343e41f4b71Sopenharmony_ciIf a key parameter of the [PUBLIC_KEY_SPEC](#asykeyspectype10) type is used to create the key generator, the specified public key can be obtained. If a key parameter of the [KEY_PAIR_SPEC](#asykeyspectype10) type is used to create the key generator, you can obtain the specified public key from the key pair generated. 2344e41f4b71Sopenharmony_ci 2345e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2346e41f4b71Sopenharmony_ci 2347e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2348e41f4b71Sopenharmony_ci 2349e41f4b71Sopenharmony_ci**Return value** 2350e41f4b71Sopenharmony_ci 2351e41f4b71Sopenharmony_ci| Type | Description | 2352e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2353e41f4b71Sopenharmony_ci| [PubKey](#pubkey) | Private key generated. | 2354e41f4b71Sopenharmony_ci 2355e41f4b71Sopenharmony_ci**Error codes** 2356e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2357e41f4b71Sopenharmony_ci 2358e41f4b71Sopenharmony_ci| ID | Error Message | 2359e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2360e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2361e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2362e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2363e41f4b71Sopenharmony_ci 2364e41f4b71Sopenharmony_ci**Example** 2365e41f4b71Sopenharmony_ci 2366e41f4b71Sopenharmony_ci<!--code_no_check--> 2367e41f4b71Sopenharmony_ci```ts 2368e41f4b71Sopenharmony_cilet asyKeyPairSpec: cryptoFramework.DSAKeyPairSpec; // Use DSA as an example. asyKeyPairSpec specifies full parameters of the private and public keys. The generation process is omitted here. 2369e41f4b71Sopenharmony_cilet asyKeyGeneratorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeyPairSpec); 2370e41f4b71Sopenharmony_citry { 2371e41f4b71Sopenharmony_ci let pubKeyData = asyKeyGeneratorBySpec.generatePubKeySync(); 2372e41f4b71Sopenharmony_ci if (pubKeyData != null) { 2373e41f4b71Sopenharmony_ci console.info('[Sync]: pub key success'); 2374e41f4b71Sopenharmony_ci } else { 2375e41f4b71Sopenharmony_ci console.error("[Sync]: get pub key result fail!"); 2376e41f4b71Sopenharmony_ci } 2377e41f4b71Sopenharmony_ci} catch (e) { 2378e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 2379e41f4b71Sopenharmony_ci} 2380e41f4b71Sopenharmony_ci``` 2381e41f4b71Sopenharmony_ci 2382e41f4b71Sopenharmony_ci## ECCKeyUtil<sup>11+</sup> 2383e41f4b71Sopenharmony_ci 2384e41f4b71Sopenharmony_ciProvides APIs for generating common parameters for an asymmetric key pair based on the elliptic curve name. 2385e41f4b71Sopenharmony_ci 2386e41f4b71Sopenharmony_ci### genECCCommonParamsSpec<sup>11+</sup> 2387e41f4b71Sopenharmony_ci 2388e41f4b71Sopenharmony_cistatic genECCCommonParamsSpec(curveName: string): ECCCommonParamsSpec 2389e41f4b71Sopenharmony_ci 2390e41f4b71Sopenharmony_ciGenerates common parameters for an asymmetric key pair based on the specified name identifier (NID) of an elliptic curve. For details, see [ECC](../../security/CryptoArchitectureKit/crypto-asym-key-generation-conversion-spec.md#ecc) and [SM2](../../security/CryptoArchitectureKit/crypto-asym-key-generation-conversion-spec.md#sm2). 2391e41f4b71Sopenharmony_ci 2392e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2393e41f4b71Sopenharmony_ci 2394e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2395e41f4b71Sopenharmony_ci 2396e41f4b71Sopenharmony_ci**Parameters** 2397e41f4b71Sopenharmony_ci 2398e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2399e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------------------------- | 2400e41f4b71Sopenharmony_ci| curveName | string | Yes | NID of the elliptic curve. | 2401e41f4b71Sopenharmony_ci 2402e41f4b71Sopenharmony_ci**Return value** 2403e41f4b71Sopenharmony_ci 2404e41f4b71Sopenharmony_ci| Type | Description | 2405e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2406e41f4b71Sopenharmony_ci| [ECCCommonParamsSpec](#ecccommonparamsspec10) | ECC common parameters generated. | 2407e41f4b71Sopenharmony_ci 2408e41f4b71Sopenharmony_ci**Error codes** 2409e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2410e41f4b71Sopenharmony_ci 2411e41f4b71Sopenharmony_ci| ID | Error Message | 2412e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 2413e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2414e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 2415e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2416e41f4b71Sopenharmony_ci 2417e41f4b71Sopenharmony_ci**Example** 2418e41f4b71Sopenharmony_ci 2419e41f4b71Sopenharmony_ci```ts 2420e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 2421e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2422e41f4b71Sopenharmony_citry { 2423e41f4b71Sopenharmony_ci let ECCCommonParamsSpec = cryptoFramework.ECCKeyUtil.genECCCommonParamsSpec('NID_brainpoolP160r1'); 2424e41f4b71Sopenharmony_ci console.info('genECCCommonParamsSpec success'); 2425e41f4b71Sopenharmony_ci} catch (err) { 2426e41f4b71Sopenharmony_ci let e: BusinessError = err as BusinessError; 2427e41f4b71Sopenharmony_ci console.error(`genECCCommonParamsSpec error, ${e.code}, ${e.message}`); 2428e41f4b71Sopenharmony_ci} 2429e41f4b71Sopenharmony_ci``` 2430e41f4b71Sopenharmony_ci 2431e41f4b71Sopenharmony_ci### convertPoint<sup>12+</sup> 2432e41f4b71Sopenharmony_ci 2433e41f4b71Sopenharmony_cistatic convertPoint(curveName: string, encodedPoint: Uint8Array): Point 2434e41f4b71Sopenharmony_ci 2435e41f4b71Sopenharmony_ciConverts the specified point data into a **Point** object based on the curve name, that is, Name IDentifier (NID). Currently, compressed and uncompressed point data is supported. 2436e41f4b71Sopenharmony_ci 2437e41f4b71Sopenharmony_ci> **NOTE** 2438e41f4b71Sopenharmony_ci> 2439e41f4b71Sopenharmony_ci> According to section 2.2 in RFC 5480:<br> 2440e41f4b71Sopenharmony_ci> 1. The uncompressed point data is represented as **0x04**\|x coordinate\|y coordinate. 2441e41f4b71Sopenharmony_ci> 2. The compressed point data in the **Fp** field (the **F2m** field is not supported currently) is represented as follows: **0x03**\|x coordinate (when the coordinate y is an odd number); **0x02**\|x coordinate (when the coordinate y is an even number). 2442e41f4b71Sopenharmony_ci 2443e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2444e41f4b71Sopenharmony_ci 2445e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2446e41f4b71Sopenharmony_ci 2447e41f4b71Sopenharmony_ci**Parameters** 2448e41f4b71Sopenharmony_ci 2449e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2450e41f4b71Sopenharmony_ci| ------------ | ---------- | ---- | ---------------------------------------------- | 2451e41f4b71Sopenharmony_ci| curveName | string | Yes | Elliptic curve name that is, the NID. | 2452e41f4b71Sopenharmony_ci| encodedPoint | Uint8Array | Yes | Data of the point on the ECC elliptic curve to convert. | 2453e41f4b71Sopenharmony_ci 2454e41f4b71Sopenharmony_ci**Return value** 2455e41f4b71Sopenharmony_ci 2456e41f4b71Sopenharmony_ci| Type | Description | 2457e41f4b71Sopenharmony_ci| ----------------- | ------------------- | 2458e41f4b71Sopenharmony_ci| [Point](#point10) | **Point** object obtained. | 2459e41f4b71Sopenharmony_ci 2460e41f4b71Sopenharmony_ci**Error codes** 2461e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2462e41f4b71Sopenharmony_ci 2463e41f4b71Sopenharmony_ci| ID | Error Message | 2464e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2465e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2466e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2467e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2468e41f4b71Sopenharmony_ci 2469e41f4b71Sopenharmony_ci**Example** 2470e41f4b71Sopenharmony_ci 2471e41f4b71Sopenharmony_ci```ts 2472e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 2473e41f4b71Sopenharmony_ci 2474e41f4b71Sopenharmony_ci// Randomly generated uncompressed point data. 2475e41f4b71Sopenharmony_cilet pkData = new Uint8Array([4, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178, 121, 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 157, 111, 40, 217, 215, 148, 120, 224, 205, 82, 83, 92, 185, 21, 211, 184, 5, 19, 114, 33, 86, 85, 228, 123, 242, 206, 200, 98, 178, 184, 130, 35, 232, 45, 5, 202, 189, 11, 46, 163, 156, 152]); 2476e41f4b71Sopenharmony_cilet returnPoint = cryptoFramework.ECCKeyUtil.convertPoint('NID_brainpoolP256r1', pkData); 2477e41f4b71Sopenharmony_ciconsole.info('returnPoint: ' + returnPoint.x.toString(16)); 2478e41f4b71Sopenharmony_ci``` 2479e41f4b71Sopenharmony_ci 2480e41f4b71Sopenharmony_ci### getEncodedPoint<sup>12+</sup> 2481e41f4b71Sopenharmony_ci 2482e41f4b71Sopenharmony_cistatic getEncodedPoint(curveName: string, point: Point, format: string): Uint8Array 2483e41f4b71Sopenharmony_ci 2484e41f4b71Sopenharmony_ciObtains the point data in the specified format from a **Point** object. Currently, compressed and uncompressed point data is supported. 2485e41f4b71Sopenharmony_ci 2486e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2487e41f4b71Sopenharmony_ci 2488e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2489e41f4b71Sopenharmony_ci 2490e41f4b71Sopenharmony_ci**Parameters** 2491e41f4b71Sopenharmony_ci 2492e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2493e41f4b71Sopenharmony_ci| ------------ | ----------------- | ---- | ---------------------------------------------- | 2494e41f4b71Sopenharmony_ci| curveName | string | Yes | Elliptic curve name that is, the NID. | 2495e41f4b71Sopenharmony_ci| point | [Point](#point10) | Yes | **Point** object of the elliptic curve. | 2496e41f4b71Sopenharmony_ci| format | string | Yes | Format of the point data to obtain. Currently, the value can be **COMPRESSED** or **UNCOMPRESSED** only. | 2497e41f4b71Sopenharmony_ci 2498e41f4b71Sopenharmony_ci**Return value** 2499e41f4b71Sopenharmony_ci 2500e41f4b71Sopenharmony_ci| Type | Description | 2501e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2502e41f4b71Sopenharmony_ci| Uint8Array | Point data in the specified format. | 2503e41f4b71Sopenharmony_ci 2504e41f4b71Sopenharmony_ci**Error codes** 2505e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2506e41f4b71Sopenharmony_ci 2507e41f4b71Sopenharmony_ci| ID | Error Message | 2508e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2509e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2510e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2511e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2512e41f4b71Sopenharmony_ci 2513e41f4b71Sopenharmony_ci**Example** 2514e41f4b71Sopenharmony_ci 2515e41f4b71Sopenharmony_ci```ts 2516e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 2517e41f4b71Sopenharmony_ci 2518e41f4b71Sopenharmony_ciasync function doTest() { 2519e41f4b71Sopenharmony_ci let generator = cryptoFramework.createAsyKeyGenerator('ECC_BrainPoolP256r1'); 2520e41f4b71Sopenharmony_ci let keyPair = await generator.generateKeyPair(); 2521e41f4b71Sopenharmony_ci let eccPkX = keyPair.pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_X_BN); 2522e41f4b71Sopenharmony_ci let eccPkY = keyPair.pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_Y_BN); 2523e41f4b71Sopenharmony_ci console.info('ECC_PK_X_BN 16: ' + eccPkX.toString(16)); 2524e41f4b71Sopenharmony_ci console.info('ECC_PK_Y_BN 16: ' + eccPkY.toString(16)); 2525e41f4b71Sopenharmony_ci // Place eccPkX.toString(16) in x and eccPkY.toString(16) in y. 2526e41f4b71Sopenharmony_ci let returnPoint: cryptoFramework.Point = { 2527e41f4b71Sopenharmony_ci x: BigInt('0x' + eccPkX.toString(16)), 2528e41f4b71Sopenharmony_ci y: BigInt('0x' + eccPkY.toString(16)) 2529e41f4b71Sopenharmony_ci }; 2530e41f4b71Sopenharmony_ci let returnData = cryptoFramework.ECCKeyUtil.getEncodedPoint('NID_brainpoolP256r1', returnPoint, 'UNCOMPRESSED'); 2531e41f4b71Sopenharmony_ci console.info('returnData: ' + returnData); 2532e41f4b71Sopenharmony_ci} 2533e41f4b71Sopenharmony_ci``` 2534e41f4b71Sopenharmony_ci 2535e41f4b71Sopenharmony_ci## DHKeyUtil<sup>11+</sup> 2536e41f4b71Sopenharmony_ci 2537e41f4b71Sopenharmony_ciProvides APIs for generating common parameters for a DH key based on the prime **p** length and the private key length. 2538e41f4b71Sopenharmony_ci 2539e41f4b71Sopenharmony_ci### genDHCommonParamsSpec<sup>11+</sup> 2540e41f4b71Sopenharmony_ci 2541e41f4b71Sopenharmony_cistatic genDHCommonParamsSpec(pLen: number, skLen?: number): DHCommonParamsSpec 2542e41f4b71Sopenharmony_ci 2543e41f4b71Sopenharmony_ciGenerates common parameters for a DH key based on the prime **p** length and the private key length. For details, see [DH](../../security/CryptoArchitectureKit/crypto-asym-key-generation-conversion-spec.md#dh). 2544e41f4b71Sopenharmony_ci 2545e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2546e41f4b71Sopenharmony_ci 2547e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2548e41f4b71Sopenharmony_ci 2549e41f4b71Sopenharmony_ci**Parameters** 2550e41f4b71Sopenharmony_ci 2551e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2552e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------ | 2553e41f4b71Sopenharmony_ci| pLen | number | Yes | Length of the prime **p**, in bits. | 2554e41f4b71Sopenharmony_ci| skLen | number | No | Length of the private key, in bits. | 2555e41f4b71Sopenharmony_ci 2556e41f4b71Sopenharmony_ci**Return value** 2557e41f4b71Sopenharmony_ci 2558e41f4b71Sopenharmony_ci| Type | Description | 2559e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2560e41f4b71Sopenharmony_ci| [DHCommonParamsSpec](#dhcommonparamsspec11) | DH common parameters generated. | 2561e41f4b71Sopenharmony_ci 2562e41f4b71Sopenharmony_ci**Error codes** 2563e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2564e41f4b71Sopenharmony_ci 2565e41f4b71Sopenharmony_ci| ID | Error Message | 2566e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 2567e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2568e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 2569e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2570e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2571e41f4b71Sopenharmony_ci 2572e41f4b71Sopenharmony_ci**Example** 2573e41f4b71Sopenharmony_ci 2574e41f4b71Sopenharmony_ci```ts 2575e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 2576e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2577e41f4b71Sopenharmony_citry { 2578e41f4b71Sopenharmony_ci let DHCommonParamsSpec = cryptoFramework.DHKeyUtil.genDHCommonParamsSpec(2048); 2579e41f4b71Sopenharmony_ci console.info('genDHCommonParamsSpec success'); 2580e41f4b71Sopenharmony_ci} catch (err) { 2581e41f4b71Sopenharmony_ci let e: BusinessError = err as BusinessError; 2582e41f4b71Sopenharmony_ci console.error(`genDHCommonParamsSpec error, ${e.code}, ${e.message}`); 2583e41f4b71Sopenharmony_ci} 2584e41f4b71Sopenharmony_ci``` 2585e41f4b71Sopenharmony_ci 2586e41f4b71Sopenharmony_ci## SM2CryptoUtil<sup>12+</sup> 2587e41f4b71Sopenharmony_ci 2588e41f4b71Sopenharmony_ciProvides APIs for SM2 cryptographic operations. 2589e41f4b71Sopenharmony_ci 2590e41f4b71Sopenharmony_ci### genCipherTextBySpec<sup>12+</sup> 2591e41f4b71Sopenharmony_ci 2592e41f4b71Sopenharmony_cistatic genCipherTextBySpec(spec: SM2CipherTextSpec, mode?: string): DataBlob 2593e41f4b71Sopenharmony_ci 2594e41f4b71Sopenharmony_ciGenerates SM2 ciphertext in ASN.1 format. 2595e41f4b71Sopenharmony_ci 2596e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2597e41f4b71Sopenharmony_ci 2598e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2599e41f4b71Sopenharmony_ci 2600e41f4b71Sopenharmony_ci**Parameters** 2601e41f4b71Sopenharmony_ci 2602e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2603e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------ | 2604e41f4b71Sopenharmony_ci| spec | [SM2CipherTextSpec](#sm2ciphertextspec12) | Yes | SM2 ciphertext parameters. | 2605e41f4b71Sopenharmony_ci| mode | string | No | Order of the SM2 parameters in the ciphertext. Currently, only C1C3C2 is supported. | 2606e41f4b71Sopenharmony_ci 2607e41f4b71Sopenharmony_ci**Return value** 2608e41f4b71Sopenharmony_ci 2609e41f4b71Sopenharmony_ci| Type | Description | 2610e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2611e41f4b71Sopenharmony_ci| [DataBlob](#datablob) | SM2 ciphertext in ASN.1 format. | 2612e41f4b71Sopenharmony_ci 2613e41f4b71Sopenharmony_ci**Error codes** 2614e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2615e41f4b71Sopenharmony_ci 2616e41f4b71Sopenharmony_ci| ID | Error Message | 2617e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 2618e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2619e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2620e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2621e41f4b71Sopenharmony_ci 2622e41f4b71Sopenharmony_ci**Example** 2623e41f4b71Sopenharmony_ci 2624e41f4b71Sopenharmony_ci```ts 2625e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 2626e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2627e41f4b71Sopenharmony_citry { 2628e41f4b71Sopenharmony_ci let spec : cryptoFramework.SM2CipherTextSpec = { 2629e41f4b71Sopenharmony_ci xCoordinate: BigInt('20625015362595980457695435345498579729138244358573902431560627260141789922999'), 2630e41f4b71Sopenharmony_ci yCoordinate: BigInt('48563164792857017065725892921053777369510340820930241057309844352421738767712'), 2631e41f4b71Sopenharmony_ci cipherTextData: new Uint8Array([100,227,78,195,249,179,43,70,242,69,169,10,65,123]), 2632e41f4b71Sopenharmony_ci hashData: new Uint8Array([87,167,167,247,88,146,203,234,83,126,117,129,52,142,82,54,152,226,201,111,143,115,169,125,128,42,157,31,114,198,109,244]), 2633e41f4b71Sopenharmony_ci } 2634e41f4b71Sopenharmony_ci let data = cryptoFramework.SM2CryptoUtil.genCipherTextBySpec(spec, 'C1C3C2'); 2635e41f4b71Sopenharmony_ci console.info('genCipherTextBySpec success'); 2636e41f4b71Sopenharmony_ci} catch (err) { 2637e41f4b71Sopenharmony_ci let e: BusinessError = err as BusinessError; 2638e41f4b71Sopenharmony_ci console.error(`genCipherTextBySpec error, ${e.code}, ${e.message}`); 2639e41f4b71Sopenharmony_ci} 2640e41f4b71Sopenharmony_ci``` 2641e41f4b71Sopenharmony_ci 2642e41f4b71Sopenharmony_ci### getCipherTextSpec<sup>12+</sup> 2643e41f4b71Sopenharmony_ci 2644e41f4b71Sopenharmony_cistatic getCipherTextSpec(cipherText: DataBlob, mode?: string): SM2CipherTextSpec 2645e41f4b71Sopenharmony_ci 2646e41f4b71Sopenharmony_ciObtains SM2 ciphertext parameters from the SM2 ciphertext in ASN.1 format. 2647e41f4b71Sopenharmony_ci 2648e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2649e41f4b71Sopenharmony_ci 2650e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2651e41f4b71Sopenharmony_ci 2652e41f4b71Sopenharmony_ci**Parameters** 2653e41f4b71Sopenharmony_ci 2654e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2655e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------ | 2656e41f4b71Sopenharmony_ci| cipherText | [DataBlob](#datablob) | Yes | SM2 ciphertext in ASN.1 format. 2657e41f4b71Sopenharmony_ci| mode | string | No | Order of the SM2 parameters in the ciphertext. Currently, only C1C3C2 is supported. | 2658e41f4b71Sopenharmony_ci 2659e41f4b71Sopenharmony_ci**Return value** 2660e41f4b71Sopenharmony_ci 2661e41f4b71Sopenharmony_ci| Type | Description | 2662e41f4b71Sopenharmony_ci| ----------------- | --------------------------------- | 2663e41f4b71Sopenharmony_ci| [SM2CipherTextSpec](#sm2ciphertextspec12) | SM2 ciphertext parameters obtained. | 2664e41f4b71Sopenharmony_ci 2665e41f4b71Sopenharmony_ci**Error codes** 2666e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2667e41f4b71Sopenharmony_ci 2668e41f4b71Sopenharmony_ci| ID | Error Message | 2669e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 2670e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2671e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2672e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2673e41f4b71Sopenharmony_ci 2674e41f4b71Sopenharmony_ci```ts 2675e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 2676e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2677e41f4b71Sopenharmony_citry { 2678e41f4b71Sopenharmony_ci let cipherTextArray = new Uint8Array([48,118,2,32,45,153,88,82,104,221,226,43,174,21,122,248,5,232,105,41,92,95,102,224,216,149,85,236,110,6,64,188,149,70,70,183,2,32,107,93,198,247,119,18,40,110,90,156,193,158,205,113,170,128,146,109,75,17,181,109,110,91,149,5,110,233,209,78,229,96,4,32,87,167,167,247,88,146,203,234,83,126,117,129,52,142,82,54,152,226,201,111,143,115,169,125,128,42,157,31,114,198,109,244,4,14,100,227,78,195,249,179,43,70,242,69,169,10,65,123]); 2679e41f4b71Sopenharmony_ci let cipherText : cryptoFramework.DataBlob = {data : cipherTextArray}; 2680e41f4b71Sopenharmony_ci let spec : cryptoFramework.SM2CipherTextSpec = cryptoFramework.SM2CryptoUtil.getCipherTextSpec(cipherText, 'C1C3C2'); 2681e41f4b71Sopenharmony_ci console.info('getCipherTextSpec success'); 2682e41f4b71Sopenharmony_ci} catch (err) { 2683e41f4b71Sopenharmony_ci let e: BusinessError = err as BusinessError; 2684e41f4b71Sopenharmony_ci console.error(`getCipherTextSpec error, ${e.code}, ${e.message}`); 2685e41f4b71Sopenharmony_ci} 2686e41f4b71Sopenharmony_ci``` 2687e41f4b71Sopenharmony_ci 2688e41f4b71Sopenharmony_ci## cryptoFramework.createCipher 2689e41f4b71Sopenharmony_ci 2690e41f4b71Sopenharmony_cicreateCipher(transformation: string): Cipher 2691e41f4b71Sopenharmony_ci 2692e41f4b71Sopenharmony_ciCreates a [Cipher](#cipher) instance based on the specified algorithm. 2693e41f4b71Sopenharmony_ci 2694e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Symmetric Key Encryption and Decryption Algorithm Specifications](../../security/CryptoArchitectureKit/crypto-sym-encrypt-decrypt-spec.md) and [Asymmetric Key Encryption and Decryption Algorithm Specifications](../../security/CryptoArchitectureKit/crypto-asym-encrypt-decrypt-spec.md). 2695e41f4b71Sopenharmony_ci 2696e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2697e41f4b71Sopenharmony_ci 2698e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2699e41f4b71Sopenharmony_ci 2700e41f4b71Sopenharmony_ci**Parameters** 2701e41f4b71Sopenharmony_ci 2702e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2703e41f4b71Sopenharmony_ci| -------------- | ------ | ---- | ------------------------------------------------------------ | 2704e41f4b71Sopenharmony_ci| transformation | string | Yes | Combination of the algorithm name (including the key length), encryption mode, and padding algorithm of the **Cipher** instance to create. | 2705e41f4b71Sopenharmony_ci 2706e41f4b71Sopenharmony_ci> **NOTE** 2707e41f4b71Sopenharmony_ci> 2708e41f4b71Sopenharmony_ci> 1. In symmetric encryption and decryption, the implementation of PKCS #5 is the same as that of PKCS #7. PKCS #5 and PKCS #7 use the same padding length and block length. That is, data is padded with 8 bytes in 3DES and 16 bytes in AES. **noPadding** indicates that no padding is performed.<br> 2709e41f4b71Sopenharmony_ci> You need to understand the differences between different block cipher modes and use the correct parameter specifications. For example, padding is required for ECB and CBC. Otherwise, ensure that the plaintext length is an integer multiple of the block size. No padding is recommended for other modes. In this case, the ciphertext length is the same as the plaintext length. 2710e41f4b71Sopenharmony_ci> 2. When RSA or SM2 is used for asymmetric encryption and decryption, create a **Cipher** instance for encryption and decryption respectively. Do not use the same **Cipher** instance for encryption and decryption. For symmetric encryption and decryption, one **cipher** object can be used to perform both encryption and decryption as long as the algorithm specifications are the same. 2711e41f4b71Sopenharmony_ci 2712e41f4b71Sopenharmony_ci**Return value** 2713e41f4b71Sopenharmony_ci 2714e41f4b71Sopenharmony_ci| Type | Description | 2715e41f4b71Sopenharmony_ci| ----------------- | ------------------------ | 2716e41f4b71Sopenharmony_ci| [Cipher](#cipher) | [Cipher](#cipher) instance created. | 2717e41f4b71Sopenharmony_ci 2718e41f4b71Sopenharmony_ci**Error codes** 2719e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2720e41f4b71Sopenharmony_ci 2721e41f4b71Sopenharmony_ci| ID | Error Message | 2722e41f4b71Sopenharmony_ci| -------- | ---------------------- | 2723e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2724e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 2725e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2726e41f4b71Sopenharmony_ci 2727e41f4b71Sopenharmony_ci**Example** 2728e41f4b71Sopenharmony_ci 2729e41f4b71Sopenharmony_ci```ts 2730e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 2731e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2732e41f4b71Sopenharmony_ci 2733e41f4b71Sopenharmony_cilet cipherAlgName = '3DES192|ECB|PKCS7'; 2734e41f4b71Sopenharmony_citry { 2735e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher(cipherAlgName); 2736e41f4b71Sopenharmony_ci console.info('cipher algName: ' + cipher.algName); 2737e41f4b71Sopenharmony_ci} catch (error) { 2738e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 2739e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 2740e41f4b71Sopenharmony_ci} 2741e41f4b71Sopenharmony_ci``` 2742e41f4b71Sopenharmony_ci 2743e41f4b71Sopenharmony_ci## Cipher 2744e41f4b71Sopenharmony_ci 2745e41f4b71Sopenharmony_ciProvides APIs for cipher operations. The [init()](#init-1), [update()](#update), and [doFinal()](#dofinal) APIs in this class are called in sequence to implement symmetric encryption or decryption and asymmetric encryption or decryption. 2746e41f4b71Sopenharmony_ci 2747e41f4b71Sopenharmony_ciFor details about the encryption and decryption process, see [Encryption and Decryption Overview](../../security/CryptoArchitectureKit/crypto-encryption-decryption-overview.md). 2748e41f4b71Sopenharmony_ci 2749e41f4b71Sopenharmony_ciA complete symmetric encryption/decryption process is slightly different from the asymmetric encryption/decryption process. 2750e41f4b71Sopenharmony_ci 2751e41f4b71Sopenharmony_ci- Symmetric encryption and decryption: **init()** and **doFinal()** are mandatory. **update()** is optional and can be called multiple times to encrypt or decrypt big data. After **doFinal()** is called to complete an encryption or decryption operation, **init()** can be called to start a new encryption or decryption operation. 2752e41f4b71Sopenharmony_ci- RSA or SM2 asymmetric encryption and decryption: **init()** and **doFinal()** are mandatory, and **update()** is not supported. **doFinal()** can be called multiple times to encrypt or decrypt big data. **init()** cannot be called repeatedly. If the encryption/decryption mode or padding mode is changed, a new **Cipher** object must be created. 2753e41f4b71Sopenharmony_ci 2754e41f4b71Sopenharmony_ci### Attributes 2755e41f4b71Sopenharmony_ci 2756e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2757e41f4b71Sopenharmony_ci 2758e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2759e41f4b71Sopenharmony_ci 2760e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 2761e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ---------------------------- | 2762e41f4b71Sopenharmony_ci| algName | string | Yes | No | Algorithm. | 2763e41f4b71Sopenharmony_ci 2764e41f4b71Sopenharmony_ci### init 2765e41f4b71Sopenharmony_ci 2766e41f4b71Sopenharmony_ciinit(opMode: CryptoMode, key: Key, params: ParamsSpec | null, callback: AsyncCallback\<void>): void 2767e41f4b71Sopenharmony_ci 2768e41f4b71Sopenharmony_ciInitializes a [cipher](#cipher) instance. This API uses an asynchronous callback to return the result. **init**, **update**, and **doFinal** must be used together. **init** and **doFinal** are mandatory, and **update** is optional. 2769e41f4b71Sopenharmony_ci 2770e41f4b71Sopenharmony_ciThis API can be used only after a [Cipher](#cipher) instance is created by using [createCipher](#cryptoframeworkcreatecipher). 2771e41f4b71Sopenharmony_ci 2772e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2773e41f4b71Sopenharmony_ci 2774e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2775e41f4b71Sopenharmony_ci 2776e41f4b71Sopenharmony_ci**Parameters** 2777e41f4b71Sopenharmony_ci 2778e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2779e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 2780e41f4b71Sopenharmony_ci| opMode | [CryptoMode](#cryptomode) | Yes | Operation (encryption or decryption) to perform. | 2781e41f4b71Sopenharmony_ci| key | [Key](#key) | Yes | Key for encryption or decryption. | 2782e41f4b71Sopenharmony_ci| params | [ParamsSpec](#paramsspec) \| null<sup>10+</sup> | Yes | Parameters for encryption or decryption. For algorithm modes without parameters (such as ECB), **null** can be passed in. In versions earlier than API version 10, only **ParamsSpec** is supported. Since API version 10, **null** is also supported. | 2783e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 2784e41f4b71Sopenharmony_ci 2785e41f4b71Sopenharmony_ci**Error codes** 2786e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2787e41f4b71Sopenharmony_ci 2788e41f4b71Sopenharmony_ci| ID | Error Message | 2789e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------------- | 2790e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2791e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2792e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 2793e41f4b71Sopenharmony_ci| 17630001 | crypto operation error.| 2794e41f4b71Sopenharmony_ci 2795e41f4b71Sopenharmony_ci### init 2796e41f4b71Sopenharmony_ci 2797e41f4b71Sopenharmony_ciinit(opMode: CryptoMode, key: Key, params: ParamsSpec | null): Promise\<void> 2798e41f4b71Sopenharmony_ci 2799e41f4b71Sopenharmony_ciInitializes a [cipher](#cipher) instance. This API uses a promise to return the result. **init**, **update**, and **doFinal** must be used together. **init** and **doFinal** are mandatory, and **update** is optional. 2800e41f4b71Sopenharmony_ci 2801e41f4b71Sopenharmony_ciThis API can be used only after a [Cipher](#cipher) instance is created by using [createCipher](#cryptoframeworkcreatecipher). 2802e41f4b71Sopenharmony_ci 2803e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2804e41f4b71Sopenharmony_ci 2805e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2806e41f4b71Sopenharmony_ci 2807e41f4b71Sopenharmony_ci**Parameters** 2808e41f4b71Sopenharmony_ci 2809e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2810e41f4b71Sopenharmony_ci| ------ | ------------------------- | ---- | ------------------------------------------------------------ | 2811e41f4b71Sopenharmony_ci| opMode | [CryptoMode](#cryptomode) | Yes | Operation (encryption or decryption) to perform. | 2812e41f4b71Sopenharmony_ci| key | [Key](#key) | Yes | Key for encryption or decryption. | 2813e41f4b71Sopenharmony_ci| params | [ParamsSpec](#paramsspec) \| null<sup>10+</sup> | Yes | Parameters for encryption or decryption. For algorithm modes without parameters (such as ECB), **null** can be passed in. In versions earlier than API version 10, only **ParamsSpec** is supported. Since API version 10, **null** is also supported. | 2814e41f4b71Sopenharmony_ci 2815e41f4b71Sopenharmony_ci**Return value** 2816e41f4b71Sopenharmony_ci 2817e41f4b71Sopenharmony_ci| Type | Description | 2818e41f4b71Sopenharmony_ci| -------------- | -------------------------------------- | 2819e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value. | 2820e41f4b71Sopenharmony_ci 2821e41f4b71Sopenharmony_ci**Error codes** 2822e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2823e41f4b71Sopenharmony_ci 2824e41f4b71Sopenharmony_ci| ID | Error Message | 2825e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 2826e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2827e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2828e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 2829e41f4b71Sopenharmony_ci| 17630001 | crypto operation error.| 2830e41f4b71Sopenharmony_ci 2831e41f4b71Sopenharmony_ci### initSync<sup>12+</sup> 2832e41f4b71Sopenharmony_ci 2833e41f4b71Sopenharmony_ciinitSync(opMode: CryptoMode, key: Key, params: ParamsSpec | null): void 2834e41f4b71Sopenharmony_ci 2835e41f4b71Sopenharmony_ciInitializes a [cipher](#cipher) instance. This API returns the result synchronously. **initSync**, **updateSync**, and **doFinalSync** must be used together. **initSync** and **doFinalSync** are mandatory, and **updateSync** is optional. 2836e41f4b71Sopenharmony_ci 2837e41f4b71Sopenharmony_ciThis API can be used only after a [Cipher](#cipher) instance is created by using [createCipher](#cryptoframeworkcreatecipher). 2838e41f4b71Sopenharmony_ci 2839e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2840e41f4b71Sopenharmony_ci 2841e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2842e41f4b71Sopenharmony_ci 2843e41f4b71Sopenharmony_ci**Parameters** 2844e41f4b71Sopenharmony_ci 2845e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2846e41f4b71Sopenharmony_ci| ------ | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 2847e41f4b71Sopenharmony_ci| opMode | [CryptoMode](#cryptomode) | Yes | Operation (encryption or decryption) to perform. | 2848e41f4b71Sopenharmony_ci| key | [Key](#key) | Yes | Key for encryption or decryption. | 2849e41f4b71Sopenharmony_ci| params | [ParamsSpec](#paramsspec) | Yes | Parameters for encryption or decryption. For algorithm modes without parameters (such as ECB), **null** can be passed in. | 2850e41f4b71Sopenharmony_ci 2851e41f4b71Sopenharmony_ci**Error codes** 2852e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2853e41f4b71Sopenharmony_ci 2854e41f4b71Sopenharmony_ci| ID | Error Message | 2855e41f4b71Sopenharmony_ci| -------- | ----------------------- | 2856e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2857e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2858e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 2859e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2860e41f4b71Sopenharmony_ci 2861e41f4b71Sopenharmony_ci### update 2862e41f4b71Sopenharmony_ci 2863e41f4b71Sopenharmony_ciupdate(data: DataBlob, callback: AsyncCallback\<DataBlob>): void 2864e41f4b71Sopenharmony_ci 2865e41f4b71Sopenharmony_ciUpdates the data to encrypt or decrypt by segment. This API uses an asynchronous callback to return the encrypted or decrypted data. 2866e41f4b71Sopenharmony_ci 2867e41f4b71Sopenharmony_ciThis API can be called only after the [Cipher](#cipher) instance is initialized by using [init()](#init-1). 2868e41f4b71Sopenharmony_ci 2869e41f4b71Sopenharmony_ci> **NOTE** 2870e41f4b71Sopenharmony_ci> 2871e41f4b71Sopenharmony_ci> 1. The **update()** and **doFinal()** operation results vary with the block mode used. If you are not familiar with the block modes for symmetric encryption and decryption, add a judgment to determine whether the result of each **update()** and **doFinal()** is null. If the result is not null, obtain and combine the data segments into complete ciphertext or plaintext. <br>For example, in ECB or CBC mode, data is encrypted or decrypted by block no matter whether the data passed in by **update()** is an integer multiple of the block length, and the data generated by this **update()** is output.<br>That is, data is returned as long as the data passed in by **update()** reaches the size of a block. Otherwise, **null** is returned and the data will be retained until a block is formed in the next **update()**/**doFinal()**.<br>When **doFinal()** is called, the data that has not been encrypted or decrypted will be padded based on the padding mode set in [createCipher](#cryptoframeworkcreatecipher) to an integer multiple of the block length, and then encrypted or decrypted.<br>For a mode in which a block cipher can be converted into a stream cipher, the length of the ciphertext may be the same as that of the plaintext. 2872e41f4b71Sopenharmony_ci> 2. You can use **update()** multiple times or do not use it (use **doFinal()** after **init()**), depending on the data volume.<br> 2873e41f4b71Sopenharmony_ci> The amount of the data to be passed in by **update** (one-time or accumulative) is not limited. If there is a large amount of data, you are advised to call **update()** multiple times to pass in the data by segment.<br> 2874e41f4b71Sopenharmony_ci> For details about the sample code for calling **update** multiple times, see [Encryption and Decryption by Segment with an AES Symmetric Key (GCM Mode)](../../security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-gcm-by-segment.md). 2875e41f4b71Sopenharmony_ci> 3. RSA or SM2 asymmetric encryption and decryption do not support **update()**. 2876e41f4b71Sopenharmony_ci> 4. If CCM is used in symmetric encryption or decryption, **update()** can be called only once. In the encryption process, you can either use **update()** to encrypt data and use **doFinal()** to obtain **authTag** or use **doFinal()** without using **update()**. In the decryption process, you can either use **update()** once or use **doFinal()** to decrypt data and verify the tag. 2877e41f4b71Sopenharmony_ci 2878e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2879e41f4b71Sopenharmony_ci 2880e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2881e41f4b71Sopenharmony_ci 2882e41f4b71Sopenharmony_ci**Parameters** 2883e41f4b71Sopenharmony_ci 2884e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2885e41f4b71Sopenharmony_ci| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 2886e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to encrypt or decrypt. It cannot be null. | 2887e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[DataBlob](#datablob)> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**, and **data** is **DataBlob** (containing the encrypted or decrypted data). Otherwise, **err** is an error object. | 2888e41f4b71Sopenharmony_ci 2889e41f4b71Sopenharmony_ci**Error codes** 2890e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2891e41f4b71Sopenharmony_ci 2892e41f4b71Sopenharmony_ci| ID | Error Message | 2893e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- | 2894e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2895e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2896e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 2897e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2898e41f4b71Sopenharmony_ci 2899e41f4b71Sopenharmony_ci### update 2900e41f4b71Sopenharmony_ci 2901e41f4b71Sopenharmony_ciupdate(data: DataBlob): Promise\<DataBlob> 2902e41f4b71Sopenharmony_ci 2903e41f4b71Sopenharmony_ciUpdates the data to encrypt or decrypt by segment. This API uses a promise to return the encrypted or decrypted data. 2904e41f4b71Sopenharmony_ci 2905e41f4b71Sopenharmony_ciThis API can be called only after the [Cipher](#cipher) instance is initialized by using [init()](#init-2). 2906e41f4b71Sopenharmony_ci 2907e41f4b71Sopenharmony_ci> **NOTE** 2908e41f4b71Sopenharmony_ci> 2909e41f4b71Sopenharmony_ci> 1. The **update()** and **doFinal()** operation results vary with the block mode used. If you are not familiar with the block modes for symmetric encryption and decryption, add a judgment to determine whether the result of each **update()** and **doFinal()** is null. If the result is not null, obtain and combine the data segments into complete ciphertext or plaintext. 2910e41f4b71Sopenharmony_ci> <br>For example, in ECB or CBC mode, data is encrypted or decrypted by block no matter whether the data passed in by **update()** is an integer multiple of the block length, and the data generated by this **update()** is output.<br>That is, data is returned as long as the data passed in by **update()** reaches the size of a block. Otherwise, **null** is returned and the data will be retained until a block is formed in the next **update()**/**doFinal()**.<br>When **doFinal()** is called, the data that has not been encrypted or decrypted will be padded based on the padding mode set in [createCipher](#cryptoframeworkcreatecipher) to an integer multiple of the block length, and then encrypted or decrypted.<br>For a mode in which a block cipher can be converted into a stream cipher, the length of the ciphertext may be the same as that of the plaintext. 2911e41f4b71Sopenharmony_ci> 2. You can use **update()** multiple times or do not use it (use **doFinal()** after **init()**), depending on the data volume.<br> 2912e41f4b71Sopenharmony_ci> The amount of the data to be passed in by **update()** (one-time or accumulative) is not limited. If there is a large amount of data, you are advised to call **update()** multiple times to pass in the data by segment.<br> 2913e41f4b71Sopenharmony_ci> For details about the sample code for calling **update()** multiple times, see [Encryption and Decryption by Segment with an AES Symmetric Key (GCM Mode)](../../security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-gcm-by-segment.md). 2914e41f4b71Sopenharmony_ci> 3. RSA or SM2 asymmetric encryption and decryption do not support **update()**. 2915e41f4b71Sopenharmony_ci> 4. If CCM is used in symmetric encryption or decryption, **update()** can be called only once. In the encryption process, you can either use **update()** to encrypt data and use **doFinal()** to obtain **authTag** or use **doFinal()** without using **update()**. In the decryption process, you can either use **update()** once or use **doFinal()** to decrypt data and verify the tag. 2916e41f4b71Sopenharmony_ci 2917e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2918e41f4b71Sopenharmony_ci 2919e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2920e41f4b71Sopenharmony_ci 2921e41f4b71Sopenharmony_ci**Parameters** 2922e41f4b71Sopenharmony_ci 2923e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2924e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | -------------------- | 2925e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to encrypt or decrypt. It cannot be null. | 2926e41f4b71Sopenharmony_ci 2927e41f4b71Sopenharmony_ci**Return value** 2928e41f4b71Sopenharmony_ci 2929e41f4b71Sopenharmony_ci| Type | Description | 2930e41f4b71Sopenharmony_ci| ------------------------------- | ------------------------------------------------ | 2931e41f4b71Sopenharmony_ci| Promise\<[DataBlob](#datablob)> | Promise used to return the **DataBlob** (containing the encrypted or decrypted data). | 2932e41f4b71Sopenharmony_ci 2933e41f4b71Sopenharmony_ci**Error codes** 2934e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2935e41f4b71Sopenharmony_ci 2936e41f4b71Sopenharmony_ci| ID | Error Message | 2937e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 2938e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2939e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2940e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 2941e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2942e41f4b71Sopenharmony_ci 2943e41f4b71Sopenharmony_ci### updateSync<sup>12+</sup> 2944e41f4b71Sopenharmony_ci 2945e41f4b71Sopenharmony_ciupdateSync(data: DataBlob): DataBlob 2946e41f4b71Sopenharmony_ci 2947e41f4b71Sopenharmony_ciUpdates the data to encrypt or decrypt by segment. This API returns the encrypted or decrypted data synchronously. 2948e41f4b71Sopenharmony_ci 2949e41f4b71Sopenharmony_ciThis API can be called only after the [Cipher](#cipher) instance is initialized by using [initSync()](#initsync12). 2950e41f4b71Sopenharmony_ci 2951e41f4b71Sopenharmony_ciSee **NOTE** in **update()** for other precautions. 2952e41f4b71Sopenharmony_ci 2953e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2954e41f4b71Sopenharmony_ci 2955e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2956e41f4b71Sopenharmony_ci 2957e41f4b71Sopenharmony_ci**Parameters** 2958e41f4b71Sopenharmony_ci 2959e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2960e41f4b71Sopenharmony_ci| ------ | --------------------- | ---- | ------------------------------------------------------------ | 2961e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to encrypt or decrypt. It cannot be null. | 2962e41f4b71Sopenharmony_ci 2963e41f4b71Sopenharmony_ci**Error codes** 2964e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 2965e41f4b71Sopenharmony_ci 2966e41f4b71Sopenharmony_ci| ID | Error Message | 2967e41f4b71Sopenharmony_ci| -------- | ----------------------- | 2968e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 2969e41f4b71Sopenharmony_ci| 17620001 | memory error. | 2970e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 2971e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 2972e41f4b71Sopenharmony_ci 2973e41f4b71Sopenharmony_ci### doFinal 2974e41f4b71Sopenharmony_ci 2975e41f4b71Sopenharmony_cidoFinal(data: DataBlob | null, callback: AsyncCallback\<DataBlob>): void 2976e41f4b71Sopenharmony_ci 2977e41f4b71Sopenharmony_ci (1) Encrypts or decrypts the remaining data (generated by the block cipher mode) and the data passed in by **doFinal()** to finalize the symmetric encryption or decryption. This API uses an asynchronous callback to return the encrypted or decrypted data.<br>If a small amount of data needs to be encrypted or decrypted, you can use **doFinal()** to pass in data without using **update()**. If all the data has been passed in by [update()](#update-4), you can pass in **null** in **data** of **doFinal()**.<br>The output of **doFinal()** varies with the symmetric encryption/decryption mode in use. 2978e41f4b71Sopenharmony_ci 2979e41f4b71Sopenharmony_ci- Symmetric encryption in GCM and CCM mode: The result consists of the ciphertext and **authTag** (the last 16 bytes for GCM and the last 12 bytes for CCM). If **null** is passed in by **data** of **doFinal()**, the result of **doFinal()** is **authTag**. **authTag** must be [GcmParamsSpec](#gcmparamsspec) or [CcmParamsSpec](#ccmparamsspec) used for decryption. The ciphertext is the **data** passed in for decryption. 2980e41f4b71Sopenharmony_ci- Symmetric encryption and decryption in other modes and symmetric decryption in GCM and CCM modes: The result is the complete plaintext/ciphertext. 2981e41f4b71Sopenharmony_ci 2982e41f4b71Sopenharmony_ci (2) Encrypts or decrypts the input data for RSA or SM2 asymmetric encryption/decryption. This API uses an asynchronous callback to return the result. If a large amount of data needs to be encrypted/decrypted, call **doFinal()** multiple times and concatenate the result of each **doFinal()** to obtain the complete plaintext/ciphertext. 2983e41f4b71Sopenharmony_ci 2984e41f4b71Sopenharmony_ci> **NOTE** 2985e41f4b71Sopenharmony_ci> 2986e41f4b71Sopenharmony_ci> 1. In symmetric encryption and decryption, after **doFinal** is called, the encryption and decryption process is complete and the [Cipher](#cipher) instance is cleared. When a new encryption and decryption process is started, **init()** must be called with a complete parameter list for initialization.<br>Even if the same symmetric key is used to encrypt and decrypt the same **Cipher** instance, the **params** parameter must be set when **init** is called during decryption. 2987e41f4b71Sopenharmony_ci> 2. If a decryption fails, check whether the data to be encrypted and decrypted matches the parameters in **init()**. For the GCM mode, check whether the **authTag** obtained after encryption is obtained from the **GcmParamsSpec** for decryption. 2988e41f4b71Sopenharmony_ci> 3. The result of **doFinal()** may be **null**. To avoid exceptions, determine whether the result is **null** before using the **.data** field to access the **doFinal()** result. 2989e41f4b71Sopenharmony_ci> 4. For details about the sample code for calling **doFinal** multiple times in asymmetric encryption and decryption, see [Encryption and Decryption by Segment with an RSA Asymmetric Key Pair](../../security/CryptoArchitectureKit/crypto-rsa-asym-encrypt-decrypt-by-segment.md). The operations are similar for SM2 and RSA. 2990e41f4b71Sopenharmony_ci 2991e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2992e41f4b71Sopenharmony_ci 2993e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 2994e41f4b71Sopenharmony_ci 2995e41f4b71Sopenharmony_ci**Parameters** 2996e41f4b71Sopenharmony_ci 2997e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2998e41f4b71Sopenharmony_ci| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | 2999e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Data to encrypt or decrypt. It can be **null** in symmetric encryption or decryption, but cannot be {data:Uint8Array(empty)}. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 3000e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[DataBlob](#datablob)> | Yes | Callback invoked to return the result. If the data is successfully encrypted or decrypted, **err** is **undefined**, and **data** is the **DataBlob** (encryption or decryption result of the remaining data). Otherwise, **err** is an error object. | 3001e41f4b71Sopenharmony_ci 3002e41f4b71Sopenharmony_ci**Error codes** 3003e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3004e41f4b71Sopenharmony_ci 3005e41f4b71Sopenharmony_ci| ID | Error Message | 3006e41f4b71Sopenharmony_ci| -------- | ----------------------- | 3007e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3008e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3009e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3010e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3011e41f4b71Sopenharmony_ci 3012e41f4b71Sopenharmony_ci**Encryption with AES GCM (example)** 3013e41f4b71Sopenharmony_ci 3014e41f4b71Sopenharmony_ciFor more encryption and decryption examples, see [Encryption and Decryption with an AES Symmetric Key (GCM Mode)](../../security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-gcm.md). 3015e41f4b71Sopenharmony_ci 3016e41f4b71Sopenharmony_ci```ts 3017e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 3018e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 3019e41f4b71Sopenharmony_ci 3020e41f4b71Sopenharmony_cifunction genGcmParamsSpec() { 3021e41f4b71Sopenharmony_ci let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 3022e41f4b71Sopenharmony_ci let dataIv = new Uint8Array(arr); 3023e41f4b71Sopenharmony_ci let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 3024e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0]; 3025e41f4b71Sopenharmony_ci let dataAad = new Uint8Array(arr); 3026e41f4b71Sopenharmony_ci let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; 3027e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 3028e41f4b71Sopenharmony_ci let dataTag = new Uint8Array(arr); 3029e41f4b71Sopenharmony_ci let tagBlob: cryptoFramework.DataBlob = { 3030e41f4b71Sopenharmony_ci data: dataTag 3031e41f4b71Sopenharmony_ci }; 3032e41f4b71Sopenharmony_ci let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { 3033e41f4b71Sopenharmony_ci iv: ivBlob, 3034e41f4b71Sopenharmony_ci aad: aadBlob, 3035e41f4b71Sopenharmony_ci authTag: tagBlob, 3036e41f4b71Sopenharmony_ci algName: "GcmParamsSpec" 3037e41f4b71Sopenharmony_ci }; 3038e41f4b71Sopenharmony_ci return gcmParamsSpec; 3039e41f4b71Sopenharmony_ci} 3040e41f4b71Sopenharmony_ci 3041e41f4b71Sopenharmony_cifunction cipherByCallback() { 3042e41f4b71Sopenharmony_ci let gcmParams = genGcmParamsSpec(); 3043e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 3044e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 3045e41f4b71Sopenharmony_ci symKeyGenerator.generateSymKey((err, symKey) => { 3046e41f4b71Sopenharmony_ci cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams, (err,) => { 3047e41f4b71Sopenharmony_ci let message = "This is a test"; 3048e41f4b71Sopenharmony_ci let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 3049e41f4b71Sopenharmony_ci cipher.update(plainText, (err, encryptUpdate) => { 3050e41f4b71Sopenharmony_ci cipher.doFinal(null, (err, tag) => { 3051e41f4b71Sopenharmony_ci gcmParams.authTag = tag; 3052e41f4b71Sopenharmony_ci console.info('encryptUpdate plainText: ' + encryptUpdate.data); 3053e41f4b71Sopenharmony_ci }); 3054e41f4b71Sopenharmony_ci }); 3055e41f4b71Sopenharmony_ci }); 3056e41f4b71Sopenharmony_ci }); 3057e41f4b71Sopenharmony_ci} 3058e41f4b71Sopenharmony_ci``` 3059e41f4b71Sopenharmony_ci 3060e41f4b71Sopenharmony_ci### doFinal 3061e41f4b71Sopenharmony_ci 3062e41f4b71Sopenharmony_cidoFinal(data: DataBlob | null): Promise\<DataBlob> 3063e41f4b71Sopenharmony_ci 3064e41f4b71Sopenharmony_ci (1) Encrypts or decrypts the remaining data (generated by the block cipher mode) and the data passed in by **doFinal()** to finalize the symmetric encryption or decryption. This API uses a promise to return the encrypted or decrypted data.<br>If a small amount of data needs to be encrypted or decrypted, you can use **doFinal()** to pass in data without using **update()**. If all the data has been passed in by **update()**, you can pass in **null** in **data** of **doFinal()**.<br>The output of **doFinal()** varies with the symmetric encryption/decryption mode in use. 3065e41f4b71Sopenharmony_ci 3066e41f4b71Sopenharmony_ci- Symmetric encryption in GCM and CCM mode: The result consists of the ciphertext and **authTag** (the last 16 bytes for GCM and the last 12 bytes for CCM). If **data** in **doFinal** is null, the result of **doFinal** is **authTag**.<br>Set **authTag** to [GcmParamsSpec](#gcmparamsspec) or [CcmParamsSpec](#ccmparamsspec) for decryption. The ciphertext is used as the input parameter **data** for decryption. 3067e41f4b71Sopenharmony_ci- Symmetric encryption and decryption in other modes and symmetric decryption in GCM and CCM modes: The result is the complete plaintext/ciphertext. 3068e41f4b71Sopenharmony_ci 3069e41f4b71Sopenharmony_ci (2) Encrypts or decrypts the input data for RSA or SM2 asymmetric encryption/decryption. This API uses a promise to return the result. If a large amount of data needs to be encrypted/decrypted, call **doFinal()** multiple times and concatenate the result of each **doFinal()** to obtain the complete plaintext/ciphertext. 3070e41f4b71Sopenharmony_ci 3071e41f4b71Sopenharmony_ci> **NOTE** 3072e41f4b71Sopenharmony_ci> 3073e41f4b71Sopenharmony_ci> 1. In symmetric encryption and decryption, after **doFinal** is called, the encryption and decryption process is complete and the [Cipher](#cipher) instance is cleared. When a new encryption and decryption process is started, **init()** must be called with a complete parameter list for initialization.<br>Even if the same symmetric key is used to encrypt and decrypt the same **Cipher** instance, the **params** parameter must be set when **init** is called during decryption. 3074e41f4b71Sopenharmony_ci> 2. If a decryption fails, check whether the data to be encrypted and decrypted matches the parameters in **init()**. For the GCM mode, check whether the **authTag** obtained after encryption is obtained from the **GcmParamsSpec** for decryption. 3075e41f4b71Sopenharmony_ci> 3. The result of **doFinal()** may be **null**. To avoid exceptions, determine whether the result is **null** before using the **.data** field to access the **doFinal()** result. 3076e41f4b71Sopenharmony_ci> 4. For details about the sample code for calling **doFinal** multiple times in asymmetric encryption and decryption, see [Encryption and Decryption by Segment with an RSA Asymmetric Key Pair](../../security/CryptoArchitectureKit/crypto-rsa-asym-encrypt-decrypt-by-segment.md). The operations are similar for SM2 and RSA. 3077e41f4b71Sopenharmony_ci 3078e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3079e41f4b71Sopenharmony_ci 3080e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3081e41f4b71Sopenharmony_ci 3082e41f4b71Sopenharmony_ci**Parameters** 3083e41f4b71Sopenharmony_ci 3084e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3085e41f4b71Sopenharmony_ci| ---- | --------------------- | ---- | -------------------- | 3086e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Data to encrypt or decrypt. It can be **null**, but cannot be {data:Uint8Array(empty)}. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 3087e41f4b71Sopenharmony_ci 3088e41f4b71Sopenharmony_ci**Return value** 3089e41f4b71Sopenharmony_ci 3090e41f4b71Sopenharmony_ci| Type | Description | 3091e41f4b71Sopenharmony_ci| ------------------------------- | ------------------------------------------------ | 3092e41f4b71Sopenharmony_ci| Promise\<[DataBlob](#datablob)> | Promise used to return the **DataBlob**, which is the encryption or decryption result of the remaining data. | 3093e41f4b71Sopenharmony_ci 3094e41f4b71Sopenharmony_ci**Error codes** 3095e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3096e41f4b71Sopenharmony_ci 3097e41f4b71Sopenharmony_ci| ID | Error Message | 3098e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 3099e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3100e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3101e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3102e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3103e41f4b71Sopenharmony_ci 3104e41f4b71Sopenharmony_ci**Encryption with AES GCM (example)** 3105e41f4b71Sopenharmony_ci 3106e41f4b71Sopenharmony_ciFor more encryption and decryption examples, see [Encryption and Decryption with an AES Symmetric Key (GCM Mode)](../../security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-gcm.md). 3107e41f4b71Sopenharmony_ci 3108e41f4b71Sopenharmony_ci```ts 3109e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 3110e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 3111e41f4b71Sopenharmony_ci 3112e41f4b71Sopenharmony_cifunction genGcmParamsSpec() { 3113e41f4b71Sopenharmony_ci let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 3114e41f4b71Sopenharmony_ci let dataIv = new Uint8Array(arr); 3115e41f4b71Sopenharmony_ci let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 3116e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0]; 3117e41f4b71Sopenharmony_ci let dataAad = new Uint8Array(arr); 3118e41f4b71Sopenharmony_ci let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; 3119e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 3120e41f4b71Sopenharmony_ci let dataTag = new Uint8Array(arr); 3121e41f4b71Sopenharmony_ci let tagBlob: cryptoFramework.DataBlob = { 3122e41f4b71Sopenharmony_ci data: dataTag 3123e41f4b71Sopenharmony_ci }; 3124e41f4b71Sopenharmony_ci let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { 3125e41f4b71Sopenharmony_ci iv: ivBlob, 3126e41f4b71Sopenharmony_ci aad: aadBlob, 3127e41f4b71Sopenharmony_ci authTag: tagBlob, 3128e41f4b71Sopenharmony_ci algName: "GcmParamsSpec" 3129e41f4b71Sopenharmony_ci }; 3130e41f4b71Sopenharmony_ci return gcmParamsSpec; 3131e41f4b71Sopenharmony_ci} 3132e41f4b71Sopenharmony_ci 3133e41f4b71Sopenharmony_ciasync function cipherByPromise() { 3134e41f4b71Sopenharmony_ci let gcmParams = genGcmParamsSpec(); 3135e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 3136e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 3137e41f4b71Sopenharmony_ci let symKey = await symKeyGenerator.generateSymKey(); 3138e41f4b71Sopenharmony_ci await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); 3139e41f4b71Sopenharmony_ci let message = "This is a test"; 3140e41f4b71Sopenharmony_ci let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 3141e41f4b71Sopenharmony_ci let encryptUpdate = await cipher.update(plainText); 3142e41f4b71Sopenharmony_ci gcmParams.authTag = await cipher.doFinal(null); 3143e41f4b71Sopenharmony_ci console.info('encryptUpdate plainText: ' + encryptUpdate.data); 3144e41f4b71Sopenharmony_ci} 3145e41f4b71Sopenharmony_ci``` 3146e41f4b71Sopenharmony_ci 3147e41f4b71Sopenharmony_ci### doFinalSync<sup>12+</sup> 3148e41f4b71Sopenharmony_ci 3149e41f4b71Sopenharmony_cidoFinalSync(data: DataBlob | null): DataBlob 3150e41f4b71Sopenharmony_ci 3151e41f4b71Sopenharmony_ci (1) Encrypts or decrypts the remaining data (generated by the block cipher mode) and the data passed in by **doFinalSync()** to finalize the symmetric encryption or decryption. This API returns the result synchronously.<br>If the data volume is small, you can pass in all the data in **doFinalSync** without using **updateSync**. If data has been passed using [updateSync](#updatesync12), you can pass in **null** in **doFinalSync**.<br>The output of **doFinalSync** varies with the symmetric cipher mode in use. 3152e41f4b71Sopenharmony_ci 3153e41f4b71Sopenharmony_ci- Symmetric encryption in GCM and CCM mode: The result consists of the ciphertext and **authTag** (the last 16 bytes for GCM and the last 12 bytes for CCM). If **data** in **doFinalSync** is **null**, the result of **doFinalSync** is **authTag**.<br>Set **authTag** to [GcmParamsSpec](#gcmparamsspec) or [CcmParamsSpec](#ccmparamsspec) for decryption. The ciphertext is used as the input parameter **data** for decryption. 3154e41f4b71Sopenharmony_ci- Symmetric encryption and decryption in other modes and symmetric decryption in GCM and CCM modes: The result is the complete plaintext/ciphertext, obtained by concatenating the output of each **updateSync** and **doFinalSync**. 3155e41f4b71Sopenharmony_ci 3156e41f4b71Sopenharmony_ci (2) Encrypts or decrypts the input data for RSA or SM2 asymmetric encryption/decryption. This API returns the result synchronously. If a large amount of data needs to be encrypted/decrypted, call **doFinalSync** multiple times and concatenate the result of each **doFinalSync** to obtain the complete plaintext/ciphertext. 3157e41f4b71Sopenharmony_ci 3158e41f4b71Sopenharmony_ciSee **NOTE** in [doFinal()](#dofinal) for other precautions. 3159e41f4b71Sopenharmony_ci 3160e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3161e41f4b71Sopenharmony_ci 3162e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3163e41f4b71Sopenharmony_ci 3164e41f4b71Sopenharmony_ci**Parameters** 3165e41f4b71Sopenharmony_ci 3166e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3167e41f4b71Sopenharmony_ci| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ | 3168e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to encrypt or decrypt. It can be **null** in symmetric encryption or decryption, but cannot be {data:Uint8Array(empty)}. | 3169e41f4b71Sopenharmony_ci 3170e41f4b71Sopenharmony_ci**Error codes** 3171e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3172e41f4b71Sopenharmony_ci 3173e41f4b71Sopenharmony_ci| ID | Error Message | 3174e41f4b71Sopenharmony_ci| -------- | ----------------------- | 3175e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3176e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3177e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3178e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3179e41f4b71Sopenharmony_ci 3180e41f4b71Sopenharmony_ci**Encryption with AES GCM (example)** 3181e41f4b71Sopenharmony_ci 3182e41f4b71Sopenharmony_ciFor more encryption and decryption examples, see [Encryption and Decryption with an AES Symmetric Key (GCM Mode)](../../security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-gcm.md). 3183e41f4b71Sopenharmony_ci 3184e41f4b71Sopenharmony_ci```ts 3185e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 3186e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 3187e41f4b71Sopenharmony_ci 3188e41f4b71Sopenharmony_cifunction genGcmParamsSpec() { 3189e41f4b71Sopenharmony_ci let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 3190e41f4b71Sopenharmony_ci let dataIv = new Uint8Array(arr); 3191e41f4b71Sopenharmony_ci let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 3192e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0]; 3193e41f4b71Sopenharmony_ci let dataAad = new Uint8Array(arr); 3194e41f4b71Sopenharmony_ci let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; 3195e41f4b71Sopenharmony_ci arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 3196e41f4b71Sopenharmony_ci let dataTag = new Uint8Array(arr); 3197e41f4b71Sopenharmony_ci let tagBlob: cryptoFramework.DataBlob = { 3198e41f4b71Sopenharmony_ci data: dataTag 3199e41f4b71Sopenharmony_ci }; 3200e41f4b71Sopenharmony_ci let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { 3201e41f4b71Sopenharmony_ci iv: ivBlob, 3202e41f4b71Sopenharmony_ci aad: aadBlob, 3203e41f4b71Sopenharmony_ci authTag: tagBlob, 3204e41f4b71Sopenharmony_ci algName: "GcmParamsSpec" 3205e41f4b71Sopenharmony_ci }; 3206e41f4b71Sopenharmony_ci return gcmParamsSpec; 3207e41f4b71Sopenharmony_ci} 3208e41f4b71Sopenharmony_ci 3209e41f4b71Sopenharmony_ciasync function cipherBySync() { 3210e41f4b71Sopenharmony_ci let gcmParams = genGcmParamsSpec(); 3211e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 3212e41f4b71Sopenharmony_ci let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 3213e41f4b71Sopenharmony_ci let symKey = await symKeyGenerator.generateSymKey(); 3214e41f4b71Sopenharmony_ci await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); 3215e41f4b71Sopenharmony_ci let message = "This is a test"; 3216e41f4b71Sopenharmony_ci let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 3217e41f4b71Sopenharmony_ci let encryptUpdate = cipher.updateSync(plainText); 3218e41f4b71Sopenharmony_ci gcmParams.authTag = cipher.doFinalSync(null); 3219e41f4b71Sopenharmony_ci console.info('encryptUpdate plainText: ' + encryptUpdate.data); 3220e41f4b71Sopenharmony_ci} 3221e41f4b71Sopenharmony_ci 3222e41f4b71Sopenharmony_ci``` 3223e41f4b71Sopenharmony_ci 3224e41f4b71Sopenharmony_ci### setCipherSpec<sup>10+</sup> 3225e41f4b71Sopenharmony_ci 3226e41f4b71Sopenharmony_cisetCipherSpec(itemType: CipherSpecItem, itemValue: Uint8Array): void 3227e41f4b71Sopenharmony_ci 3228e41f4b71Sopenharmony_ciSets cipher specifications. You can use this API to set cipher specifications that cannot be set by [createCipher](#cryptoframeworkcreatecipher). Currently, only RSA is supported. 3229e41f4b71Sopenharmony_ci 3230e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3231e41f4b71Sopenharmony_ci 3232e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3233e41f4b71Sopenharmony_ci 3234e41f4b71Sopenharmony_ci**Parameters** 3235e41f4b71Sopenharmony_ci 3236e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3237e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------- | 3238e41f4b71Sopenharmony_ci| itemType | [CipherSpecItem](#cipherspecitem10) | Yes | Cipher parameter to set. | 3239e41f4b71Sopenharmony_ci| itemValue | Uint8Array | Yes | Value of the parameter to set. | 3240e41f4b71Sopenharmony_ci 3241e41f4b71Sopenharmony_ci**Error codes** 3242e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3243e41f4b71Sopenharmony_ci 3244e41f4b71Sopenharmony_ci| ID | Error Message | 3245e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3246e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3247e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 3248e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3249e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3250e41f4b71Sopenharmony_ci 3251e41f4b71Sopenharmony_ci**Example** 3252e41f4b71Sopenharmony_ci 3253e41f4b71Sopenharmony_ci<!--code_no_check--> 3254e41f4b71Sopenharmony_ci```ts 3255e41f4b71Sopenharmony_cilet cipher: cryptoFramework.Cipher; // The process of generating the Cipher instance is omitted here. 3256e41f4b71Sopenharmony_cilet pSource = new Uint8Array([1,2,3,4]); 3257e41f4b71Sopenharmony_cicipher.setCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR, pSource); 3258e41f4b71Sopenharmony_ci``` 3259e41f4b71Sopenharmony_ci 3260e41f4b71Sopenharmony_ci### getCipherSpec<sup>10+</sup> 3261e41f4b71Sopenharmony_ci 3262e41f4b71Sopenharmony_cigetCipherSpec(itemType: CipherSpecItem): string | Uint8Array 3263e41f4b71Sopenharmony_ci 3264e41f4b71Sopenharmony_ciObtains cipher specifications. Currently, only RSA and SM2 (available since API version 11) are supported. 3265e41f4b71Sopenharmony_ci 3266e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3267e41f4b71Sopenharmony_ci 3268e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3269e41f4b71Sopenharmony_ci 3270e41f4b71Sopenharmony_ci**Parameters** 3271e41f4b71Sopenharmony_ci 3272e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3273e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 3274e41f4b71Sopenharmony_ci| itemType | [CipherSpecItem](#cipherspecitem10) | Yes | Cipher parameter to obtain. | 3275e41f4b71Sopenharmony_ci 3276e41f4b71Sopenharmony_ci**Return value** 3277e41f4b71Sopenharmony_ci 3278e41f4b71Sopenharmony_ci| Type | Description | 3279e41f4b71Sopenharmony_ci| -------------- | ----------- | 3280e41f4b71Sopenharmony_ci| string \| Uint8Array | Returns the value of the cipher parameter obtained. | 3281e41f4b71Sopenharmony_ci 3282e41f4b71Sopenharmony_ci**Error codes** 3283e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3284e41f4b71Sopenharmony_ci 3285e41f4b71Sopenharmony_ci| ID | Error Message | 3286e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3287e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3288e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 3289e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3290e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3291e41f4b71Sopenharmony_ci 3292e41f4b71Sopenharmony_ci**Example** 3293e41f4b71Sopenharmony_ci 3294e41f4b71Sopenharmony_ci<!--code_no_check--> 3295e41f4b71Sopenharmony_ci```ts 3296e41f4b71Sopenharmony_cilet cipher: cryptoFramework.Cipher; // The process of generating the Cipher instance is omitted here. 3297e41f4b71Sopenharmony_cilet mdName = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR); 3298e41f4b71Sopenharmony_ci``` 3299e41f4b71Sopenharmony_ci 3300e41f4b71Sopenharmony_ci## cryptoFramework.createSign 3301e41f4b71Sopenharmony_ci 3302e41f4b71Sopenharmony_cicreateSign(algName: string): Sign 3303e41f4b71Sopenharmony_ci 3304e41f4b71Sopenharmony_ciCreates a **Sign** instance. 3305e41f4b71Sopenharmony_ci 3306e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Signing and Signature Verification Overview and Algorithm Specifications](../../security/CryptoArchitectureKit/crypto-sign-sig-verify-overview.md). 3307e41f4b71Sopenharmony_ci 3308e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3309e41f4b71Sopenharmony_ci 3310e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3311e41f4b71Sopenharmony_ci 3312e41f4b71Sopenharmony_ci**Parameters** 3313e41f4b71Sopenharmony_ci 3314e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3315e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 3316e41f4b71Sopenharmony_ci| algName | string | Yes | Signing algorithm to use. Currently, RSA, ECC, DSA, SM2<sup>10+</sup> and Ed25519<sup>11+</sup> are supported. <br>If the RSA PKCS1 mode is used, you need to set the digest. If the RSA PSS mode is used, you need to set the digest and mask digest.<br>When RSA is used for signing, you can set **OnlySign** to enable the input data digest to be used only for signing. | 3317e41f4b71Sopenharmony_ci 3318e41f4b71Sopenharmony_ci**Return value** 3319e41f4b71Sopenharmony_ci 3320e41f4b71Sopenharmony_ci| Type | Description | 3321e41f4b71Sopenharmony_ci| ---- | ---------------------------------- | 3322e41f4b71Sopenharmony_ci| Sign | Returns the **Sign** instance created. | 3323e41f4b71Sopenharmony_ci 3324e41f4b71Sopenharmony_ci**Error codes** 3325e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3326e41f4b71Sopenharmony_ci 3327e41f4b71Sopenharmony_ci| ID | Error Message | 3328e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3329e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3330e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 3331e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3332e41f4b71Sopenharmony_ci 3333e41f4b71Sopenharmony_ci**Example** 3334e41f4b71Sopenharmony_ci 3335e41f4b71Sopenharmony_ci```ts 3336e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 3337e41f4b71Sopenharmony_ci 3338e41f4b71Sopenharmony_cilet signer1 = cryptoFramework.createSign('RSA1024|PKCS1|SHA256'); 3339e41f4b71Sopenharmony_ci 3340e41f4b71Sopenharmony_cilet signer2 = cryptoFramework.createSign('RSA1024|PSS|SHA256|MGF1_SHA256'); 3341e41f4b71Sopenharmony_ci 3342e41f4b71Sopenharmony_cilet signer3 = cryptoFramework.createSign('ECC224|SHA256'); 3343e41f4b71Sopenharmony_ci 3344e41f4b71Sopenharmony_cilet signer4 = cryptoFramework.createSign('DSA2048|SHA256'); 3345e41f4b71Sopenharmony_ci 3346e41f4b71Sopenharmony_cilet signer5 = cryptoFramework.createSign('RSA1024|PKCS1|SHA256|OnlySign'); 3347e41f4b71Sopenharmony_ci``` 3348e41f4b71Sopenharmony_ci 3349e41f4b71Sopenharmony_ci## Sign 3350e41f4b71Sopenharmony_ci 3351e41f4b71Sopenharmony_ciProvides APIs for signing. Before using any API of the **Sign** class, you must create a **Sign** instance by using [createSign(algName: string): Sign](#cryptoframeworkcreatesign). Invoke **init()**, **update()**, and **sign()** in this class in sequence to complete the signing operation. For details about the sample code, see [Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1.md). 3352e41f4b71Sopenharmony_ci 3353e41f4b71Sopenharmony_ciThe **Sign** class does not support repeated initialization. When a new key is used for signing, you must create a new **Sign** instance and call **init()** for initialization. 3354e41f4b71Sopenharmony_ci 3355e41f4b71Sopenharmony_ciThe signing mode is determined in **createSign()**, and the key is set by **init()**. 3356e41f4b71Sopenharmony_ci 3357e41f4b71Sopenharmony_ciIf the data to be signed is short, you can directly call **sign()** to pass in the original data for signing after **init()**. That is, you do not need to use **update()**. 3358e41f4b71Sopenharmony_ci 3359e41f4b71Sopenharmony_ciIf the data to be signed is long, you can use **update()** to pass in the data by segment, and then use **sign()** to sign the entire data. 3360e41f4b71Sopenharmony_ci 3361e41f4b71Sopenharmony_ciWhen **update()** is used, the **sign()** API supports only **DataBlob** in versions earlier than API version 10 and starts to support **null** since API version 10. After all the data is passed in by using **update()**, **sign()** can be called to sign the data. 3362e41f4b71Sopenharmony_ci 3363e41f4b71Sopenharmony_ciIf the DSA algorithm is used for signing and the digest algorithm is **NoHash**, **update()** is not supported. If **update()** is called in this case, **ERR_CRYPTO_OPERATION** will be returned. 3364e41f4b71Sopenharmony_ci 3365e41f4b71Sopenharmony_ci### Attributes 3366e41f4b71Sopenharmony_ci 3367e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3368e41f4b71Sopenharmony_ci 3369e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3370e41f4b71Sopenharmony_ci 3371e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 3372e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ---------------------------- | 3373e41f4b71Sopenharmony_ci| algName | string | Yes | No | Algorithm to use. | 3374e41f4b71Sopenharmony_ci 3375e41f4b71Sopenharmony_ci### init 3376e41f4b71Sopenharmony_ci 3377e41f4b71Sopenharmony_ciinit(priKey: PriKey, callback: AsyncCallback\<void>): void 3378e41f4b71Sopenharmony_ci 3379e41f4b71Sopenharmony_ciInitializes the **Sign** instance with a private key. This API uses an asynchronous callback to return the result. **init**, **update**, and **sign** must be used together. **init** and **sign** are mandatory, and **update** is optional. 3380e41f4b71Sopenharmony_ci 3381e41f4b71Sopenharmony_ciThe **Sign** class does not support repeated use of **init()**. 3382e41f4b71Sopenharmony_ci 3383e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3384e41f4b71Sopenharmony_ci 3385e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3386e41f4b71Sopenharmony_ci 3387e41f4b71Sopenharmony_ci**Parameters** 3388e41f4b71Sopenharmony_ci 3389e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3390e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------------- | 3391e41f4b71Sopenharmony_ci| priKey | [PriKey](#prikey) | Yes | Private key used for the initialization. | 3392e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 3393e41f4b71Sopenharmony_ci 3394e41f4b71Sopenharmony_ci**Error codes** 3395e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3396e41f4b71Sopenharmony_ci 3397e41f4b71Sopenharmony_ci| ID | Error Message | 3398e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3399e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3400e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3401e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3402e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3403e41f4b71Sopenharmony_ci 3404e41f4b71Sopenharmony_ci### init 3405e41f4b71Sopenharmony_ci 3406e41f4b71Sopenharmony_ciinit(priKey: PriKey): Promise\<void> 3407e41f4b71Sopenharmony_ci 3408e41f4b71Sopenharmony_ciInitializes the **Sign** instance with a private key. This API uses a promise to return the result. **init**, **update**, and **sign** must be used together. **init** and **sign** are mandatory, and **update** is optional. 3409e41f4b71Sopenharmony_ci 3410e41f4b71Sopenharmony_ciThe **Sign** class does not support repeated use of **init()**. 3411e41f4b71Sopenharmony_ci 3412e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3413e41f4b71Sopenharmony_ci 3414e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3415e41f4b71Sopenharmony_ci 3416e41f4b71Sopenharmony_ci**Parameters** 3417e41f4b71Sopenharmony_ci 3418e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3419e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ---------------- | 3420e41f4b71Sopenharmony_ci| priKey | [PriKey](#prikey) | Yes | Private key used for the initialization. | 3421e41f4b71Sopenharmony_ci 3422e41f4b71Sopenharmony_ci**Return value** 3423e41f4b71Sopenharmony_ci 3424e41f4b71Sopenharmony_ci| Type | Description | 3425e41f4b71Sopenharmony_ci| -------------- | ------------- | 3426e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value. | 3427e41f4b71Sopenharmony_ci 3428e41f4b71Sopenharmony_ci**Error codes** 3429e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3430e41f4b71Sopenharmony_ci 3431e41f4b71Sopenharmony_ci| ID | Error Message | 3432e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3433e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3434e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3435e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3436e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3437e41f4b71Sopenharmony_ci 3438e41f4b71Sopenharmony_ci### initSync<sup>12+</sup> 3439e41f4b71Sopenharmony_ci 3440e41f4b71Sopenharmony_ciinitSync(priKey: PriKey): void 3441e41f4b71Sopenharmony_ci 3442e41f4b71Sopenharmony_ciInitializes the **Sign** instance with a private key. This API returns the result synchronously. **initSync**, **updateSync**, and **signSync** must be used together. **initSync** and **signSync** are mandatory, and **updateSync** is optional. 3443e41f4b71Sopenharmony_ci 3444e41f4b71Sopenharmony_ciThe **Sign** class does not support repeated use of **initSync()**. 3445e41f4b71Sopenharmony_ci 3446e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3447e41f4b71Sopenharmony_ci 3448e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3449e41f4b71Sopenharmony_ci 3450e41f4b71Sopenharmony_ci**Parameters** 3451e41f4b71Sopenharmony_ci 3452e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3453e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ---------------- | 3454e41f4b71Sopenharmony_ci| priKey | [PriKey](#prikey) | Yes | Private key used for the initialization. | 3455e41f4b71Sopenharmony_ci 3456e41f4b71Sopenharmony_ci**Error codes** 3457e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3458e41f4b71Sopenharmony_ci 3459e41f4b71Sopenharmony_ci| ID | Error Message | 3460e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3461e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3462e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3463e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3464e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3465e41f4b71Sopenharmony_ci 3466e41f4b71Sopenharmony_ci### update 3467e41f4b71Sopenharmony_ci 3468e41f4b71Sopenharmony_ciupdate(data: DataBlob, callback: AsyncCallback\<void>): void 3469e41f4b71Sopenharmony_ci 3470e41f4b71Sopenharmony_ciUpdates the data to be signed. This API uses an asynchronous callback to return the result. 3471e41f4b71Sopenharmony_ci 3472e41f4b71Sopenharmony_ciThis API can be called only after the [Sign](#sign) instance is initialized by using [init()](#init-2). 3473e41f4b71Sopenharmony_ci 3474e41f4b71Sopenharmony_ci> **NOTE** 3475e41f4b71Sopenharmony_ci> 3476e41f4b71Sopenharmony_ci> You can call **update** multiple times or do not use **update** (call [sign](#sign-1) after [init](#init-2)), depending on the data volume.<br> 3477e41f4b71Sopenharmony_ci> The amount of the data to be passed in by **update()** (one-time or accumulative) is not limited. If there is a large amount of data, you are advised to call **update()** multiple times to pass in the data by segment. This prevents too much memory from being requested at a time.<br> 3478e41f4b71Sopenharmony_ci> For details about the sample code for calling **update()** multiple times in signing, see [Signing and Signature Verification by Segment with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1-by-segment.md). The operations of other algorithms are similar.<br> 3479e41f4b71Sopenharmony_ci> **OnlySign** cannot be used with **update()**. If **OnlySign** is specified, use **sign()** to pass in data.<br> 3480e41f4b71Sopenharmony_ci> If the DSA algorithm is used for signing and the digest algorithm is **NoHash**, **update()** is not supported. If **update()** is called in this case, **ERR_CRYPTO_OPERATION** will be returned. 3481e41f4b71Sopenharmony_ci 3482e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3483e41f4b71Sopenharmony_ci 3484e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3485e41f4b71Sopenharmony_ci 3486e41f4b71Sopenharmony_ci**Parameters** 3487e41f4b71Sopenharmony_ci 3488e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3489e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ------------ | 3490e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to pass in. | 3491e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 3492e41f4b71Sopenharmony_ci 3493e41f4b71Sopenharmony_ci**Error codes** 3494e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3495e41f4b71Sopenharmony_ci 3496e41f4b71Sopenharmony_ci| ID | Error Message | 3497e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3498e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3499e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3500e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3501e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3502e41f4b71Sopenharmony_ci 3503e41f4b71Sopenharmony_ci### update 3504e41f4b71Sopenharmony_ci 3505e41f4b71Sopenharmony_ciupdate(data: DataBlob): Promise\<void> 3506e41f4b71Sopenharmony_ci 3507e41f4b71Sopenharmony_ciUpdates the data to be signed. This API uses a promise to return the result. 3508e41f4b71Sopenharmony_ci 3509e41f4b71Sopenharmony_ciThis API can be called only after the [Sign](#sign) instance is initialized by using [init()](#init-3). 3510e41f4b71Sopenharmony_ci 3511e41f4b71Sopenharmony_ci> **NOTE** 3512e41f4b71Sopenharmony_ci> 3513e41f4b71Sopenharmony_ci> You can call **update** multiple times or do not use **update** (call [sign](#sign-2) after [init](#init-3)), depending on the data volume.<br> 3514e41f4b71Sopenharmony_ci> The amount of the data to be passed in by **update()** (one-time or accumulative) is not limited. If there is a large amount of data, you are advised to call **update()** multiple times to pass in the data by segment. This prevents too much memory from being requested at a time.<br> 3515e41f4b71Sopenharmony_ci> For details about the sample code for calling **update()** multiple times in signing, see [Signing and Signature Verification by Segment with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1-by-segment.md). The operations of other algorithms are similar.<br> 3516e41f4b71Sopenharmony_ci> **OnlySign** cannot be used with **update()**. If **OnlySign** is specified, use **sign()** to pass in data.<br> 3517e41f4b71Sopenharmony_ci> If the DSA algorithm is used for signing and the digest algorithm is **NoHash**, **update()** is not supported. If **update()** is called in this case, **ERR_CRYPTO_OPERATION** will be returned. 3518e41f4b71Sopenharmony_ci 3519e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3520e41f4b71Sopenharmony_ci 3521e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3522e41f4b71Sopenharmony_ci 3523e41f4b71Sopenharmony_ci**Parameters** 3524e41f4b71Sopenharmony_ci 3525e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3526e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 3527e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to pass in. | 3528e41f4b71Sopenharmony_ci 3529e41f4b71Sopenharmony_ci**Return value** 3530e41f4b71Sopenharmony_ci 3531e41f4b71Sopenharmony_ci| Type | Description | 3532e41f4b71Sopenharmony_ci| -------------- | ------------- | 3533e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value. | 3534e41f4b71Sopenharmony_ci 3535e41f4b71Sopenharmony_ci**Error codes** 3536e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3537e41f4b71Sopenharmony_ci 3538e41f4b71Sopenharmony_ci| ID | Error Message | 3539e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3540e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3541e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3542e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3543e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3544e41f4b71Sopenharmony_ci 3545e41f4b71Sopenharmony_ci### updateSync<sup>12+</sup> 3546e41f4b71Sopenharmony_ci 3547e41f4b71Sopenharmony_ciupdateSync(data: DataBlob): void 3548e41f4b71Sopenharmony_ci 3549e41f4b71Sopenharmony_ciUpdates the data to be signed. This API returns the result synchronously. 3550e41f4b71Sopenharmony_ci 3551e41f4b71Sopenharmony_ciThis API can be called only after the [Sign](#sign) instance is initialized by using [initSync()](#initsync12-1). 3552e41f4b71Sopenharmony_ci 3553e41f4b71Sopenharmony_ci> **NOTE** 3554e41f4b71Sopenharmony_ci> 3555e41f4b71Sopenharmony_ci> You can call **updateSync** multiple times or do not use **updateSync** (call [signSync](#signsync12) after [initSync](#initsync12-1)), depending on the data volume.<br> 3556e41f4b71Sopenharmony_ci> The amount of the data to be passed in by **updateSync** (one-time or accumulative) is not limited. If there is a large amount of data, you are advised to call **updateSync** multiple times to pass in the data by segment. This prevents too much memory from being requested at a time.<br> 3557e41f4b71Sopenharmony_ci> For details about the sample code for calling **updateSync** multiple times in signing, see [Signing and Signature Verification by Segment with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1-by-segment.md). The operations of other algorithms are similar.<br> 3558e41f4b71Sopenharmony_ci> **OnlySign** cannot be used with **updateSync**. If **OnlySign** is specified, use **signSync** to pass in data.<br> 3559e41f4b71Sopenharmony_ci> If the DSA algorithm is used for signing and the digest algorithm is **NoHash**, **updateSync** is not supported. If **updateSync** is called in this case, **ERR_CRYPTO_OPERATION** will be returned. 3560e41f4b71Sopenharmony_ci 3561e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3562e41f4b71Sopenharmony_ci 3563e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3564e41f4b71Sopenharmony_ci 3565e41f4b71Sopenharmony_ci**Parameters** 3566e41f4b71Sopenharmony_ci 3567e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3568e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 3569e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to pass in. | 3570e41f4b71Sopenharmony_ci 3571e41f4b71Sopenharmony_ci**Return value** 3572e41f4b71Sopenharmony_ci 3573e41f4b71Sopenharmony_ci| Type | Description | 3574e41f4b71Sopenharmony_ci| -------------- | ------------- | 3575e41f4b71Sopenharmony_ci| void | No value is returned. | 3576e41f4b71Sopenharmony_ci 3577e41f4b71Sopenharmony_ci**Error codes** 3578e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3579e41f4b71Sopenharmony_ci 3580e41f4b71Sopenharmony_ci| ID | Error Message | 3581e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3582e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3583e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3584e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3585e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3586e41f4b71Sopenharmony_ci 3587e41f4b71Sopenharmony_ci### sign 3588e41f4b71Sopenharmony_ci 3589e41f4b71Sopenharmony_cisign(data: DataBlob | null, callback: AsyncCallback\<DataBlob>): void 3590e41f4b71Sopenharmony_ci 3591e41f4b71Sopenharmony_ciSigns the data. This API uses an asynchronous callback to return the result. 3592e41f4b71Sopenharmony_ci 3593e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3594e41f4b71Sopenharmony_ci 3595e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3596e41f4b71Sopenharmony_ci 3597e41f4b71Sopenharmony_ci**Parameters** 3598e41f4b71Sopenharmony_ci 3599e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3600e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------- | 3601e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Data to pass in. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 3602e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[DataBlob](#datablob)> | Yes | Callback invoked to return a **DataBlob** object. | 3603e41f4b71Sopenharmony_ci 3604e41f4b71Sopenharmony_ci**Error codes** 3605e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3606e41f4b71Sopenharmony_ci 3607e41f4b71Sopenharmony_ci| ID | Error Message | 3608e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3609e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3610e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3611e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3612e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3613e41f4b71Sopenharmony_ci 3614e41f4b71Sopenharmony_ci### sign 3615e41f4b71Sopenharmony_ci 3616e41f4b71Sopenharmony_cisign(data: DataBlob | null): Promise\<DataBlob> 3617e41f4b71Sopenharmony_ci 3618e41f4b71Sopenharmony_ciSigns the data. This API uses a promise to return the result. 3619e41f4b71Sopenharmony_ci 3620e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3621e41f4b71Sopenharmony_ci 3622e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3623e41f4b71Sopenharmony_ci 3624e41f4b71Sopenharmony_ci**Parameters** 3625e41f4b71Sopenharmony_ci 3626e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3627e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 3628e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Data to pass in. | 3629e41f4b71Sopenharmony_ci 3630e41f4b71Sopenharmony_ci**Return value** 3631e41f4b71Sopenharmony_ci 3632e41f4b71Sopenharmony_ci| Type | Description | 3633e41f4b71Sopenharmony_ci| -------------- | ------------- | 3634e41f4b71Sopenharmony_ci| Promise\<[DataBlob](#datablob)> | Promise used to return the signature. | 3635e41f4b71Sopenharmony_ci 3636e41f4b71Sopenharmony_ci**Error codes** 3637e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3638e41f4b71Sopenharmony_ci 3639e41f4b71Sopenharmony_ci| ID | Error Message | 3640e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3641e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3642e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3643e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3644e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3645e41f4b71Sopenharmony_ci 3646e41f4b71Sopenharmony_ci### signSync<sup>12+</sup> 3647e41f4b71Sopenharmony_ci 3648e41f4b71Sopenharmony_cisignSync(data: DataBlob | null): DataBlob 3649e41f4b71Sopenharmony_ci 3650e41f4b71Sopenharmony_ciSigns the data. This API returns the result synchronously. 3651e41f4b71Sopenharmony_ci 3652e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3653e41f4b71Sopenharmony_ci 3654e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3655e41f4b71Sopenharmony_ci 3656e41f4b71Sopenharmony_ci**Parameters** 3657e41f4b71Sopenharmony_ci 3658e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3659e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 3660e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) \| null | Yes | Data to pass in. | 3661e41f4b71Sopenharmony_ci 3662e41f4b71Sopenharmony_ci**Return value** 3663e41f4b71Sopenharmony_ci 3664e41f4b71Sopenharmony_ci| Type | Description | 3665e41f4b71Sopenharmony_ci| -------------- | ------------- | 3666e41f4b71Sopenharmony_ci| [DataBlob](#datablob) | Signature. | 3667e41f4b71Sopenharmony_ci 3668e41f4b71Sopenharmony_ci**Error codes** 3669e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3670e41f4b71Sopenharmony_ci 3671e41f4b71Sopenharmony_ci| ID | Error Message | 3672e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3673e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3674e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3675e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3676e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3677e41f4b71Sopenharmony_ci 3678e41f4b71Sopenharmony_ci**Example (using the callback-based API)** 3679e41f4b71Sopenharmony_ci 3680e41f4b71Sopenharmony_ciFor more examples of signing and signature verification, see [Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1.md). 3681e41f4b71Sopenharmony_ci 3682e41f4b71Sopenharmony_ci```ts 3683e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 3684e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 3685e41f4b71Sopenharmony_ci 3686e41f4b71Sopenharmony_cifunction signByCallback() { 3687e41f4b71Sopenharmony_ci let inputUpdate: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 3688e41f4b71Sopenharmony_ci let inputVerify: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 3689e41f4b71Sopenharmony_ci let pkData = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1]); 3690e41f4b71Sopenharmony_ci let skData = new Uint8Array([48, 130, 2, 120, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 98, 48, 130, 2, 94, 2, 1, 0, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1, 2, 129, 129, 0, 152, 111, 145, 203, 10, 88, 116, 163, 112, 126, 9, 20, 68, 34, 235, 121, 98, 14, 182, 102, 151, 125, 114, 91, 210, 122, 215, 29, 212, 5, 176, 203, 238, 146, 5, 190, 41, 21, 91, 56, 125, 239, 111, 133, 53, 200, 192, 56, 132, 202, 42, 145, 120, 3, 224, 40, 223, 46, 148, 29, 41, 92, 17, 40, 12, 72, 165, 69, 192, 211, 142, 233, 81, 202, 177, 235, 156, 27, 179, 48, 18, 85, 154, 101, 193, 45, 218, 91, 24, 143, 196, 248, 16, 83, 177, 198, 136, 77, 111, 134, 60, 219, 95, 246, 23, 5, 45, 14, 83, 29, 137, 248, 159, 28, 132, 142, 205, 99, 226, 213, 84, 232, 57, 130, 156, 81, 191, 237, 2, 65, 0, 255, 158, 212, 13, 43, 132, 244, 135, 148, 161, 232, 219, 20, 81, 196, 102, 103, 44, 110, 71, 100, 62, 73, 200, 32, 138, 114, 209, 171, 150, 179, 92, 198, 5, 190, 218, 79, 227, 227, 37, 32, 57, 159, 252, 107, 211, 139, 198, 202, 248, 137, 143, 186, 205, 106, 81, 85, 207, 134, 148, 110, 204, 243, 27, 2, 65, 0, 215, 4, 181, 121, 57, 224, 170, 168, 183, 159, 152, 8, 74, 233, 80, 244, 146, 81, 48, 159, 194, 199, 36, 187, 6, 181, 182, 223, 115, 133, 151, 171, 78, 219, 90, 161, 248, 69, 6, 207, 173, 3, 81, 161, 2, 60, 238, 204, 177, 12, 138, 17, 220, 179, 71, 113, 200, 248, 159, 153, 252, 150, 180, 155, 2, 65, 0, 190, 202, 185, 211, 170, 171, 238, 40, 84, 84, 21, 13, 144, 57, 7, 178, 183, 71, 126, 120, 98, 229, 235, 4, 40, 229, 173, 149, 185, 209, 29, 199, 29, 54, 164, 161, 38, 8, 30, 62, 83, 179, 47, 42, 165, 0, 156, 207, 160, 39, 169, 229, 81, 180, 136, 170, 116, 182, 20, 233, 45, 90, 100, 9, 2, 65, 0, 152, 255, 47, 198, 15, 201, 238, 133, 89, 11, 133, 153, 184, 252, 37, 239, 177, 65, 118, 80, 231, 190, 222, 66, 250, 118, 72, 166, 221, 67, 156, 245, 119, 138, 28, 6, 142, 107, 71, 122, 116, 200, 156, 199, 237, 152, 191, 239, 4, 184, 64, 114, 143, 81, 62, 48, 23, 233, 217, 95, 47, 221, 104, 171, 2, 64, 30, 219, 1, 230, 241, 70, 246, 243, 121, 174, 67, 66, 11, 99, 202, 17, 52, 234, 78, 29, 3, 57, 51, 123, 149, 86, 64, 192, 73, 199, 108, 101, 55, 232, 41, 114, 153, 237, 253, 52, 205, 148, 45, 86, 186, 241, 182, 183, 42, 77, 252, 195, 29, 158, 173, 3, 182, 207, 254, 61, 71, 184, 167, 184]); 3691e41f4b71Sopenharmony_ci let pubKeyBlob: cryptoFramework.DataBlob = { data: pkData }; 3692e41f4b71Sopenharmony_ci let priKeyBlob: cryptoFramework.DataBlob = { data: skData }; 3693e41f4b71Sopenharmony_ci let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 3694e41f4b71Sopenharmony_ci let signer = cryptoFramework.createSign('RSA1024|PKCS1|SHA256'); 3695e41f4b71Sopenharmony_ci rsaGenerator.convertKey(pubKeyBlob, priKeyBlob, (err, keyPair) => { 3696e41f4b71Sopenharmony_ci signer.init(keyPair.priKey, err => { 3697e41f4b71Sopenharmony_ci signer.update(inputUpdate, err => { 3698e41f4b71Sopenharmony_ci signer.sign(inputVerify, (err, signData) => { 3699e41f4b71Sopenharmony_ci console.info('sign output is ' + signData.data); 3700e41f4b71Sopenharmony_ci }); 3701e41f4b71Sopenharmony_ci }); 3702e41f4b71Sopenharmony_ci }); 3703e41f4b71Sopenharmony_ci }); 3704e41f4b71Sopenharmony_ci} 3705e41f4b71Sopenharmony_ci``` 3706e41f4b71Sopenharmony_ci 3707e41f4b71Sopenharmony_ci**Example (using the promise-based API)** 3708e41f4b71Sopenharmony_ci 3709e41f4b71Sopenharmony_ciFor more examples of signing and signature verification, see [Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1.md). 3710e41f4b71Sopenharmony_ci 3711e41f4b71Sopenharmony_ci```ts 3712e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 3713e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 3714e41f4b71Sopenharmony_ci 3715e41f4b71Sopenharmony_ciasync function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) { 3716e41f4b71Sopenharmony_ci let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData }; 3717e41f4b71Sopenharmony_ci let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData }; 3718e41f4b71Sopenharmony_ci let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 3719e41f4b71Sopenharmony_ci let keyPair = await rsaGenerator.convertKey(pubKeyBlob, priKeyBlob); 3720e41f4b71Sopenharmony_ci console.info('convertKey success'); 3721e41f4b71Sopenharmony_ci return keyPair; 3722e41f4b71Sopenharmony_ci} 3723e41f4b71Sopenharmony_ci 3724e41f4b71Sopenharmony_ciasync function signByPromise() { 3725e41f4b71Sopenharmony_ci let pkData = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1]); 3726e41f4b71Sopenharmony_ci let skData = new Uint8Array([48, 130, 2, 120, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 98, 48, 130, 2, 94, 2, 1, 0, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1, 2, 129, 129, 0, 152, 111, 145, 203, 10, 88, 116, 163, 112, 126, 9, 20, 68, 34, 235, 121, 98, 14, 182, 102, 151, 125, 114, 91, 210, 122, 215, 29, 212, 5, 176, 203, 238, 146, 5, 190, 41, 21, 91, 56, 125, 239, 111, 133, 53, 200, 192, 56, 132, 202, 42, 145, 120, 3, 224, 40, 223, 46, 148, 29, 41, 92, 17, 40, 12, 72, 165, 69, 192, 211, 142, 233, 81, 202, 177, 235, 156, 27, 179, 48, 18, 85, 154, 101, 193, 45, 218, 91, 24, 143, 196, 248, 16, 83, 177, 198, 136, 77, 111, 134, 60, 219, 95, 246, 23, 5, 45, 14, 83, 29, 137, 248, 159, 28, 132, 142, 205, 99, 226, 213, 84, 232, 57, 130, 156, 81, 191, 237, 2, 65, 0, 255, 158, 212, 13, 43, 132, 244, 135, 148, 161, 232, 219, 20, 81, 196, 102, 103, 44, 110, 71, 100, 62, 73, 200, 32, 138, 114, 209, 171, 150, 179, 92, 198, 5, 190, 218, 79, 227, 227, 37, 32, 57, 159, 252, 107, 211, 139, 198, 202, 248, 137, 143, 186, 205, 106, 81, 85, 207, 134, 148, 110, 204, 243, 27, 2, 65, 0, 215, 4, 181, 121, 57, 224, 170, 168, 183, 159, 152, 8, 74, 233, 80, 244, 146, 81, 48, 159, 194, 199, 36, 187, 6, 181, 182, 223, 115, 133, 151, 171, 78, 219, 90, 161, 248, 69, 6, 207, 173, 3, 81, 161, 2, 60, 238, 204, 177, 12, 138, 17, 220, 179, 71, 113, 200, 248, 159, 153, 252, 150, 180, 155, 2, 65, 0, 190, 202, 185, 211, 170, 171, 238, 40, 84, 84, 21, 13, 144, 57, 7, 178, 183, 71, 126, 120, 98, 229, 235, 4, 40, 229, 173, 149, 185, 209, 29, 199, 29, 54, 164, 161, 38, 8, 30, 62, 83, 179, 47, 42, 165, 0, 156, 207, 160, 39, 169, 229, 81, 180, 136, 170, 116, 182, 20, 233, 45, 90, 100, 9, 2, 65, 0, 152, 255, 47, 198, 15, 201, 238, 133, 89, 11, 133, 153, 184, 252, 37, 239, 177, 65, 118, 80, 231, 190, 222, 66, 250, 118, 72, 166, 221, 67, 156, 245, 119, 138, 28, 6, 142, 107, 71, 122, 116, 200, 156, 199, 237, 152, 191, 239, 4, 184, 64, 114, 143, 81, 62, 48, 23, 233, 217, 95, 47, 221, 104, 171, 2, 64, 30, 219, 1, 230, 241, 70, 246, 243, 121, 174, 67, 66, 11, 99, 202, 17, 52, 234, 78, 29, 3, 57, 51, 123, 149, 86, 64, 192, 73, 199, 108, 101, 55, 232, 41, 114, 153, 237, 253, 52, 205, 148, 45, 86, 186, 241, 182, 183, 42, 77, 252, 195, 29, 158, 173, 3, 182, 207, 254, 61, 71, 184, 167, 184]); 3727e41f4b71Sopenharmony_ci let keyPair = await genKeyPairByData(pkData, skData); 3728e41f4b71Sopenharmony_ci let inputUpdate: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 3729e41f4b71Sopenharmony_ci let inputSign: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 3730e41f4b71Sopenharmony_ci let signer = cryptoFramework.createSign('RSA1024|PKCS1|SHA256'); 3731e41f4b71Sopenharmony_ci await signer.init(keyPair.priKey); 3732e41f4b71Sopenharmony_ci await signer.update(inputUpdate); 3733e41f4b71Sopenharmony_ci let signData = await signer.sign(inputSign); 3734e41f4b71Sopenharmony_ci console.info('signData result: ' + signData.data); 3735e41f4b71Sopenharmony_ci} 3736e41f4b71Sopenharmony_ci``` 3737e41f4b71Sopenharmony_ci 3738e41f4b71Sopenharmony_ci**Example (using the sync API)** 3739e41f4b71Sopenharmony_ci 3740e41f4b71Sopenharmony_ciFor more examples of signing and signature verification, see [Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1.md). 3741e41f4b71Sopenharmony_ci 3742e41f4b71Sopenharmony_ci```ts 3743e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 3744e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 3745e41f4b71Sopenharmony_ci 3746e41f4b71Sopenharmony_cifunction genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) { 3747e41f4b71Sopenharmony_ci let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData }; 3748e41f4b71Sopenharmony_ci let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData }; 3749e41f4b71Sopenharmony_ci let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 3750e41f4b71Sopenharmony_ci let keyPair = rsaGenerator.convertKeySync(pubKeyBlob, priKeyBlob); 3751e41f4b71Sopenharmony_ci console.info('convertKeySync success'); 3752e41f4b71Sopenharmony_ci return keyPair; 3753e41f4b71Sopenharmony_ci} 3754e41f4b71Sopenharmony_ci 3755e41f4b71Sopenharmony_cifunction signBySync() { 3756e41f4b71Sopenharmony_ci let pkData = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1]); 3757e41f4b71Sopenharmony_ci let skData = new Uint8Array([48, 130, 2, 120, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 98, 48, 130, 2, 94, 2, 1, 0, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1, 2, 129, 129, 0, 152, 111, 145, 203, 10, 88, 116, 163, 112, 126, 9, 20, 68, 34, 235, 121, 98, 14, 182, 102, 151, 125, 114, 91, 210, 122, 215, 29, 212, 5, 176, 203, 238, 146, 5, 190, 41, 21, 91, 56, 125, 239, 111, 133, 53, 200, 192, 56, 132, 202, 42, 145, 120, 3, 224, 40, 223, 46, 148, 29, 41, 92, 17, 40, 12, 72, 165, 69, 192, 211, 142, 233, 81, 202, 177, 235, 156, 27, 179, 48, 18, 85, 154, 101, 193, 45, 218, 91, 24, 143, 196, 248, 16, 83, 177, 198, 136, 77, 111, 134, 60, 219, 95, 246, 23, 5, 45, 14, 83, 29, 137, 248, 159, 28, 132, 142, 205, 99, 226, 213, 84, 232, 57, 130, 156, 81, 191, 237, 2, 65, 0, 255, 158, 212, 13, 43, 132, 244, 135, 148, 161, 232, 219, 20, 81, 196, 102, 103, 44, 110, 71, 100, 62, 73, 200, 32, 138, 114, 209, 171, 150, 179, 92, 198, 5, 190, 218, 79, 227, 227, 37, 32, 57, 159, 252, 107, 211, 139, 198, 202, 248, 137, 143, 186, 205, 106, 81, 85, 207, 134, 148, 110, 204, 243, 27, 2, 65, 0, 215, 4, 181, 121, 57, 224, 170, 168, 183, 159, 152, 8, 74, 233, 80, 244, 146, 81, 48, 159, 194, 199, 36, 187, 6, 181, 182, 223, 115, 133, 151, 171, 78, 219, 90, 161, 248, 69, 6, 207, 173, 3, 81, 161, 2, 60, 238, 204, 177, 12, 138, 17, 220, 179, 71, 113, 200, 248, 159, 153, 252, 150, 180, 155, 2, 65, 0, 190, 202, 185, 211, 170, 171, 238, 40, 84, 84, 21, 13, 144, 57, 7, 178, 183, 71, 126, 120, 98, 229, 235, 4, 40, 229, 173, 149, 185, 209, 29, 199, 29, 54, 164, 161, 38, 8, 30, 62, 83, 179, 47, 42, 165, 0, 156, 207, 160, 39, 169, 229, 81, 180, 136, 170, 116, 182, 20, 233, 45, 90, 100, 9, 2, 65, 0, 152, 255, 47, 198, 15, 201, 238, 133, 89, 11, 133, 153, 184, 252, 37, 239, 177, 65, 118, 80, 231, 190, 222, 66, 250, 118, 72, 166, 221, 67, 156, 245, 119, 138, 28, 6, 142, 107, 71, 122, 116, 200, 156, 199, 237, 152, 191, 239, 4, 184, 64, 114, 143, 81, 62, 48, 23, 233, 217, 95, 47, 221, 104, 171, 2, 64, 30, 219, 1, 230, 241, 70, 246, 243, 121, 174, 67, 66, 11, 99, 202, 17, 52, 234, 78, 29, 3, 57, 51, 123, 149, 86, 64, 192, 73, 199, 108, 101, 55, 232, 41, 114, 153, 237, 253, 52, 205, 148, 45, 86, 186, 241, 182, 183, 42, 77, 252, 195, 29, 158, 173, 3, 182, 207, 254, 61, 71, 184, 167, 184]); 3758e41f4b71Sopenharmony_ci let keyPair = genKeyPairByData(pkData, skData); 3759e41f4b71Sopenharmony_ci let inputUpdate: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 3760e41f4b71Sopenharmony_ci let inputSign: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 3761e41f4b71Sopenharmony_ci let signer = cryptoFramework.createSign('RSA1024|PKCS1|SHA256'); 3762e41f4b71Sopenharmony_ci signer.initSync(keyPair.priKey); 3763e41f4b71Sopenharmony_ci signer.updateSync(inputUpdate); 3764e41f4b71Sopenharmony_ci let signData = signer.signSync(inputSign); 3765e41f4b71Sopenharmony_ci console.info('signData result: ' + signData.data); 3766e41f4b71Sopenharmony_ci} 3767e41f4b71Sopenharmony_ci``` 3768e41f4b71Sopenharmony_ci 3769e41f4b71Sopenharmony_ci### setSignSpec<sup>10+</sup> 3770e41f4b71Sopenharmony_ci 3771e41f4b71Sopenharmony_cisetSignSpec(itemType: SignSpecItem, itemValue: number): void 3772e41f4b71Sopenharmony_ci 3773e41f4b71Sopenharmony_cisetSignSpec(itemType: SignSpecItem, itemValue: number \| Uint8Array): void 3774e41f4b71Sopenharmony_ci 3775e41f4b71Sopenharmony_ciSets signing specifications. You can use this API to set signing parameters that cannot be set by [createSign](#cryptoframeworkcreatesign). 3776e41f4b71Sopenharmony_ci 3777e41f4b71Sopenharmony_ciCurrently, only RSA and SM2 are supported. Since API version 11, SM2 signing parameters can be set. 3778e41f4b71Sopenharmony_ci 3779e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3780e41f4b71Sopenharmony_ci 3781e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3782e41f4b71Sopenharmony_ci 3783e41f4b71Sopenharmony_ci**Parameters** 3784e41f4b71Sopenharmony_ci 3785e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3786e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------- | 3787e41f4b71Sopenharmony_ci| itemType | [SignSpecItem](#signspecitem10) | Yes | Signing parameter to set. | 3788e41f4b71Sopenharmony_ci| itemValue | number \| Uint8Array<sup>11+</sup> | Yes | Value of the signing parameter to set. | 3789e41f4b71Sopenharmony_ci 3790e41f4b71Sopenharmony_ci**Error codes** 3791e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3792e41f4b71Sopenharmony_ci 3793e41f4b71Sopenharmony_ci| ID | Error Message | 3794e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3795e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3796e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 3797e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3798e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3799e41f4b71Sopenharmony_ci 3800e41f4b71Sopenharmony_ci**Example** 3801e41f4b71Sopenharmony_ci 3802e41f4b71Sopenharmony_ci<!--code_no_check--> 3803e41f4b71Sopenharmony_ci```ts 3804e41f4b71Sopenharmony_cilet signer: cryptoFramework.Sign; // The process of generating the Sign instance is omitted here. 3805e41f4b71Sopenharmony_cilet setN = 20; 3806e41f4b71Sopenharmony_cisigner.setSignSpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, setN); 3807e41f4b71Sopenharmony_ci``` 3808e41f4b71Sopenharmony_ci 3809e41f4b71Sopenharmony_ci### getSignSpec<sup>10+</sup> 3810e41f4b71Sopenharmony_ci 3811e41f4b71Sopenharmony_cigetSignSpec(itemType: SignSpecItem): string | number 3812e41f4b71Sopenharmony_ci 3813e41f4b71Sopenharmony_ciObtains signing specifications. Currently, only RSA is supported. 3814e41f4b71Sopenharmony_ci 3815e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3816e41f4b71Sopenharmony_ci 3817e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3818e41f4b71Sopenharmony_ci 3819e41f4b71Sopenharmony_ci**Parameters** 3820e41f4b71Sopenharmony_ci 3821e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3822e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 3823e41f4b71Sopenharmony_ci| itemType | [SignSpecItem](#signspecitem10) | Yes | Signing parameter to obtain. | 3824e41f4b71Sopenharmony_ci 3825e41f4b71Sopenharmony_ci**Return value** 3826e41f4b71Sopenharmony_ci 3827e41f4b71Sopenharmony_ci| Type | Description | 3828e41f4b71Sopenharmony_ci| -------------- | ----------- | 3829e41f4b71Sopenharmony_ci| string \| number | Returns the value of the signing parameter obtained. | 3830e41f4b71Sopenharmony_ci 3831e41f4b71Sopenharmony_ci**Error codes** 3832e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3833e41f4b71Sopenharmony_ci 3834e41f4b71Sopenharmony_ci| ID | Error Message | 3835e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3836e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3837e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 3838e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3839e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3840e41f4b71Sopenharmony_ci 3841e41f4b71Sopenharmony_ci**Example** 3842e41f4b71Sopenharmony_ci 3843e41f4b71Sopenharmony_ci<!--code_no_check--> 3844e41f4b71Sopenharmony_ci```ts 3845e41f4b71Sopenharmony_cilet signer: cryptoFramework.Sign; // The process of generating the Sign instance is omitted here. 3846e41f4b71Sopenharmony_cilet saltLen = signer.getSignSpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM); 3847e41f4b71Sopenharmony_ci``` 3848e41f4b71Sopenharmony_ci 3849e41f4b71Sopenharmony_ci## cryptoFramework.createVerify 3850e41f4b71Sopenharmony_ci 3851e41f4b71Sopenharmony_cicreateVerify(algName: string): Verify 3852e41f4b71Sopenharmony_ci 3853e41f4b71Sopenharmony_ciCreates a **Verify** instance. 3854e41f4b71Sopenharmony_ci 3855e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Signing and Signature Verification Overview and Algorithm Specifications](../../security/CryptoArchitectureKit/crypto-sign-sig-verify-overview.md). 3856e41f4b71Sopenharmony_ci 3857e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3858e41f4b71Sopenharmony_ci 3859e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3860e41f4b71Sopenharmony_ci 3861e41f4b71Sopenharmony_ci**Parameters** 3862e41f4b71Sopenharmony_ci 3863e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3864e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 3865e41f4b71Sopenharmony_ci| algName | string | Yes | Signing algorithm to use. Currently, RSA, ECC, DSA, SM2<sup>10+</sup> and Ed25519<sup>11+</sup> are supported. <br>If the RSA PKCS1 mode is used, you need to set the digest. If the RSA PSS mode is used, you need to set the digest and mask digest.<br>When the RSA algorithm is used for signature verification, you can use **Recover** to verify and recover the signed data.| 3866e41f4b71Sopenharmony_ci 3867e41f4b71Sopenharmony_ci**Return value** 3868e41f4b71Sopenharmony_ci 3869e41f4b71Sopenharmony_ci| Type | Description | 3870e41f4b71Sopenharmony_ci| ------ | ------------------------------------ | 3871e41f4b71Sopenharmony_ci| Verify | Returns the **Verify** instance created. | 3872e41f4b71Sopenharmony_ci 3873e41f4b71Sopenharmony_ci**Error codes** 3874e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3875e41f4b71Sopenharmony_ci 3876e41f4b71Sopenharmony_ci| ID | Error Message | 3877e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3878e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3879e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 3880e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3881e41f4b71Sopenharmony_ci 3882e41f4b71Sopenharmony_ci**Example** 3883e41f4b71Sopenharmony_ci 3884e41f4b71Sopenharmony_ci```ts 3885e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 3886e41f4b71Sopenharmony_ci 3887e41f4b71Sopenharmony_cilet verifyer1 = cryptoFramework.createVerify('RSA1024|PKCS1|SHA256'); 3888e41f4b71Sopenharmony_ci 3889e41f4b71Sopenharmony_cilet verifyer2 = cryptoFramework.createVerify('RSA1024|PSS|SHA256|MGF1_SHA256'); 3890e41f4b71Sopenharmony_ci 3891e41f4b71Sopenharmony_cilet verifyer3 = cryptoFramework.createVerify('RSA1024|PKCS1|SHA256|Recover'); 3892e41f4b71Sopenharmony_ci``` 3893e41f4b71Sopenharmony_ci 3894e41f4b71Sopenharmony_ci## Verify 3895e41f4b71Sopenharmony_ci 3896e41f4b71Sopenharmony_ciProvides APIs for signature verification. Before using any API of the **Verify** class, you must create a **Verify** instance by using [createVerify(algName: string): Verify](#cryptoframeworkcreateverify). Invoke **init()**, **update()**, and **sign()** in this class in sequence to complete the signature verification. For details about the sample code, see [Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1.md). 3897e41f4b71Sopenharmony_ci 3898e41f4b71Sopenharmony_ciThe **Verify** class does not support repeated initialization. When a new key is used for signature verification, you must create a new **Verify** instance and call **init()** for initialization. 3899e41f4b71Sopenharmony_ci 3900e41f4b71Sopenharmony_ciThe signature verification mode is determined in **createVerify()**, and the key is set by **init()**. 3901e41f4b71Sopenharmony_ci 3902e41f4b71Sopenharmony_ciIf the signed message is short, you can call **verify()** to pass in the signed message and signature (**signatureData**) for signature verification after **init()**. That is, you do not need to use **update()**. 3903e41f4b71Sopenharmony_ci 3904e41f4b71Sopenharmony_ciIf the signed message is too long, you can call **update()** multiple times to pass in the signed message by segment, and then call **verify()** to verify the full text of the message. In versions earlier than API version 10, the input parameter **data** of **verify()** supports only **DataBlob**. Since API version 10, **data** also supports **null**. After all the data is passed in by using **update()**, **verify()** can be called to verify the signature data. 3905e41f4b71Sopenharmony_ci 3906e41f4b71Sopenharmony_ciIf the DSA algorithm is used for signature verification and the digest algorithm is **NoHash**, **update()** is not supported. If **update()** is called in this case, **ERR_CRYPTO_OPERATION** will be returned. 3907e41f4b71Sopenharmony_ci 3908e41f4b71Sopenharmony_ci### Attributes 3909e41f4b71Sopenharmony_ci 3910e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3911e41f4b71Sopenharmony_ci 3912e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3913e41f4b71Sopenharmony_ci 3914e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 3915e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ---------------------------- | 3916e41f4b71Sopenharmony_ci| algName | string | Yes | No | Algorithm to be used for signature verification. | 3917e41f4b71Sopenharmony_ci 3918e41f4b71Sopenharmony_ci### init 3919e41f4b71Sopenharmony_ci 3920e41f4b71Sopenharmony_ciinit(pubKey: PubKey, callback: AsyncCallback\<void>): void 3921e41f4b71Sopenharmony_ci 3922e41f4b71Sopenharmony_ciInitializes the **Verify** instance with a public key. This API uses an asynchronous callback to return the result. **init**, **update**, and **verify** must be used together. **init** and **verify** are mandatory, and **update** is optional. 3923e41f4b71Sopenharmony_ci 3924e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3925e41f4b71Sopenharmony_ci 3926e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3927e41f4b71Sopenharmony_ci 3928e41f4b71Sopenharmony_ci**Parameters** 3929e41f4b71Sopenharmony_ci 3930e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3931e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ------------------------------ | 3932e41f4b71Sopenharmony_ci| pubKey | [PubKey](#pubkey) | Yes | Public key used to initialize the **Verify** instance. | 3933e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 3934e41f4b71Sopenharmony_ci 3935e41f4b71Sopenharmony_ci**Error codes** 3936e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3937e41f4b71Sopenharmony_ci 3938e41f4b71Sopenharmony_ci| ID | Error Message | 3939e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3940e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3941e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3942e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3943e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3944e41f4b71Sopenharmony_ci 3945e41f4b71Sopenharmony_ci### init 3946e41f4b71Sopenharmony_ci 3947e41f4b71Sopenharmony_ciinit(pubKey: PubKey): Promise\<void> 3948e41f4b71Sopenharmony_ci 3949e41f4b71Sopenharmony_ciInitializes the **Verify** instance with a public key. This API uses a promise to return the result. **init**, **update**, and **verify** must be used together. **init** and **verify** are mandatory, and **update** is optional. 3950e41f4b71Sopenharmony_ci 3951e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3952e41f4b71Sopenharmony_ci 3953e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3954e41f4b71Sopenharmony_ci 3955e41f4b71Sopenharmony_ci**Parameters** 3956e41f4b71Sopenharmony_ci 3957e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3958e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ---------------------------- | 3959e41f4b71Sopenharmony_ci| pubKey | [PubKey](#pubkey) | Yes | Public key used to initialize the **Verify** instance. | 3960e41f4b71Sopenharmony_ci 3961e41f4b71Sopenharmony_ci**Return value** 3962e41f4b71Sopenharmony_ci 3963e41f4b71Sopenharmony_ci| Type | Description | 3964e41f4b71Sopenharmony_ci| -------------- | ------------- | 3965e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value. | 3966e41f4b71Sopenharmony_ci 3967e41f4b71Sopenharmony_ci**Error codes** 3968e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 3969e41f4b71Sopenharmony_ci 3970e41f4b71Sopenharmony_ci| ID | Error Message | 3971e41f4b71Sopenharmony_ci| -------- | ---------------------- | 3972e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 3973e41f4b71Sopenharmony_ci| 17620001 | memory error. | 3974e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 3975e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 3976e41f4b71Sopenharmony_ci 3977e41f4b71Sopenharmony_ci### initSync<sup>12+</sup> 3978e41f4b71Sopenharmony_ci 3979e41f4b71Sopenharmony_ciinitSync(pubKey: PubKey): void 3980e41f4b71Sopenharmony_ci 3981e41f4b71Sopenharmony_ciInitializes the **Verify** instance with a public key. This API returns the result synchronously. **initSync**, **updateSync**, and **verifySync** must be used together. **initSync** and **verifySync** are mandatory, and **updateSync** is optional. 3982e41f4b71Sopenharmony_ci 3983e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3984e41f4b71Sopenharmony_ci 3985e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 3986e41f4b71Sopenharmony_ci 3987e41f4b71Sopenharmony_ci**Parameters** 3988e41f4b71Sopenharmony_ci 3989e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3990e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ---------------------------- | 3991e41f4b71Sopenharmony_ci| pubKey | [PubKey](#pubkey) | Yes | Public key used to initialize the **Verify** instance. | 3992e41f4b71Sopenharmony_ci 3993e41f4b71Sopenharmony_ci**Return value** 3994e41f4b71Sopenharmony_ci 3995e41f4b71Sopenharmony_ci| Type | Description | 3996e41f4b71Sopenharmony_ci| -------------- | ------------- | 3997e41f4b71Sopenharmony_ci| void | No value is returned. | 3998e41f4b71Sopenharmony_ci 3999e41f4b71Sopenharmony_ci**Error codes** 4000e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4001e41f4b71Sopenharmony_ci 4002e41f4b71Sopenharmony_ci| ID | Error Message | 4003e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4004e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4005e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4006e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4007e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4008e41f4b71Sopenharmony_ci 4009e41f4b71Sopenharmony_ci### update 4010e41f4b71Sopenharmony_ci 4011e41f4b71Sopenharmony_ciupdate(data: DataBlob, callback: AsyncCallback\<void>): void 4012e41f4b71Sopenharmony_ci 4013e41f4b71Sopenharmony_ciUpdates the data for signature verification. This API uses an asynchronous callback to return the result. 4014e41f4b71Sopenharmony_ci 4015e41f4b71Sopenharmony_ciThis API can be called only after the [Verify](#verify) instance is initialized using [init()](#init-4). 4016e41f4b71Sopenharmony_ci 4017e41f4b71Sopenharmony_ci> **NOTE** 4018e41f4b71Sopenharmony_ci> 4019e41f4b71Sopenharmony_ci> You can call **update** multiple times or do not use **update** (call [verify](#verify-1) after [init](#init-4)), depending on the data volume.<br> 4020e41f4b71Sopenharmony_ci> The amount of the data to be passed in by **update()** (one-time or accumulative) is not limited. If there is a large amount of data, you are advised to call **update()** multiple times to pass in the data by segment. This prevents too much memory from being requested at a time.<br> 4021e41f4b71Sopenharmony_ci> For details about the sample code for calling **update()** multiple times in signature verification, see [Signing and Signature Verification by Segment with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1-by-segment.md). The operations of other algorithms are similar.<br> 4022e41f4b71Sopenharmony_ci> If the DSA algorithm is used for signature verification and the digest algorithm is **NoHash**, **update()** is not supported. If **update()** is called in this case, **ERR_CRYPTO_OPERATION** will be returned. 4023e41f4b71Sopenharmony_ci 4024e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4025e41f4b71Sopenharmony_ci 4026e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4027e41f4b71Sopenharmony_ci 4028e41f4b71Sopenharmony_ci**Parameters** 4029e41f4b71Sopenharmony_ci 4030e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4031e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ------------ | 4032e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to pass in. | 4033e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 4034e41f4b71Sopenharmony_ci 4035e41f4b71Sopenharmony_ci**Error codes** 4036e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4037e41f4b71Sopenharmony_ci 4038e41f4b71Sopenharmony_ci| ID | Error Message | 4039e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4040e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4041e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4042e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4043e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4044e41f4b71Sopenharmony_ci 4045e41f4b71Sopenharmony_ci### update 4046e41f4b71Sopenharmony_ci 4047e41f4b71Sopenharmony_ciupdate(data: DataBlob): Promise\<void> 4048e41f4b71Sopenharmony_ci 4049e41f4b71Sopenharmony_ciUpdates the data for signature verifications. This API uses a promise to return the result. 4050e41f4b71Sopenharmony_ci 4051e41f4b71Sopenharmony_ciThis API can be called only after the [Verify](#verify) instance is initialized using [init()](#init-5). 4052e41f4b71Sopenharmony_ci 4053e41f4b71Sopenharmony_ci> **NOTE** 4054e41f4b71Sopenharmony_ci> 4055e41f4b71Sopenharmony_ci> You can call **update** multiple times or do not use **update** (call [verify](#verify-2) after [init](#init-5)), depending on the data volume.<br> 4056e41f4b71Sopenharmony_ci> The amount of the data to be passed in by **update()** (one-time or accumulative) is not limited. If there is a large amount of data, you are advised to call **update()** multiple times to pass in the data by segment. This prevents too much memory from being requested at a time.<br> 4057e41f4b71Sopenharmony_ci> For details about the sample code for calling **update()** multiple times in signature verification, see [Signing and Signature Verification by Segment with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1-by-segment.md). The operations of other algorithms are similar.<br> 4058e41f4b71Sopenharmony_ci> If the DSA algorithm is used for signature verification and the digest algorithm is **NoHash**, **update()** is not supported. If **update()** is called in this case, **ERR_CRYPTO_OPERATION** will be returned. 4059e41f4b71Sopenharmony_ci 4060e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4061e41f4b71Sopenharmony_ci 4062e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4063e41f4b71Sopenharmony_ci 4064e41f4b71Sopenharmony_ci**Parameters** 4065e41f4b71Sopenharmony_ci 4066e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4067e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 4068e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to pass in. | 4069e41f4b71Sopenharmony_ci 4070e41f4b71Sopenharmony_ci**Return value** 4071e41f4b71Sopenharmony_ci 4072e41f4b71Sopenharmony_ci| Type | Description | 4073e41f4b71Sopenharmony_ci| -------------- | ------------- | 4074e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value. | 4075e41f4b71Sopenharmony_ci 4076e41f4b71Sopenharmony_ci**Error codes** 4077e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4078e41f4b71Sopenharmony_ci 4079e41f4b71Sopenharmony_ci| ID | Error Message | 4080e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4081e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4082e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4083e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4084e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4085e41f4b71Sopenharmony_ci 4086e41f4b71Sopenharmony_ci### updateSync<sup>12+</sup> 4087e41f4b71Sopenharmony_ci 4088e41f4b71Sopenharmony_ciupdateSync(data: DataBlob): void 4089e41f4b71Sopenharmony_ci 4090e41f4b71Sopenharmony_ciUpdates the data for signature verifications. This API returns the result synchronously. 4091e41f4b71Sopenharmony_ci 4092e41f4b71Sopenharmony_ciThis API can be called only after the [Verify](#verify) instance is initialized by using [initSync()](#initsync12-2). 4093e41f4b71Sopenharmony_ci 4094e41f4b71Sopenharmony_ci> **NOTE** 4095e41f4b71Sopenharmony_ci> 4096e41f4b71Sopenharmony_ci> You can call **updateSync** multiple times or do not use **updateSync** (call [verifySync](#verifysync12)after [initSync](#initsync12-2)), depending on the data volume.<br> 4097e41f4b71Sopenharmony_ci> The amount of the data to be passed in by **updateSync** (one-time or accumulative) is not limited. If there is a large amount of data, you are advised to call **updateSync** multiple times to pass in the data by segment. This prevents too much memory from being requested at a time.<br> 4098e41f4b71Sopenharmony_ci> For details about the sample code for calling **updateSync** multiple times in signature verification, see [Signing and Signature Verification by Segment with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1-by-segment.md). The operations of other algorithms are similar.<br> 4099e41f4b71Sopenharmony_ci> If the DSA algorithm is used for signature verification and the digest algorithm is **NoHash**, **updateSync** is not supported. If **updateSync** is called in this case, **ERR_CRYPTO_OPERATION** will be returned. 4100e41f4b71Sopenharmony_ci 4101e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4102e41f4b71Sopenharmony_ci 4103e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4104e41f4b71Sopenharmony_ci 4105e41f4b71Sopenharmony_ci**Parameters** 4106e41f4b71Sopenharmony_ci 4107e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4108e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 4109e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) | Yes | Data to pass in. | 4110e41f4b71Sopenharmony_ci 4111e41f4b71Sopenharmony_ci**Return value** 4112e41f4b71Sopenharmony_ci 4113e41f4b71Sopenharmony_ci| Type | Description | 4114e41f4b71Sopenharmony_ci| -------------- | ------------- | 4115e41f4b71Sopenharmony_ci| void | No value is returned. | 4116e41f4b71Sopenharmony_ci 4117e41f4b71Sopenharmony_ci**Error codes** 4118e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4119e41f4b71Sopenharmony_ci 4120e41f4b71Sopenharmony_ci| ID | Error Message | 4121e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4122e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4123e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4124e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4125e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4126e41f4b71Sopenharmony_ci 4127e41f4b71Sopenharmony_ci### verify 4128e41f4b71Sopenharmony_ci 4129e41f4b71Sopenharmony_civerify(data: DataBlob | null, signatureData: DataBlob, callback: AsyncCallback\<boolean>): void 4130e41f4b71Sopenharmony_ci 4131e41f4b71Sopenharmony_ciVerifies the signature. This API uses an asynchronous callback to return the result. 4132e41f4b71Sopenharmony_ci 4133e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4134e41f4b71Sopenharmony_ci 4135e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4136e41f4b71Sopenharmony_ci 4137e41f4b71Sopenharmony_ci**Parameters** 4138e41f4b71Sopenharmony_ci 4139e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4140e41f4b71Sopenharmony_ci| ------------- | -------------------- | ---- | ---------- | 4141e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Data to pass in. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 4142e41f4b71Sopenharmony_ci| signatureData | [DataBlob](#datablob) | Yes | Signature data. | 4143e41f4b71Sopenharmony_ci| callback | AsyncCallback\<boolean> | Yes | Callback invoked to return the signature verification result. | 4144e41f4b71Sopenharmony_ci 4145e41f4b71Sopenharmony_ci**Error codes** 4146e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4147e41f4b71Sopenharmony_ci 4148e41f4b71Sopenharmony_ci| ID | Error Message | 4149e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4150e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4151e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4152e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4153e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4154e41f4b71Sopenharmony_ci 4155e41f4b71Sopenharmony_ci### verify 4156e41f4b71Sopenharmony_ci 4157e41f4b71Sopenharmony_civerify(data: DataBlob | null, signatureData: DataBlob): Promise\<boolean> 4158e41f4b71Sopenharmony_ci 4159e41f4b71Sopenharmony_ciVerifies the signature. This API uses a promise to return the result. 4160e41f4b71Sopenharmony_ci 4161e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4162e41f4b71Sopenharmony_ci 4163e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4164e41f4b71Sopenharmony_ci 4165e41f4b71Sopenharmony_ci**Parameters** 4166e41f4b71Sopenharmony_ci 4167e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4168e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | ---------- | 4169e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) \| null<sup>10+</sup> | Yes | Data to pass in. In versions earlier than API version 10, only **DataBlob** is supported. Since API version 10, **null** is also supported. | 4170e41f4b71Sopenharmony_ci| signatureData | [DataBlob](#datablob) | Yes | Signature data. | 4171e41f4b71Sopenharmony_ci 4172e41f4b71Sopenharmony_ci**Return value** 4173e41f4b71Sopenharmony_ci 4174e41f4b71Sopenharmony_ci| Type | Description | 4175e41f4b71Sopenharmony_ci| ----------------- | ------------------------------ | 4176e41f4b71Sopenharmony_ci| Promise\<boolean> | Promise used to return the result. | 4177e41f4b71Sopenharmony_ci 4178e41f4b71Sopenharmony_ci**Error codes** 4179e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4180e41f4b71Sopenharmony_ci 4181e41f4b71Sopenharmony_ci| ID | Error Message | 4182e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4183e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4184e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4185e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4186e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4187e41f4b71Sopenharmony_ci 4188e41f4b71Sopenharmony_ci### verifySync<sup>12+</sup> 4189e41f4b71Sopenharmony_ci 4190e41f4b71Sopenharmony_civerifySync(data: DataBlob | null, signatureData: DataBlob): boolean 4191e41f4b71Sopenharmony_ci 4192e41f4b71Sopenharmony_ciVerifies the signature. This API returns the verification result synchronously. 4193e41f4b71Sopenharmony_ci 4194e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4195e41f4b71Sopenharmony_ci 4196e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4197e41f4b71Sopenharmony_ci 4198e41f4b71Sopenharmony_ci**Parameters** 4199e41f4b71Sopenharmony_ci 4200e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4201e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | ---------- | 4202e41f4b71Sopenharmony_ci| data | [DataBlob](#datablob) \| null | Yes | Data to pass in. | 4203e41f4b71Sopenharmony_ci| signatureData | [DataBlob](#datablob) | Yes | Signature data. | 4204e41f4b71Sopenharmony_ci 4205e41f4b71Sopenharmony_ci**Return value** 4206e41f4b71Sopenharmony_ci 4207e41f4b71Sopenharmony_ci| Type | Description | 4208e41f4b71Sopenharmony_ci| ----------------- | ------------------------------ | 4209e41f4b71Sopenharmony_ci| boolean | Signature verification result. | 4210e41f4b71Sopenharmony_ci 4211e41f4b71Sopenharmony_ci**Error codes** 4212e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4213e41f4b71Sopenharmony_ci 4214e41f4b71Sopenharmony_ci| ID | Error Message | 4215e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4216e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4217e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4218e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4219e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4220e41f4b71Sopenharmony_ci 4221e41f4b71Sopenharmony_ci**Example (using the callback-based API)** 4222e41f4b71Sopenharmony_ci 4223e41f4b71Sopenharmony_ciFor more examples of signing and signature verification, see [Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1.md). 4224e41f4b71Sopenharmony_ci 4225e41f4b71Sopenharmony_ci```ts 4226e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 4227e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 4228e41f4b71Sopenharmony_ci 4229e41f4b71Sopenharmony_cifunction verifyByCallback() { 4230e41f4b71Sopenharmony_ci let inputUpdate: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 4231e41f4b71Sopenharmony_ci let inputVerify: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 4232e41f4b71Sopenharmony_ci // Key generated based on the key data and input data for signature verification. If the data in verify() is the same as that in sign(), the signature verification is successful. 4233e41f4b71Sopenharmony_ci let pkData = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1]); 4234e41f4b71Sopenharmony_ci let skData = new Uint8Array([48, 130, 2, 120, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 98, 48, 130, 2, 94, 2, 1, 0, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1, 2, 129, 129, 0, 152, 111, 145, 203, 10, 88, 116, 163, 112, 126, 9, 20, 68, 34, 235, 121, 98, 14, 182, 102, 151, 125, 114, 91, 210, 122, 215, 29, 212, 5, 176, 203, 238, 146, 5, 190, 41, 21, 91, 56, 125, 239, 111, 133, 53, 200, 192, 56, 132, 202, 42, 145, 120, 3, 224, 40, 223, 46, 148, 29, 41, 92, 17, 40, 12, 72, 165, 69, 192, 211, 142, 233, 81, 202, 177, 235, 156, 27, 179, 48, 18, 85, 154, 101, 193, 45, 218, 91, 24, 143, 196, 248, 16, 83, 177, 198, 136, 77, 111, 134, 60, 219, 95, 246, 23, 5, 45, 14, 83, 29, 137, 248, 159, 28, 132, 142, 205, 99, 226, 213, 84, 232, 57, 130, 156, 81, 191, 237, 2, 65, 0, 255, 158, 212, 13, 43, 132, 244, 135, 148, 161, 232, 219, 20, 81, 196, 102, 103, 44, 110, 71, 100, 62, 73, 200, 32, 138, 114, 209, 171, 150, 179, 92, 198, 5, 190, 218, 79, 227, 227, 37, 32, 57, 159, 252, 107, 211, 139, 198, 202, 248, 137, 143, 186, 205, 106, 81, 85, 207, 134, 148, 110, 204, 243, 27, 2, 65, 0, 215, 4, 181, 121, 57, 224, 170, 168, 183, 159, 152, 8, 74, 233, 80, 244, 146, 81, 48, 159, 194, 199, 36, 187, 6, 181, 182, 223, 115, 133, 151, 171, 78, 219, 90, 161, 248, 69, 6, 207, 173, 3, 81, 161, 2, 60, 238, 204, 177, 12, 138, 17, 220, 179, 71, 113, 200, 248, 159, 153, 252, 150, 180, 155, 2, 65, 0, 190, 202, 185, 211, 170, 171, 238, 40, 84, 84, 21, 13, 144, 57, 7, 178, 183, 71, 126, 120, 98, 229, 235, 4, 40, 229, 173, 149, 185, 209, 29, 199, 29, 54, 164, 161, 38, 8, 30, 62, 83, 179, 47, 42, 165, 0, 156, 207, 160, 39, 169, 229, 81, 180, 136, 170, 116, 182, 20, 233, 45, 90, 100, 9, 2, 65, 0, 152, 255, 47, 198, 15, 201, 238, 133, 89, 11, 133, 153, 184, 252, 37, 239, 177, 65, 118, 80, 231, 190, 222, 66, 250, 118, 72, 166, 221, 67, 156, 245, 119, 138, 28, 6, 142, 107, 71, 122, 116, 200, 156, 199, 237, 152, 191, 239, 4, 184, 64, 114, 143, 81, 62, 48, 23, 233, 217, 95, 47, 221, 104, 171, 2, 64, 30, 219, 1, 230, 241, 70, 246, 243, 121, 174, 67, 66, 11, 99, 202, 17, 52, 234, 78, 29, 3, 57, 51, 123, 149, 86, 64, 192, 73, 199, 108, 101, 55, 232, 41, 114, 153, 237, 253, 52, 205, 148, 45, 86, 186, 241, 182, 183, 42, 77, 252, 195, 29, 158, 173, 3, 182, 207, 254, 61, 71, 184, 167, 184]); 4235e41f4b71Sopenharmony_ci let pubKeyBlob: cryptoFramework.DataBlob = { data: pkData }; 4236e41f4b71Sopenharmony_ci let priKeyBlob: cryptoFramework.DataBlob = { data: skData }; 4237e41f4b71Sopenharmony_ci // The data is signData.data in Sign(). 4238e41f4b71Sopenharmony_ci let signMessageBlob: cryptoFramework.DataBlob = { data: new Uint8Array([9, 68, 164, 161, 230, 155, 255, 153, 10, 12, 14, 22, 146, 115, 209, 167, 223, 133, 89, 173, 50, 249, 176, 104, 10, 251, 219, 104, 117, 196, 105, 65, 249, 139, 119, 41, 15, 171, 191, 11, 177, 177, 1, 119, 130, 142, 87, 183, 32, 220, 226, 28, 38, 73, 222, 172, 153, 26, 87, 58, 188, 42, 150, 67, 94, 214, 147, 64, 202, 87, 155, 125, 254, 112, 95, 176, 255, 207, 106, 43, 228, 153, 131, 240, 120, 88, 253, 179, 207, 207, 110, 223, 173, 15, 113, 11, 183, 122, 237, 205, 206, 123, 246, 33, 167, 169, 251, 237, 199, 26, 220, 152, 190, 117, 131, 74, 232, 50, 39, 172, 232, 178, 112, 73, 251, 235, 131, 209]) } 4239e41f4b71Sopenharmony_ci let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 4240e41f4b71Sopenharmony_ci let verifyer = cryptoFramework.createVerify('RSA1024|PKCS1|SHA256'); 4241e41f4b71Sopenharmony_ci rsaGenerator.convertKey(pubKeyBlob, priKeyBlob, (err, keyPair) => { 4242e41f4b71Sopenharmony_ci verifyer.init(keyPair.pubKey, err => { 4243e41f4b71Sopenharmony_ci verifyer.update(inputUpdate, err => { 4244e41f4b71Sopenharmony_ci verifyer.verify(inputVerify, signMessageBlob, (err, res) => { 4245e41f4b71Sopenharmony_ci console.info('verify result is ' + res); 4246e41f4b71Sopenharmony_ci }); 4247e41f4b71Sopenharmony_ci }); 4248e41f4b71Sopenharmony_ci }); 4249e41f4b71Sopenharmony_ci }); 4250e41f4b71Sopenharmony_ci} 4251e41f4b71Sopenharmony_ci``` 4252e41f4b71Sopenharmony_ci 4253e41f4b71Sopenharmony_ci**Example (using the promise-based API)** 4254e41f4b71Sopenharmony_ci 4255e41f4b71Sopenharmony_ciFor more examples of signing and signature verification, see [Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1.md). 4256e41f4b71Sopenharmony_ci 4257e41f4b71Sopenharmony_ci```ts 4258e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 4259e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 4260e41f4b71Sopenharmony_ci 4261e41f4b71Sopenharmony_ciasync function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) { 4262e41f4b71Sopenharmony_ci let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData }; 4263e41f4b71Sopenharmony_ci let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData }; 4264e41f4b71Sopenharmony_ci let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 4265e41f4b71Sopenharmony_ci let keyPair = await rsaGenerator.convertKey(pubKeyBlob, priKeyBlob); 4266e41f4b71Sopenharmony_ci console.info('convertKey success'); 4267e41f4b71Sopenharmony_ci return keyPair; 4268e41f4b71Sopenharmony_ci} 4269e41f4b71Sopenharmony_ci 4270e41f4b71Sopenharmony_ciasync function verifyByPromise() { 4271e41f4b71Sopenharmony_ci // Key generated based on the key data and input data for signature verification. If the data in verify() is the same as that in sign(), the signature verification is successful. 4272e41f4b71Sopenharmony_ci let pkData = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1]); 4273e41f4b71Sopenharmony_ci let skData = new Uint8Array([48, 130, 2, 120, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 98, 48, 130, 2, 94, 2, 1, 0, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1, 2, 129, 129, 0, 152, 111, 145, 203, 10, 88, 116, 163, 112, 126, 9, 20, 68, 34, 235, 121, 98, 14, 182, 102, 151, 125, 114, 91, 210, 122, 215, 29, 212, 5, 176, 203, 238, 146, 5, 190, 41, 21, 91, 56, 125, 239, 111, 133, 53, 200, 192, 56, 132, 202, 42, 145, 120, 3, 224, 40, 223, 46, 148, 29, 41, 92, 17, 40, 12, 72, 165, 69, 192, 211, 142, 233, 81, 202, 177, 235, 156, 27, 179, 48, 18, 85, 154, 101, 193, 45, 218, 91, 24, 143, 196, 248, 16, 83, 177, 198, 136, 77, 111, 134, 60, 219, 95, 246, 23, 5, 45, 14, 83, 29, 137, 248, 159, 28, 132, 142, 205, 99, 226, 213, 84, 232, 57, 130, 156, 81, 191, 237, 2, 65, 0, 255, 158, 212, 13, 43, 132, 244, 135, 148, 161, 232, 219, 20, 81, 196, 102, 103, 44, 110, 71, 100, 62, 73, 200, 32, 138, 114, 209, 171, 150, 179, 92, 198, 5, 190, 218, 79, 227, 227, 37, 32, 57, 159, 252, 107, 211, 139, 198, 202, 248, 137, 143, 186, 205, 106, 81, 85, 207, 134, 148, 110, 204, 243, 27, 2, 65, 0, 215, 4, 181, 121, 57, 224, 170, 168, 183, 159, 152, 8, 74, 233, 80, 244, 146, 81, 48, 159, 194, 199, 36, 187, 6, 181, 182, 223, 115, 133, 151, 171, 78, 219, 90, 161, 248, 69, 6, 207, 173, 3, 81, 161, 2, 60, 238, 204, 177, 12, 138, 17, 220, 179, 71, 113, 200, 248, 159, 153, 252, 150, 180, 155, 2, 65, 0, 190, 202, 185, 211, 170, 171, 238, 40, 84, 84, 21, 13, 144, 57, 7, 178, 183, 71, 126, 120, 98, 229, 235, 4, 40, 229, 173, 149, 185, 209, 29, 199, 29, 54, 164, 161, 38, 8, 30, 62, 83, 179, 47, 42, 165, 0, 156, 207, 160, 39, 169, 229, 81, 180, 136, 170, 116, 182, 20, 233, 45, 90, 100, 9, 2, 65, 0, 152, 255, 47, 198, 15, 201, 238, 133, 89, 11, 133, 153, 184, 252, 37, 239, 177, 65, 118, 80, 231, 190, 222, 66, 250, 118, 72, 166, 221, 67, 156, 245, 119, 138, 28, 6, 142, 107, 71, 122, 116, 200, 156, 199, 237, 152, 191, 239, 4, 184, 64, 114, 143, 81, 62, 48, 23, 233, 217, 95, 47, 221, 104, 171, 2, 64, 30, 219, 1, 230, 241, 70, 246, 243, 121, 174, 67, 66, 11, 99, 202, 17, 52, 234, 78, 29, 3, 57, 51, 123, 149, 86, 64, 192, 73, 199, 108, 101, 55, 232, 41, 114, 153, 237, 253, 52, 205, 148, 45, 86, 186, 241, 182, 183, 42, 77, 252, 195, 29, 158, 173, 3, 182, 207, 254, 61, 71, 184, 167, 184]); 4274e41f4b71Sopenharmony_ci let keyPair = await genKeyPairByData(pkData, skData); 4275e41f4b71Sopenharmony_ci let inputUpdate: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 4276e41f4b71Sopenharmony_ci let inputVerify: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 4277e41f4b71Sopenharmony_ci // The data is signData.data in Sign(). 4278e41f4b71Sopenharmony_ci let signMessageBlob: cryptoFramework.DataBlob = { data: new Uint8Array([9, 68, 164, 161, 230, 155, 255, 153, 10, 12, 14, 22, 146, 115, 209, 167, 223, 133, 89, 173, 50, 249, 176, 104, 10, 251, 219, 104, 117, 196, 105, 65, 249, 139, 119, 41, 15, 171, 191, 11, 177, 177, 1, 119, 130, 142, 87, 183, 32, 220, 226, 28, 38, 73, 222, 172, 153, 26, 87, 58, 188, 42, 150, 67, 94, 214, 147, 64, 202, 87, 155, 125, 254, 112, 95, 176, 255, 207, 106, 43, 228, 153, 131, 240, 120, 88, 253, 179, 207, 207, 110, 223, 173, 15, 113, 11, 183, 122, 237, 205, 206, 123, 246, 33, 167, 169, 251, 237, 199, 26, 220, 152, 190, 117, 131, 74, 232, 50, 39, 172, 232, 178, 112, 73, 251, 235, 131, 209]) }; 4279e41f4b71Sopenharmony_ci let verifier = cryptoFramework.createVerify('RSA1024|PKCS1|SHA256'); 4280e41f4b71Sopenharmony_ci await verifier.init(keyPair.pubKey); 4281e41f4b71Sopenharmony_ci await verifier.update(inputUpdate); 4282e41f4b71Sopenharmony_ci let res = await verifier.verify(inputVerify, signMessageBlob); 4283e41f4b71Sopenharmony_ci console.info('verify result: ' + res); 4284e41f4b71Sopenharmony_ci} 4285e41f4b71Sopenharmony_ci``` 4286e41f4b71Sopenharmony_ci 4287e41f4b71Sopenharmony_ci**Example (using the sync API)** 4288e41f4b71Sopenharmony_ci 4289e41f4b71Sopenharmony_ciFor more examples of signing and signature verification, see [Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode)](../../security/CryptoArchitectureKit/crypto-rsa-sign-sig-verify-pkcs1.md). 4290e41f4b71Sopenharmony_ci 4291e41f4b71Sopenharmony_ci```ts 4292e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 4293e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 4294e41f4b71Sopenharmony_ci 4295e41f4b71Sopenharmony_cifunction genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) { 4296e41f4b71Sopenharmony_ci let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData }; 4297e41f4b71Sopenharmony_ci let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData }; 4298e41f4b71Sopenharmony_ci let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 4299e41f4b71Sopenharmony_ci let keyPair = rsaGenerator.convertKeySync(pubKeyBlob, priKeyBlob); 4300e41f4b71Sopenharmony_ci console.info('convertKey success'); 4301e41f4b71Sopenharmony_ci return keyPair; 4302e41f4b71Sopenharmony_ci} 4303e41f4b71Sopenharmony_ci 4304e41f4b71Sopenharmony_cifunction verifyBySync() { 4305e41f4b71Sopenharmony_ci // Key generated based on the key data and input data for signature verification. If the data in verify() is the same as that in sign(), the signature verification is successful. 4306e41f4b71Sopenharmony_ci let pkData = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1]); 4307e41f4b71Sopenharmony_ci let skData = new Uint8Array([48, 130, 2, 120, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 98, 48, 130, 2, 94, 2, 1, 0, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1, 2, 129, 129, 0, 152, 111, 145, 203, 10, 88, 116, 163, 112, 126, 9, 20, 68, 34, 235, 121, 98, 14, 182, 102, 151, 125, 114, 91, 210, 122, 215, 29, 212, 5, 176, 203, 238, 146, 5, 190, 41, 21, 91, 56, 125, 239, 111, 133, 53, 200, 192, 56, 132, 202, 42, 145, 120, 3, 224, 40, 223, 46, 148, 29, 41, 92, 17, 40, 12, 72, 165, 69, 192, 211, 142, 233, 81, 202, 177, 235, 156, 27, 179, 48, 18, 85, 154, 101, 193, 45, 218, 91, 24, 143, 196, 248, 16, 83, 177, 198, 136, 77, 111, 134, 60, 219, 95, 246, 23, 5, 45, 14, 83, 29, 137, 248, 159, 28, 132, 142, 205, 99, 226, 213, 84, 232, 57, 130, 156, 81, 191, 237, 2, 65, 0, 255, 158, 212, 13, 43, 132, 244, 135, 148, 161, 232, 219, 20, 81, 196, 102, 103, 44, 110, 71, 100, 62, 73, 200, 32, 138, 114, 209, 171, 150, 179, 92, 198, 5, 190, 218, 79, 227, 227, 37, 32, 57, 159, 252, 107, 211, 139, 198, 202, 248, 137, 143, 186, 205, 106, 81, 85, 207, 134, 148, 110, 204, 243, 27, 2, 65, 0, 215, 4, 181, 121, 57, 224, 170, 168, 183, 159, 152, 8, 74, 233, 80, 244, 146, 81, 48, 159, 194, 199, 36, 187, 6, 181, 182, 223, 115, 133, 151, 171, 78, 219, 90, 161, 248, 69, 6, 207, 173, 3, 81, 161, 2, 60, 238, 204, 177, 12, 138, 17, 220, 179, 71, 113, 200, 248, 159, 153, 252, 150, 180, 155, 2, 65, 0, 190, 202, 185, 211, 170, 171, 238, 40, 84, 84, 21, 13, 144, 57, 7, 178, 183, 71, 126, 120, 98, 229, 235, 4, 40, 229, 173, 149, 185, 209, 29, 199, 29, 54, 164, 161, 38, 8, 30, 62, 83, 179, 47, 42, 165, 0, 156, 207, 160, 39, 169, 229, 81, 180, 136, 170, 116, 182, 20, 233, 45, 90, 100, 9, 2, 65, 0, 152, 255, 47, 198, 15, 201, 238, 133, 89, 11, 133, 153, 184, 252, 37, 239, 177, 65, 118, 80, 231, 190, 222, 66, 250, 118, 72, 166, 221, 67, 156, 245, 119, 138, 28, 6, 142, 107, 71, 122, 116, 200, 156, 199, 237, 152, 191, 239, 4, 184, 64, 114, 143, 81, 62, 48, 23, 233, 217, 95, 47, 221, 104, 171, 2, 64, 30, 219, 1, 230, 241, 70, 246, 243, 121, 174, 67, 66, 11, 99, 202, 17, 52, 234, 78, 29, 3, 57, 51, 123, 149, 86, 64, 192, 73, 199, 108, 101, 55, 232, 41, 114, 153, 237, 253, 52, 205, 148, 45, 86, 186, 241, 182, 183, 42, 77, 252, 195, 29, 158, 173, 3, 182, 207, 254, 61, 71, 184, 167, 184]); 4308e41f4b71Sopenharmony_ci let keyPair = genKeyPairByData(pkData, skData); 4309e41f4b71Sopenharmony_ci let inputUpdate: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 4310e41f4b71Sopenharmony_ci let inputVerify: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 4311e41f4b71Sopenharmony_ci // The data is signData.data in Sign(). 4312e41f4b71Sopenharmony_ci let signMessageBlob: cryptoFramework.DataBlob = { data: new Uint8Array([9, 68, 164, 161, 230, 155, 255, 153, 10, 12, 14, 22, 146, 115, 209, 167, 223, 133, 89, 173, 50, 249, 176, 104, 10, 251, 219, 104, 117, 196, 105, 65, 249, 139, 119, 41, 15, 171, 191, 11, 177, 177, 1, 119, 130, 142, 87, 183, 32, 220, 226, 28, 38, 73, 222, 172, 153, 26, 87, 58, 188, 42, 150, 67, 94, 214, 147, 64, 202, 87, 155, 125, 254, 112, 95, 176, 255, 207, 106, 43, 228, 153, 131, 240, 120, 88, 253, 179, 207, 207, 110, 223, 173, 15, 113, 11, 183, 122, 237, 205, 206, 123, 246, 33, 167, 169, 251, 237, 199, 26, 220, 152, 190, 117, 131, 74, 232, 50, 39, 172, 232, 178, 112, 73, 251, 235, 131, 209]) }; 4313e41f4b71Sopenharmony_ci let verifier = cryptoFramework.createVerify('RSA1024|PKCS1|SHA256'); 4314e41f4b71Sopenharmony_ci verifier.initSync(keyPair.pubKey); 4315e41f4b71Sopenharmony_ci verifier.updateSync(inputUpdate); 4316e41f4b71Sopenharmony_ci let res = verifier.verifySync(inputVerify, signMessageBlob); 4317e41f4b71Sopenharmony_ci console.info('verify result: ' + res); 4318e41f4b71Sopenharmony_ci} 4319e41f4b71Sopenharmony_ci``` 4320e41f4b71Sopenharmony_ci 4321e41f4b71Sopenharmony_ci### recover<sup>12+</sup> 4322e41f4b71Sopenharmony_ci 4323e41f4b71Sopenharmony_cirecover(signatureData: DataBlob): Promise\<DataBlob | null> 4324e41f4b71Sopenharmony_ci 4325e41f4b71Sopenharmony_ciRecovers the original data from a signature. This API uses a promise to return the result. 4326e41f4b71Sopenharmony_ci 4327e41f4b71Sopenharmony_ci> **NOTE** 4328e41f4b71Sopenharmony_ci> 4329e41f4b71Sopenharmony_ci> Currently, only RSA is supported. 4330e41f4b71Sopenharmony_ci 4331e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4332e41f4b71Sopenharmony_ci 4333e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4334e41f4b71Sopenharmony_ci 4335e41f4b71Sopenharmony_ci**Parameters** 4336e41f4b71Sopenharmony_ci 4337e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4338e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | ---------- | 4339e41f4b71Sopenharmony_ci| signatureData | [DataBlob](#datablob) | Yes | Signature data. | 4340e41f4b71Sopenharmony_ci 4341e41f4b71Sopenharmony_ci**Return value** 4342e41f4b71Sopenharmony_ci 4343e41f4b71Sopenharmony_ci| Type | Description | 4344e41f4b71Sopenharmony_ci| ----------------- | ------------------------------ | 4345e41f4b71Sopenharmony_ci| Promise\<[DataBlob](#datablob) \| null> | Promise used to return the data restored. | 4346e41f4b71Sopenharmony_ci 4347e41f4b71Sopenharmony_ci**Error codes** 4348e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4349e41f4b71Sopenharmony_ci 4350e41f4b71Sopenharmony_ci| ID | Error Message | 4351e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4352e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4353e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4354e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4355e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4356e41f4b71Sopenharmony_ci 4357e41f4b71Sopenharmony_ci**Example** 4358e41f4b71Sopenharmony_ci 4359e41f4b71Sopenharmony_ci```ts 4360e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 4361e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 4362e41f4b71Sopenharmony_ci 4363e41f4b71Sopenharmony_ciasync function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) { 4364e41f4b71Sopenharmony_ci let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData }; 4365e41f4b71Sopenharmony_ci let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData }; 4366e41f4b71Sopenharmony_ci let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); 4367e41f4b71Sopenharmony_ci let keyPair = await rsaGenerator.convertKey(pubKeyBlob, priKeyBlob); 4368e41f4b71Sopenharmony_ci console.info('convertKey success'); 4369e41f4b71Sopenharmony_ci return keyPair; 4370e41f4b71Sopenharmony_ci} 4371e41f4b71Sopenharmony_ci 4372e41f4b71Sopenharmony_ciasync function recoverByPromise() { 4373e41f4b71Sopenharmony_ci // Key generated based on the key data and input data for signature verification. If the data in verify() is the same as that in sign(), the signature verification is successful. 4374e41f4b71Sopenharmony_ci let pkData = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1]); 4375e41f4b71Sopenharmony_ci let skData = new Uint8Array([48, 130, 2, 120, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 98, 48, 130, 2, 94, 2, 1, 0, 2, 129, 129, 0, 214, 179, 23, 198, 183, 139, 148, 8, 173, 74, 56, 160, 15, 248, 244, 166, 209, 250, 142, 74, 216, 58, 117, 215, 178, 247, 254, 39, 180, 227, 85, 201, 59, 133, 209, 221, 26, 9, 116, 31, 172, 151, 252, 185, 123, 20, 25, 7, 92, 129, 5, 196, 239, 214, 126, 254, 154, 188, 239, 144, 161, 171, 65, 42, 31, 214, 93, 115, 247, 69, 94, 143, 54, 51, 25, 49, 146, 204, 205, 165, 20, 120, 35, 184, 190, 65, 106, 12, 214, 176, 57, 125, 235, 51, 88, 135, 76, 73, 109, 112, 147, 138, 198, 252, 5, 20, 245, 51, 7, 32, 108, 89, 125, 204, 50, 189, 88, 254, 255, 146, 244, 244, 149, 79, 54, 216, 45, 89, 2, 3, 1, 0, 1, 2, 129, 129, 0, 152, 111, 145, 203, 10, 88, 116, 163, 112, 126, 9, 20, 68, 34, 235, 121, 98, 14, 182, 102, 151, 125, 114, 91, 210, 122, 215, 29, 212, 5, 176, 203, 238, 146, 5, 190, 41, 21, 91, 56, 125, 239, 111, 133, 53, 200, 192, 56, 132, 202, 42, 145, 120, 3, 224, 40, 223, 46, 148, 29, 41, 92, 17, 40, 12, 72, 165, 69, 192, 211, 142, 233, 81, 202, 177, 235, 156, 27, 179, 48, 18, 85, 154, 101, 193, 45, 218, 91, 24, 143, 196, 248, 16, 83, 177, 198, 136, 77, 111, 134, 60, 219, 95, 246, 23, 5, 45, 14, 83, 29, 137, 248, 159, 28, 132, 142, 205, 99, 226, 213, 84, 232, 57, 130, 156, 81, 191, 237, 2, 65, 0, 255, 158, 212, 13, 43, 132, 244, 135, 148, 161, 232, 219, 20, 81, 196, 102, 103, 44, 110, 71, 100, 62, 73, 200, 32, 138, 114, 209, 171, 150, 179, 92, 198, 5, 190, 218, 79, 227, 227, 37, 32, 57, 159, 252, 107, 211, 139, 198, 202, 248, 137, 143, 186, 205, 106, 81, 85, 207, 134, 148, 110, 204, 243, 27, 2, 65, 0, 215, 4, 181, 121, 57, 224, 170, 168, 183, 159, 152, 8, 74, 233, 80, 244, 146, 81, 48, 159, 194, 199, 36, 187, 6, 181, 182, 223, 115, 133, 151, 171, 78, 219, 90, 161, 248, 69, 6, 207, 173, 3, 81, 161, 2, 60, 238, 204, 177, 12, 138, 17, 220, 179, 71, 113, 200, 248, 159, 153, 252, 150, 180, 155, 2, 65, 0, 190, 202, 185, 211, 170, 171, 238, 40, 84, 84, 21, 13, 144, 57, 7, 178, 183, 71, 126, 120, 98, 229, 235, 4, 40, 229, 173, 149, 185, 209, 29, 199, 29, 54, 164, 161, 38, 8, 30, 62, 83, 179, 47, 42, 165, 0, 156, 207, 160, 39, 169, 229, 81, 180, 136, 170, 116, 182, 20, 233, 45, 90, 100, 9, 2, 65, 0, 152, 255, 47, 198, 15, 201, 238, 133, 89, 11, 133, 153, 184, 252, 37, 239, 177, 65, 118, 80, 231, 190, 222, 66, 250, 118, 72, 166, 221, 67, 156, 245, 119, 138, 28, 6, 142, 107, 71, 122, 116, 200, 156, 199, 237, 152, 191, 239, 4, 184, 64, 114, 143, 81, 62, 48, 23, 233, 217, 95, 47, 221, 104, 171, 2, 64, 30, 219, 1, 230, 241, 70, 246, 243, 121, 174, 67, 66, 11, 99, 202, 17, 52, 234, 78, 29, 3, 57, 51, 123, 149, 86, 64, 192, 73, 199, 108, 101, 55, 232, 41, 114, 153, 237, 253, 52, 205, 148, 45, 86, 186, 241, 182, 183, 42, 77, 252, 195, 29, 158, 173, 3, 182, 207, 254, 61, 71, 184, 167, 184]); 4376e41f4b71Sopenharmony_ci let keyPair = await genKeyPairByData(pkData, skData); 4377e41f4b71Sopenharmony_ci // The data is signData.data in Sign(). 4378e41f4b71Sopenharmony_ci let signMessageBlob: cryptoFramework.DataBlob = { data: new Uint8Array([9, 68, 164, 161, 230, 155, 255, 153, 10, 12, 14, 22, 146, 115, 209, 167, 223, 133, 89, 173, 50, 249, 176, 104, 10, 251, 219, 104, 117, 196, 105, 65, 249, 139, 119, 41, 15, 171, 191, 11, 177, 177, 1, 119, 130, 142, 87, 183, 32, 220, 226, 28, 38, 73, 222, 172, 153, 26, 87, 58, 188, 42, 150, 67, 94, 214, 147, 64, 202, 87, 155, 125, 254, 112, 95, 176, 255, 207, 106, 43, 228, 153, 131, 240, 120, 88, 253, 179, 207, 207, 110, 223, 173, 15, 113, 11, 183, 122, 237, 205, 206, 123, 246, 33, 167, 169, 251, 237, 199, 26, 220, 152, 190, 117, 131, 74, 232, 50, 39, 172, 232, 178, 112, 73, 251, 235, 131, 209]) }; 4379e41f4b71Sopenharmony_ci let verifier = cryptoFramework.createVerify('RSA1024|PKCS1|SHA256|Recover'); 4380e41f4b71Sopenharmony_ci await verifier.init(keyPair.pubKey); 4381e41f4b71Sopenharmony_ci try { 4382e41f4b71Sopenharmony_ci let rawSignData = await verifier.recover(signMessageBlob); 4383e41f4b71Sopenharmony_ci if (rawSignData != null) { 4384e41f4b71Sopenharmony_ci console.info('[Promise]: recover result: ' + rawSignData.data); 4385e41f4b71Sopenharmony_ci } else { 4386e41f4b71Sopenharmony_ci console.error("[Promise]: get verify recover result fail!"); 4387e41f4b71Sopenharmony_ci } 4388e41f4b71Sopenharmony_ci } catch (error) { 4389e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 4390e41f4b71Sopenharmony_ci console.error(`promise error, ${e.code}, ${e.message}`); 4391e41f4b71Sopenharmony_ci } 4392e41f4b71Sopenharmony_ci} 4393e41f4b71Sopenharmony_ci``` 4394e41f4b71Sopenharmony_ci 4395e41f4b71Sopenharmony_ci### recoverSync<sup>12+</sup> 4396e41f4b71Sopenharmony_ci 4397e41f4b71Sopenharmony_cirecoverSync(signatureData: DataBlob): DataBlob | null 4398e41f4b71Sopenharmony_ci 4399e41f4b71Sopenharmony_ciRecovers the original data from a signature. This API returns the result synchronously. 4400e41f4b71Sopenharmony_ci 4401e41f4b71Sopenharmony_ci> **NOTE** 4402e41f4b71Sopenharmony_ci> 4403e41f4b71Sopenharmony_ci> - Currently, only RSA is supported. 4404e41f4b71Sopenharmony_ci 4405e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4406e41f4b71Sopenharmony_ci 4407e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4408e41f4b71Sopenharmony_ci 4409e41f4b71Sopenharmony_ci**Parameters** 4410e41f4b71Sopenharmony_ci 4411e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4412e41f4b71Sopenharmony_ci| ------------- | -------- | ---- | ---------- | 4413e41f4b71Sopenharmony_ci| signatureData | [DataBlob](#datablob) | Yes | Signature data. | 4414e41f4b71Sopenharmony_ci 4415e41f4b71Sopenharmony_ci**Return value** 4416e41f4b71Sopenharmony_ci 4417e41f4b71Sopenharmony_ci| Type | Description | 4418e41f4b71Sopenharmony_ci| ----------------- | ------------------------------ | 4419e41f4b71Sopenharmony_ci| [DataBlob](#datablob) \| null | Data restored. | 4420e41f4b71Sopenharmony_ci 4421e41f4b71Sopenharmony_ci**Error codes** 4422e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4423e41f4b71Sopenharmony_ci 4424e41f4b71Sopenharmony_ci| ID | Error Message | 4425e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4426e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4427e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4428e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4429e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4430e41f4b71Sopenharmony_ci 4431e41f4b71Sopenharmony_ci### setVerifySpec<sup>10+</sup> 4432e41f4b71Sopenharmony_ci 4433e41f4b71Sopenharmony_cisetVerifySpec(itemType: SignSpecItem, itemValue: number): void 4434e41f4b71Sopenharmony_ci 4435e41f4b71Sopenharmony_cisetVerifySpec(itemType: SignSpecItem, itemValue: number \| Uint8Array): void 4436e41f4b71Sopenharmony_ci 4437e41f4b71Sopenharmony_ciSets signature verification specifications. You can use this API to set signature verification parameters that cannot be set by [createVerify](#cryptoframeworkcreateverify). 4438e41f4b71Sopenharmony_ci 4439e41f4b71Sopenharmony_ciCurrently, only RSA and SM2 are supported. Since API version 11, SM2 signing parameters can be set. 4440e41f4b71Sopenharmony_ci 4441e41f4b71Sopenharmony_ciThe parameters for signature verification must be the same as those for signing. 4442e41f4b71Sopenharmony_ci 4443e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4444e41f4b71Sopenharmony_ci 4445e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4446e41f4b71Sopenharmony_ci 4447e41f4b71Sopenharmony_ci**Parameters** 4448e41f4b71Sopenharmony_ci 4449e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4450e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------- | 4451e41f4b71Sopenharmony_ci| itemType | [SignSpecItem](#signspecitem10) | Yes | Signature verification parameter to set. | 4452e41f4b71Sopenharmony_ci| itemValue | number \| Uint8Array<sup>11+</sup> | Yes | Value of the signature verification parameter to set. | 4453e41f4b71Sopenharmony_ci 4454e41f4b71Sopenharmony_ci**Error codes** 4455e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4456e41f4b71Sopenharmony_ci 4457e41f4b71Sopenharmony_ci| ID | Error Message | 4458e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4459e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4460e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 4461e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4462e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4463e41f4b71Sopenharmony_ci 4464e41f4b71Sopenharmony_ci**Example** 4465e41f4b71Sopenharmony_ci 4466e41f4b71Sopenharmony_ci<!--code_no_check--> 4467e41f4b71Sopenharmony_ci```ts 4468e41f4b71Sopenharmony_cilet verifyer: cryptoFramework.Verify; // The process of generating the Verify instance is omitted here. 4469e41f4b71Sopenharmony_cilet setN = 20; 4470e41f4b71Sopenharmony_civerifyer.setVerifySpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, setN); 4471e41f4b71Sopenharmony_ci``` 4472e41f4b71Sopenharmony_ci 4473e41f4b71Sopenharmony_ci### getVerifySpec<sup>10+</sup> 4474e41f4b71Sopenharmony_ci 4475e41f4b71Sopenharmony_cigetVerifySpec(itemType: SignSpecItem): string | number 4476e41f4b71Sopenharmony_ci 4477e41f4b71Sopenharmony_ciObtains signature verification specifications. Currently, only RSA is supported. 4478e41f4b71Sopenharmony_ci 4479e41f4b71Sopenharmony_ciThe parameters for signature verification must be the same as those for signing. 4480e41f4b71Sopenharmony_ci 4481e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4482e41f4b71Sopenharmony_ci 4483e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4484e41f4b71Sopenharmony_ci 4485e41f4b71Sopenharmony_ci**Parameters** 4486e41f4b71Sopenharmony_ci 4487e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4488e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 4489e41f4b71Sopenharmony_ci| itemType | [SignSpecItem](#signspecitem10) | Yes | Signature verification parameter to obtain. | 4490e41f4b71Sopenharmony_ci 4491e41f4b71Sopenharmony_ci**Return value** 4492e41f4b71Sopenharmony_ci 4493e41f4b71Sopenharmony_ci| Type | Description | 4494e41f4b71Sopenharmony_ci| -------------- | ----------- | 4495e41f4b71Sopenharmony_ci| string \| number | Returns the value of the parameter obtained. | 4496e41f4b71Sopenharmony_ci 4497e41f4b71Sopenharmony_ci**Error codes** 4498e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4499e41f4b71Sopenharmony_ci 4500e41f4b71Sopenharmony_ci| ID | Error Message | 4501e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4502e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4503e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 4504e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4505e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4506e41f4b71Sopenharmony_ci 4507e41f4b71Sopenharmony_ci**Example** 4508e41f4b71Sopenharmony_ci 4509e41f4b71Sopenharmony_ci<!--code_no_check--> 4510e41f4b71Sopenharmony_ci```ts 4511e41f4b71Sopenharmony_cilet verifyer: cryptoFramework.Verify; // The process of generating the Verify instance is omitted here. 4512e41f4b71Sopenharmony_cilet saltLen = verifyer.getVerifySpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM); 4513e41f4b71Sopenharmony_ci``` 4514e41f4b71Sopenharmony_ci 4515e41f4b71Sopenharmony_ci## cryptoFramework.createKeyAgreement 4516e41f4b71Sopenharmony_ci 4517e41f4b71Sopenharmony_cicreateKeyAgreement(algName: string): KeyAgreement 4518e41f4b71Sopenharmony_ci 4519e41f4b71Sopenharmony_ciCreates a **KeyAgreement** instance. 4520e41f4b71Sopenharmony_ci 4521e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Key Agreement Overview and Algorithm Specifications](../../security/CryptoArchitectureKit/crypto-key-agreement-overview.md). 4522e41f4b71Sopenharmony_ci 4523e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4524e41f4b71Sopenharmony_ci 4525e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4526e41f4b71Sopenharmony_ci 4527e41f4b71Sopenharmony_ci**Parameters** 4528e41f4b71Sopenharmony_ci 4529e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4530e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 4531e41f4b71Sopenharmony_ci| algName | string | Yes | Key agreement algorithm to use. In addition to ECC, X25519 and DH are supported since API version 11. | 4532e41f4b71Sopenharmony_ci 4533e41f4b71Sopenharmony_ci**Return value** 4534e41f4b71Sopenharmony_ci 4535e41f4b71Sopenharmony_ci| Type | Description | 4536e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------ | 4537e41f4b71Sopenharmony_ci| KeyAgreement | Returns the **KeyAgreement** instance created. | 4538e41f4b71Sopenharmony_ci 4539e41f4b71Sopenharmony_ci**Error codes** 4540e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4541e41f4b71Sopenharmony_ci 4542e41f4b71Sopenharmony_ci| ID | Error Message | 4543e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4544e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4545e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 4546e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4547e41f4b71Sopenharmony_ci 4548e41f4b71Sopenharmony_ci**Example** 4549e41f4b71Sopenharmony_ci 4550e41f4b71Sopenharmony_ci```ts 4551e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 4552e41f4b71Sopenharmony_ci 4553e41f4b71Sopenharmony_cilet keyAgreement = cryptoFramework.createKeyAgreement('ECC256'); 4554e41f4b71Sopenharmony_ci``` 4555e41f4b71Sopenharmony_ci 4556e41f4b71Sopenharmony_ci## KeyAgreement 4557e41f4b71Sopenharmony_ci 4558e41f4b71Sopenharmony_ciProvides APIs for key agreement operations. Before using any API of the **KeyAgreement** class, you must create a **KeyAgreement** instance by using [createKeyAgreement(algName: string): KeyAgreement](#cryptoframeworkcreatekeyagreement). 4559e41f4b71Sopenharmony_ci 4560e41f4b71Sopenharmony_ci### Attributes 4561e41f4b71Sopenharmony_ci 4562e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4563e41f4b71Sopenharmony_ci 4564e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4565e41f4b71Sopenharmony_ci 4566e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 4567e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ---------------------------- | 4568e41f4b71Sopenharmony_ci| algName | string | Yes | No | Algorithm used for key agreement. | 4569e41f4b71Sopenharmony_ci 4570e41f4b71Sopenharmony_ci### generateSecret 4571e41f4b71Sopenharmony_ci 4572e41f4b71Sopenharmony_cigenerateSecret(priKey: PriKey, pubKey: PubKey, callback: AsyncCallback\<DataBlob>): void 4573e41f4b71Sopenharmony_ci 4574e41f4b71Sopenharmony_ciPerforms key agreement based on a private key and a public key. This API uses an asynchronous callback to return the shared secret generated. 4575e41f4b71Sopenharmony_ci 4576e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4577e41f4b71Sopenharmony_ci 4578e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4579e41f4b71Sopenharmony_ci 4580e41f4b71Sopenharmony_ci**Parameters** 4581e41f4b71Sopenharmony_ci 4582e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4583e41f4b71Sopenharmony_ci| -------- | ------------------------ | ---- | ---------------------- | 4584e41f4b71Sopenharmony_ci| priKey | [PriKey](#prikey) | Yes | Private key used for key agreement. | 4585e41f4b71Sopenharmony_ci| pubKey | [PubKey](#pubkey) | Yes | Public key used for key agreement. | 4586e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[DataBlob](#datablob)> | Yes | Callback invoked to return the shared secret. | 4587e41f4b71Sopenharmony_ci 4588e41f4b71Sopenharmony_ci**Error codes** 4589e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4590e41f4b71Sopenharmony_ci 4591e41f4b71Sopenharmony_ci| ID | Error Message | 4592e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4593e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4594e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4595e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4596e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4597e41f4b71Sopenharmony_ci 4598e41f4b71Sopenharmony_ci### generateSecret 4599e41f4b71Sopenharmony_ci 4600e41f4b71Sopenharmony_cigenerateSecret(priKey: PriKey, pubKey: PubKey): Promise\<DataBlob> 4601e41f4b71Sopenharmony_ci 4602e41f4b71Sopenharmony_ciPerforms key agreement based on a private key and a public key. This API uses a promise to return the shared secret generated. 4603e41f4b71Sopenharmony_ci 4604e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4605e41f4b71Sopenharmony_ci 4606e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4607e41f4b71Sopenharmony_ci 4608e41f4b71Sopenharmony_ci**Parameters** 4609e41f4b71Sopenharmony_ci 4610e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4611e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------- | 4612e41f4b71Sopenharmony_ci| priKey | [PriKey](#prikey) | Yes | Private key used for key agreement. | 4613e41f4b71Sopenharmony_ci| pubKey | [PubKey](#pubkey) | Yes | Public key used for key agreement. | 4614e41f4b71Sopenharmony_ci 4615e41f4b71Sopenharmony_ci**Return value** 4616e41f4b71Sopenharmony_ci 4617e41f4b71Sopenharmony_ci| Type | Description | 4618e41f4b71Sopenharmony_ci| ------------------ | -------- | 4619e41f4b71Sopenharmony_ci| Promise\<[DataBlob](#datablob)> | Promise used to return the shared secret. | 4620e41f4b71Sopenharmony_ci 4621e41f4b71Sopenharmony_ci**Error codes** 4622e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4623e41f4b71Sopenharmony_ci 4624e41f4b71Sopenharmony_ci| ID | Error Message | 4625e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4626e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4627e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4628e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4629e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4630e41f4b71Sopenharmony_ci 4631e41f4b71Sopenharmony_ci### generateSecretSync<sup>12+</sup> 4632e41f4b71Sopenharmony_ci 4633e41f4b71Sopenharmony_cigenerateSecretSync(priKey: PriKey, pubKey: PubKey): DataBlob 4634e41f4b71Sopenharmony_ci 4635e41f4b71Sopenharmony_ciPerforms key agreement based on a private key and a public key. This API returns the shared secret generated synchronously. 4636e41f4b71Sopenharmony_ci 4637e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4638e41f4b71Sopenharmony_ci 4639e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4640e41f4b71Sopenharmony_ci 4641e41f4b71Sopenharmony_ci**Parameters** 4642e41f4b71Sopenharmony_ci 4643e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4644e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------- | 4645e41f4b71Sopenharmony_ci| priKey | [PriKey](#prikey) | Yes | Private key used for key agreement. | 4646e41f4b71Sopenharmony_ci| pubKey | [PubKey](#pubkey) | Yes | Public key used for key agreement. | 4647e41f4b71Sopenharmony_ci 4648e41f4b71Sopenharmony_ci**Return value** 4649e41f4b71Sopenharmony_ci 4650e41f4b71Sopenharmony_ci| Type | Description | 4651e41f4b71Sopenharmony_ci| ------------------ | -------- | 4652e41f4b71Sopenharmony_ci|[DataBlob](#datablob) | Promise used to return the shared secret. | 4653e41f4b71Sopenharmony_ci 4654e41f4b71Sopenharmony_ci**Error codes** 4655e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4656e41f4b71Sopenharmony_ci 4657e41f4b71Sopenharmony_ci| ID | Error Message | 4658e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4659e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4660e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4661e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4662e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4663e41f4b71Sopenharmony_ci 4664e41f4b71Sopenharmony_ci**Example (using the callback-based API)** 4665e41f4b71Sopenharmony_ci 4666e41f4b71Sopenharmony_ci<!--code_no_check--> 4667e41f4b71Sopenharmony_ci```ts 4668e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4669e41f4b71Sopenharmony_ci 4670e41f4b71Sopenharmony_cilet globalKeyPair: cryptoFramework.KeyPair; // globalKeyPair is an asymmetric key object generated by the asymmetric key generator. The generation process is omitted here. 4671e41f4b71Sopenharmony_cilet keyAgreement = cryptoFramework.createKeyAgreement('ECC256'); 4672e41f4b71Sopenharmony_cikeyAgreement.generateSecret(globalKeyPair.priKey, globalKeyPair.pubKey, (err, secret) => { 4673e41f4b71Sopenharmony_ci if (err) { 4674e41f4b71Sopenharmony_ci console.error("keyAgreement error."); 4675e41f4b71Sopenharmony_ci return; 4676e41f4b71Sopenharmony_ci } 4677e41f4b71Sopenharmony_ci console.info('keyAgreement output is ' + secret.data); 4678e41f4b71Sopenharmony_ci}); 4679e41f4b71Sopenharmony_ci``` 4680e41f4b71Sopenharmony_ci 4681e41f4b71Sopenharmony_ci**Example (using the promise-based API)** 4682e41f4b71Sopenharmony_ci 4683e41f4b71Sopenharmony_ci<!--code_no_check--> 4684e41f4b71Sopenharmony_ci```ts 4685e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4686e41f4b71Sopenharmony_ci 4687e41f4b71Sopenharmony_cilet globalKeyPair: cryptoFramework.KeyPair; // globalKeyPair is an asymmetric key object generated by the asymmetric key generator. The generation process is omitted here. 4688e41f4b71Sopenharmony_cilet keyAgreement = cryptoFramework.createKeyAgreement('ECC256'); 4689e41f4b71Sopenharmony_cilet keyAgreementPromise = keyAgreement.generateSecret(globalKeyPair.priKey, globalKeyPair.pubKey); 4690e41f4b71Sopenharmony_cikeyAgreementPromise.then(secret => { 4691e41f4b71Sopenharmony_ci console.info('keyAgreement output is ' + secret.data); 4692e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 4693e41f4b71Sopenharmony_ci console.error("keyAgreement error."); 4694e41f4b71Sopenharmony_ci}); 4695e41f4b71Sopenharmony_ci``` 4696e41f4b71Sopenharmony_ci 4697e41f4b71Sopenharmony_ci**Example (using the sync API)** 4698e41f4b71Sopenharmony_ci 4699e41f4b71Sopenharmony_ci<!--code_no_check--> 4700e41f4b71Sopenharmony_ci```ts 4701e41f4b71Sopenharmony_cilet asyGenerator = cryptoFramework.CreateAsyKeyGenerator("ECC256"); 4702e41f4b71Sopenharmony_cilet globalKeyPair = asyGenerator.generateKeyPairSync(); 4703e41f4b71Sopenharmony_cilet keyAgreement = cryptoFramework.createKeyAgreement('ECC256'); 4704e41f4b71Sopenharmony_cilet secret = keyAgreement.generateSecretSync(globalKeyPair.priKey, globalKeyPair.pubKey); 4705e41f4b71Sopenharmony_ciconsole.info("[Sync]keyAgreement output is " + secret.data); 4706e41f4b71Sopenharmony_ci``` 4707e41f4b71Sopenharmony_ci 4708e41f4b71Sopenharmony_ci## cryptoFramework.createMd 4709e41f4b71Sopenharmony_ci 4710e41f4b71Sopenharmony_cicreateMd(algName: string): Md 4711e41f4b71Sopenharmony_ci 4712e41f4b71Sopenharmony_ciCreates an **Md** instance for MD operations. 4713e41f4b71Sopenharmony_ci 4714e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Supported Algorithms and Specifications](../../security/CryptoArchitectureKit/crypto-generate-message-digest.md#supported-algorithms-and-specifications). 4715e41f4b71Sopenharmony_ci 4716e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4717e41f4b71Sopenharmony_ci 4718e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4719e41f4b71Sopenharmony_ci 4720e41f4b71Sopenharmony_ci**Parameters** 4721e41f4b71Sopenharmony_ci 4722e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4723e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 4724e41f4b71Sopenharmony_ci| algName | string | Yes | MD algorithm to use. For details about the supported algorithms, see [Supported Algorithms and Specifications](../../security/CryptoArchitectureKit/crypto-generate-message-digest.md#supported-algorithms-and-specifications). | 4725e41f4b71Sopenharmony_ci 4726e41f4b71Sopenharmony_ci**Return value** 4727e41f4b71Sopenharmony_ci 4728e41f4b71Sopenharmony_ci| Type | Description | 4729e41f4b71Sopenharmony_ci| ---- | --------------------------------------- | 4730e41f4b71Sopenharmony_ci| Md | Returns the [Md](#md) instance created. | 4731e41f4b71Sopenharmony_ci 4732e41f4b71Sopenharmony_ci**Error codes** 4733e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4734e41f4b71Sopenharmony_ci 4735e41f4b71Sopenharmony_ci| ID | Error Message | 4736e41f4b71Sopenharmony_ci| -------- | ------------------ | 4737e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4738e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4739e41f4b71Sopenharmony_ci 4740e41f4b71Sopenharmony_ci**Example** 4741e41f4b71Sopenharmony_ci 4742e41f4b71Sopenharmony_ci```ts 4743e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 4744e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4745e41f4b71Sopenharmony_ci 4746e41f4b71Sopenharmony_citry { 4747e41f4b71Sopenharmony_ci // Set algName based on the algorithm supported. 4748e41f4b71Sopenharmony_ci let md = cryptoFramework.createMd('SHA256'); 4749e41f4b71Sopenharmony_ci} catch (error) { 4750e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 4751e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 4752e41f4b71Sopenharmony_ci} 4753e41f4b71Sopenharmony_ci``` 4754e41f4b71Sopenharmony_ci 4755e41f4b71Sopenharmony_ci## Md 4756e41f4b71Sopenharmony_ci 4757e41f4b71Sopenharmony_ciProvides APIs for MD operations. Before using any API of the **Md** class, you must create an **Md** instance by using [createMd](#cryptoframeworkcreatemd). 4758e41f4b71Sopenharmony_ci 4759e41f4b71Sopenharmony_ci### Attributes 4760e41f4b71Sopenharmony_ci 4761e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4762e41f4b71Sopenharmony_ci 4763e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4764e41f4b71Sopenharmony_ci 4765e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 4766e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ---------------------- | 4767e41f4b71Sopenharmony_ci| algName | string | Yes | No | Digest algorithm. | 4768e41f4b71Sopenharmony_ci 4769e41f4b71Sopenharmony_ci### update 4770e41f4b71Sopenharmony_ci 4771e41f4b71Sopenharmony_ciupdate(input: DataBlob, callback: AsyncCallback\<void>): void 4772e41f4b71Sopenharmony_ci 4773e41f4b71Sopenharmony_ciUpdates the message for MD operations. This API uses an asynchronous callback to return the result. **update** must be used with **digest** together. **digest** is mandatory, and **update** is optional. 4774e41f4b71Sopenharmony_ci 4775e41f4b71Sopenharmony_ci> **NOTE** 4776e41f4b71Sopenharmony_ci> 4777e41f4b71Sopenharmony_ci> For details about the code for calling **update** multiple times in an MD operation, see [MD (Passing In Data by Segment)](../../security/CryptoArchitectureKit/crypto-generate-message-digest.md#md-passing-in-data-by-segment). 4778e41f4b71Sopenharmony_ci 4779e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4780e41f4b71Sopenharmony_ci 4781e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4782e41f4b71Sopenharmony_ci 4783e41f4b71Sopenharmony_ci**Parameters** 4784e41f4b71Sopenharmony_ci 4785e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4786e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ------------ | 4787e41f4b71Sopenharmony_ci| input | [DataBlob](#datablob) | Yes | Data to pass in. | 4788e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 4789e41f4b71Sopenharmony_ci 4790e41f4b71Sopenharmony_ci**Error codes** 4791e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4792e41f4b71Sopenharmony_ci 4793e41f4b71Sopenharmony_ci| ID | Error Message | 4794e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4795e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4796e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4797e41f4b71Sopenharmony_ci 4798e41f4b71Sopenharmony_ci### update 4799e41f4b71Sopenharmony_ci 4800e41f4b71Sopenharmony_ciupdate(input: DataBlob): Promise\<void> 4801e41f4b71Sopenharmony_ci 4802e41f4b71Sopenharmony_ciUpdates the message for MD operations. This API uses a promise to return the result. **update** must be used with **digest** together. **digest** is mandatory, and **update** is optional. 4803e41f4b71Sopenharmony_ci 4804e41f4b71Sopenharmony_ci> **NOTE** 4805e41f4b71Sopenharmony_ci> 4806e41f4b71Sopenharmony_ci> For details about the code for calling **update** multiple times in an MD operation, see [MD (Passing In Data by Segment)](../../security/CryptoArchitectureKit/crypto-generate-message-digest.md#md-passing-in-data-by-segment). 4807e41f4b71Sopenharmony_ci 4808e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4809e41f4b71Sopenharmony_ci 4810e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4811e41f4b71Sopenharmony_ci 4812e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4813e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------ | 4814e41f4b71Sopenharmony_ci| input | [DataBlob](#datablob) | Yes | Data to pass in. | 4815e41f4b71Sopenharmony_ci 4816e41f4b71Sopenharmony_ci**Return value** 4817e41f4b71Sopenharmony_ci 4818e41f4b71Sopenharmony_ci| Type | Description | 4819e41f4b71Sopenharmony_ci| -------------- | ------------- | 4820e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value. | 4821e41f4b71Sopenharmony_ci 4822e41f4b71Sopenharmony_ci**Error codes** 4823e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4824e41f4b71Sopenharmony_ci 4825e41f4b71Sopenharmony_ci| ID | Error Message | 4826e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4827e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 4828e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4829e41f4b71Sopenharmony_ci 4830e41f4b71Sopenharmony_ci### updateSync<sup>12+</sup> 4831e41f4b71Sopenharmony_ci 4832e41f4b71Sopenharmony_ciupdateSync(input: DataBlob): void 4833e41f4b71Sopenharmony_ci 4834e41f4b71Sopenharmony_ciUpdates the message for MD operations. This API returns the result synchronously. **updateSync** must be used with **digestSync** together. **digestSync** is mandatory, and **updateSync** is optional. 4835e41f4b71Sopenharmony_ci 4836e41f4b71Sopenharmony_ci> **NOTE** 4837e41f4b71Sopenharmony_ci> 4838e41f4b71Sopenharmony_ci> For details about the code for calling **updateSync** multiple times in an MD operation, see [MD (Passing In Data by Segment)](../../security/CryptoArchitectureKit/crypto-generate-message-digest.md#md-passing-in-data-by-segment). 4839e41f4b71Sopenharmony_ci 4840e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4841e41f4b71Sopenharmony_ci 4842e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4843e41f4b71Sopenharmony_ci 4844e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4845e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------ | 4846e41f4b71Sopenharmony_ci| input | [DataBlob](#datablob) | Yes | Data to pass in. | 4847e41f4b71Sopenharmony_ci 4848e41f4b71Sopenharmony_ci**Return value** 4849e41f4b71Sopenharmony_ci 4850e41f4b71Sopenharmony_ci| Type | Description | 4851e41f4b71Sopenharmony_ci| -------------- | ------------- | 4852e41f4b71Sopenharmony_ci| void | No value is returned. | 4853e41f4b71Sopenharmony_ci 4854e41f4b71Sopenharmony_ci**Error codes** 4855e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4856e41f4b71Sopenharmony_ci 4857e41f4b71Sopenharmony_ci| ID | Error Message | 4858e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4859e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 4860e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4861e41f4b71Sopenharmony_ci 4862e41f4b71Sopenharmony_ci### digest 4863e41f4b71Sopenharmony_ci 4864e41f4b71Sopenharmony_cidigest(callback: AsyncCallback\<DataBlob>): void 4865e41f4b71Sopenharmony_ci 4866e41f4b71Sopenharmony_ciGenerates an MD. This API uses an asynchronous callback to return the result. 4867e41f4b71Sopenharmony_ci 4868e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4869e41f4b71Sopenharmony_ci 4870e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4871e41f4b71Sopenharmony_ci 4872e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4873e41f4b71Sopenharmony_ci| -------- | ------------------------ | ---- | ---------- | 4874e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[DataBlob](#datablob)> | Yes | Callback invoked to return a **DataBlob** object. | 4875e41f4b71Sopenharmony_ci 4876e41f4b71Sopenharmony_ci**Error codes** 4877e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4878e41f4b71Sopenharmony_ci 4879e41f4b71Sopenharmony_ci| ID | Error Message | 4880e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4881e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4882e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4883e41f4b71Sopenharmony_ci 4884e41f4b71Sopenharmony_ci**Example** 4885e41f4b71Sopenharmony_ci 4886e41f4b71Sopenharmony_ci```ts 4887e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 4888e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 4889e41f4b71Sopenharmony_ci 4890e41f4b71Sopenharmony_cifunction mdByCallback() { 4891e41f4b71Sopenharmony_ci let md = cryptoFramework.createMd('SHA256'); 4892e41f4b71Sopenharmony_ci md.update({ data: new Uint8Array(buffer.from("mdTestMessage", 'utf-8').buffer) }, (err,) => { 4893e41f4b71Sopenharmony_ci md.digest((err, digestOutput) => { 4894e41f4b71Sopenharmony_ci console.info('[Callback]: MD result: ' + digestOutput.data); 4895e41f4b71Sopenharmony_ci console.info('[Callback]: MD len: ' + md.getMdLength()); 4896e41f4b71Sopenharmony_ci }); 4897e41f4b71Sopenharmony_ci }); 4898e41f4b71Sopenharmony_ci} 4899e41f4b71Sopenharmony_ci``` 4900e41f4b71Sopenharmony_ci 4901e41f4b71Sopenharmony_ci### digest 4902e41f4b71Sopenharmony_ci 4903e41f4b71Sopenharmony_cidigest(): Promise\<DataBlob> 4904e41f4b71Sopenharmony_ci 4905e41f4b71Sopenharmony_ciGenerates an MD. This API uses a promise to return the result. 4906e41f4b71Sopenharmony_ci 4907e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4908e41f4b71Sopenharmony_ci 4909e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4910e41f4b71Sopenharmony_ci 4911e41f4b71Sopenharmony_ci**Return value** 4912e41f4b71Sopenharmony_ci 4913e41f4b71Sopenharmony_ci| Type | Description | 4914e41f4b71Sopenharmony_ci| ------------------ | ----------- | 4915e41f4b71Sopenharmony_ci| Promise\<[DataBlob](#datablob)> | Promise used to return the result. | 4916e41f4b71Sopenharmony_ci 4917e41f4b71Sopenharmony_ci**Error codes** 4918e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4919e41f4b71Sopenharmony_ci 4920e41f4b71Sopenharmony_ci| ID | Error Message | 4921e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4922e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4923e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4924e41f4b71Sopenharmony_ci 4925e41f4b71Sopenharmony_ci**Example** 4926e41f4b71Sopenharmony_ci 4927e41f4b71Sopenharmony_ci```ts 4928e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 4929e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 4930e41f4b71Sopenharmony_ci 4931e41f4b71Sopenharmony_ciasync function mdByPromise() { 4932e41f4b71Sopenharmony_ci let md = cryptoFramework.createMd('SHA256'); 4933e41f4b71Sopenharmony_ci await md.update({ data: new Uint8Array(buffer.from("mdTestMessage", 'utf-8').buffer) }); 4934e41f4b71Sopenharmony_ci let mdOutput = await md.digest(); 4935e41f4b71Sopenharmony_ci console.info('[Promise]: MD result: ' + mdOutput.data); 4936e41f4b71Sopenharmony_ci console.info('[Promise]: MD len: ' + md.getMdLength()); 4937e41f4b71Sopenharmony_ci} 4938e41f4b71Sopenharmony_ci``` 4939e41f4b71Sopenharmony_ci 4940e41f4b71Sopenharmony_ci### digestSync<sup>12+</sup> 4941e41f4b71Sopenharmony_ci 4942e41f4b71Sopenharmony_cidigestSync(): DataBlob 4943e41f4b71Sopenharmony_ci 4944e41f4b71Sopenharmony_ciGenerates an MD. This API returns the result synchronously. 4945e41f4b71Sopenharmony_ci 4946e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4947e41f4b71Sopenharmony_ci 4948e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4949e41f4b71Sopenharmony_ci 4950e41f4b71Sopenharmony_ci**Return value** 4951e41f4b71Sopenharmony_ci 4952e41f4b71Sopenharmony_ci| Type | Description | 4953e41f4b71Sopenharmony_ci| ------------------ | ----------- | 4954e41f4b71Sopenharmony_ci| [DataBlob](#datablob) | MD generated. | 4955e41f4b71Sopenharmony_ci 4956e41f4b71Sopenharmony_ci**Error codes** 4957e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4958e41f4b71Sopenharmony_ci 4959e41f4b71Sopenharmony_ci| ID | Error Message | 4960e41f4b71Sopenharmony_ci| -------- | ---------------------- | 4961e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 4962e41f4b71Sopenharmony_ci| 17620001 | memory error. | 4963e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 4964e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 4965e41f4b71Sopenharmony_ci 4966e41f4b71Sopenharmony_ci**Example** 4967e41f4b71Sopenharmony_ci 4968e41f4b71Sopenharmony_ci```ts 4969e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 4970e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 4971e41f4b71Sopenharmony_ci 4972e41f4b71Sopenharmony_ciasync function mdBySync() { 4973e41f4b71Sopenharmony_ci let md = cryptoFramework.createMd('SHA256'); 4974e41f4b71Sopenharmony_ci md.updateSync({ data: new Uint8Array(buffer.from("mdTestMessage", 'utf-8').buffer) }); 4975e41f4b71Sopenharmony_ci let mdOutput = md.digestSync(); 4976e41f4b71Sopenharmony_ci console.info('[Sync]: MD result: ' + mdOutput.data); 4977e41f4b71Sopenharmony_ci console.info('[Sync]: MD len: ' + md.getMdLength()); 4978e41f4b71Sopenharmony_ci} 4979e41f4b71Sopenharmony_ci``` 4980e41f4b71Sopenharmony_ci 4981e41f4b71Sopenharmony_ci### getMdLength 4982e41f4b71Sopenharmony_ci 4983e41f4b71Sopenharmony_cigetMdLength(): number 4984e41f4b71Sopenharmony_ci 4985e41f4b71Sopenharmony_ciObtains the MD length, in bytes. 4986e41f4b71Sopenharmony_ci 4987e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4988e41f4b71Sopenharmony_ci 4989e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 4990e41f4b71Sopenharmony_ci 4991e41f4b71Sopenharmony_ci**Return value** 4992e41f4b71Sopenharmony_ci 4993e41f4b71Sopenharmony_ci| Type | Description | 4994e41f4b71Sopenharmony_ci| ------ | -------------------------- | 4995e41f4b71Sopenharmony_ci| number | MD length obtained. | 4996e41f4b71Sopenharmony_ci 4997e41f4b71Sopenharmony_ci**Error codes** 4998e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 4999e41f4b71Sopenharmony_ci 5000e41f4b71Sopenharmony_ci| ID | Error Message | 5001e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5002e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5003e41f4b71Sopenharmony_ci 5004e41f4b71Sopenharmony_ci**Example** 5005e41f4b71Sopenharmony_ci 5006e41f4b71Sopenharmony_ci```ts 5007e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5008e41f4b71Sopenharmony_ci 5009e41f4b71Sopenharmony_cifunction getLength() { 5010e41f4b71Sopenharmony_ci let md = cryptoFramework.createMd('SHA256'); 5011e41f4b71Sopenharmony_ci console.info('[Promise]: MD len: ' + md.getMdLength()); 5012e41f4b71Sopenharmony_ci} 5013e41f4b71Sopenharmony_ci``` 5014e41f4b71Sopenharmony_ci 5015e41f4b71Sopenharmony_ci## cryptoFramework.createMac 5016e41f4b71Sopenharmony_ci 5017e41f4b71Sopenharmony_cicreateMac(algName: string): Mac 5018e41f4b71Sopenharmony_ci 5019e41f4b71Sopenharmony_ciCreates a **Mac** instance for message authentication code (MAC) operations. 5020e41f4b71Sopenharmony_ci 5021e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Supported Algorithms and Specifications](../../security/CryptoArchitectureKit/crypto-compute-mac.md#supported-algorithms-and-specifications). 5022e41f4b71Sopenharmony_ci 5023e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5024e41f4b71Sopenharmony_ci 5025e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5026e41f4b71Sopenharmony_ci 5027e41f4b71Sopenharmony_ci**Parameters** 5028e41f4b71Sopenharmony_ci 5029e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5030e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 5031e41f4b71Sopenharmony_ci| algName | string | Yes | MD algorithm to use. For details about the supported algorithms, see [Supported Algorithms and Specifications](../../security/CryptoArchitectureKit/crypto-compute-mac.md#supported-algorithms-and-specifications). | 5032e41f4b71Sopenharmony_ci 5033e41f4b71Sopenharmony_ci**Return value** 5034e41f4b71Sopenharmony_ci 5035e41f4b71Sopenharmony_ci| Type | Description | 5036e41f4b71Sopenharmony_ci| ---- | ----------------------------------------- | 5037e41f4b71Sopenharmony_ci| Mac | Returns the [Mac](#mac) instance created. | 5038e41f4b71Sopenharmony_ci 5039e41f4b71Sopenharmony_ci**Error codes** 5040e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5041e41f4b71Sopenharmony_ci 5042e41f4b71Sopenharmony_ci| ID | Error Message | 5043e41f4b71Sopenharmony_ci| -------- | ------------------ | 5044e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5045e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5046e41f4b71Sopenharmony_ci 5047e41f4b71Sopenharmony_ci**Example** 5048e41f4b71Sopenharmony_ci 5049e41f4b71Sopenharmony_ci```ts 5050e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5051e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5052e41f4b71Sopenharmony_ci 5053e41f4b71Sopenharmony_citry { 5054e41f4b71Sopenharmony_ci // Set algName based on the algorithm supported. 5055e41f4b71Sopenharmony_ci let mac = cryptoFramework.createMac('SHA256'); 5056e41f4b71Sopenharmony_ci} catch (error) { 5057e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 5058e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 5059e41f4b71Sopenharmony_ci} 5060e41f4b71Sopenharmony_ci``` 5061e41f4b71Sopenharmony_ci 5062e41f4b71Sopenharmony_ci## Mac 5063e41f4b71Sopenharmony_ci 5064e41f4b71Sopenharmony_ciProvides APIs for MAC operations. Before using any API of the **Mac** class, you must create a **Mac** instance by using [createMac](#cryptoframeworkcreatemac). 5065e41f4b71Sopenharmony_ci 5066e41f4b71Sopenharmony_ci### Attributes 5067e41f4b71Sopenharmony_ci 5068e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5069e41f4b71Sopenharmony_ci 5070e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5071e41f4b71Sopenharmony_ci 5072e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 5073e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ---------------------- | 5074e41f4b71Sopenharmony_ci| algName | string | Yes | No | Digest algorithm. | 5075e41f4b71Sopenharmony_ci 5076e41f4b71Sopenharmony_ci### init 5077e41f4b71Sopenharmony_ci 5078e41f4b71Sopenharmony_ciinit(key: SymKey, callback: AsyncCallback\<void>): void 5079e41f4b71Sopenharmony_ci 5080e41f4b71Sopenharmony_ciInitializes the MAC computation with a symmetric key. This API uses an asynchronous callback to return the result. **init**, **update**, and **doFinal** must be used together. **init** and **doFinal** are mandatory, and **update** is optional. 5081e41f4b71Sopenharmony_ci 5082e41f4b71Sopenharmony_ci > **NOTE** 5083e41f4b71Sopenharmony_ci > 5084e41f4b71Sopenharmony_ci > You are advised to create a symmetric key generator based on the [HMAC key generation specifications](../../security/CryptoArchitectureKit/crypto-sym-key-generation-conversion-spec.md#hmac) and use [generateSymKey](#generatesymkey) to randomly generate a symmetric key or use [convertKey](#convertkey) to convert the binary data (whose length is the same as the key specifications) into a key.<br>If **HMAC** is specified to generate the symmetric key generator, only [convertKey](#convertkey) can be called to pass in a binary key of 1 to 4096 bytes. 5085e41f4b71Sopenharmony_ci 5086e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5087e41f4b71Sopenharmony_ci 5088e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5089e41f4b71Sopenharmony_ci 5090e41f4b71Sopenharmony_ci**Parameters** 5091e41f4b71Sopenharmony_ci 5092e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5093e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | -------------- | 5094e41f4b71Sopenharmony_ci| key | [SymKey](#symkey) | Yes | Shared symmetric key. | 5095e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 5096e41f4b71Sopenharmony_ci 5097e41f4b71Sopenharmony_ci**Error codes** 5098e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5099e41f4b71Sopenharmony_ci 5100e41f4b71Sopenharmony_ci| ID | Error Message | 5101e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5102e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5103e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5104e41f4b71Sopenharmony_ci 5105e41f4b71Sopenharmony_ci### init 5106e41f4b71Sopenharmony_ci 5107e41f4b71Sopenharmony_ciinit(key: SymKey): Promise\<void> 5108e41f4b71Sopenharmony_ci 5109e41f4b71Sopenharmony_ciInitializes the MAC computation with a symmetric key. This API uses a promise to return the result. **init**, **update**, and **doFinal** must be used together. **init** and **doFinal** are mandatory, and **update** is optional. 5110e41f4b71Sopenharmony_ci 5111e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5112e41f4b71Sopenharmony_ci 5113e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5114e41f4b71Sopenharmony_ci 5115e41f4b71Sopenharmony_ci**Parameters** 5116e41f4b71Sopenharmony_ci 5117e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5118e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------ | 5119e41f4b71Sopenharmony_ci| key | [SymKey](#symkey) | Yes | Shared symmetric key. | 5120e41f4b71Sopenharmony_ci 5121e41f4b71Sopenharmony_ci**Return value** 5122e41f4b71Sopenharmony_ci 5123e41f4b71Sopenharmony_ci| Type | Description | 5124e41f4b71Sopenharmony_ci| -------------- | ------------- | 5125e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value. | 5126e41f4b71Sopenharmony_ci 5127e41f4b71Sopenharmony_ci**Error codes** 5128e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5129e41f4b71Sopenharmony_ci 5130e41f4b71Sopenharmony_ci| ID | Error Message | 5131e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5132e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5133e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5134e41f4b71Sopenharmony_ci 5135e41f4b71Sopenharmony_ci### initSync<sup>12+</sup> 5136e41f4b71Sopenharmony_ci 5137e41f4b71Sopenharmony_ciinitSync(key: SymKey): void 5138e41f4b71Sopenharmony_ci 5139e41f4b71Sopenharmony_ciInitializes the MAC computation with a symmetric key. This API returns the result synchronously. **initSync**, **updateSync**, and **doFinalSync** must be used together. **initSync** and **doFinalSync** are mandatory, and **updateSync** is optional. 5140e41f4b71Sopenharmony_ci 5141e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5142e41f4b71Sopenharmony_ci 5143e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5144e41f4b71Sopenharmony_ci 5145e41f4b71Sopenharmony_ci**Parameters** 5146e41f4b71Sopenharmony_ci 5147e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5148e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------ | 5149e41f4b71Sopenharmony_ci| key | [SymKey](#symkey) | Yes | Shared symmetric key. | 5150e41f4b71Sopenharmony_ci 5151e41f4b71Sopenharmony_ci**Return value** 5152e41f4b71Sopenharmony_ci 5153e41f4b71Sopenharmony_ci| Type | Description | 5154e41f4b71Sopenharmony_ci| -------------- | ------------- | 5155e41f4b71Sopenharmony_ci| void | No value is returned. | 5156e41f4b71Sopenharmony_ci 5157e41f4b71Sopenharmony_ci**Error codes** 5158e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5159e41f4b71Sopenharmony_ci 5160e41f4b71Sopenharmony_ci| ID | Error Message | 5161e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5162e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 5163e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5164e41f4b71Sopenharmony_ci 5165e41f4b71Sopenharmony_ci### update 5166e41f4b71Sopenharmony_ci 5167e41f4b71Sopenharmony_ciupdate(input: DataBlob, callback: AsyncCallback\<void>): void 5168e41f4b71Sopenharmony_ci 5169e41f4b71Sopenharmony_ciUpdates the message for MAC computation. This API uses an asynchronous callback to return the result. 5170e41f4b71Sopenharmony_ci 5171e41f4b71Sopenharmony_ci> **NOTE** 5172e41f4b71Sopenharmony_ci> 5173e41f4b71Sopenharmony_ci> For details about the sample code for calling **update** multiple times in an HMAC operation, see [HMAC (Passing In Data by Segment)](../../security/CryptoArchitectureKit/crypto-compute-mac.md#hmac-passing-in-data-by-segment). 5174e41f4b71Sopenharmony_ci 5175e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5176e41f4b71Sopenharmony_ci 5177e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5178e41f4b71Sopenharmony_ci 5179e41f4b71Sopenharmony_ci**Parameters** 5180e41f4b71Sopenharmony_ci 5181e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5182e41f4b71Sopenharmony_ci| -------- | --------------------- | ---- | ------------ | 5183e41f4b71Sopenharmony_ci| input | [DataBlob](#datablob) | Yes | Data to pass in. | 5184e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 5185e41f4b71Sopenharmony_ci 5186e41f4b71Sopenharmony_ci**Error codes** 5187e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5188e41f4b71Sopenharmony_ci 5189e41f4b71Sopenharmony_ci| ID | Error Message | 5190e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5191e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5192e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5193e41f4b71Sopenharmony_ci 5194e41f4b71Sopenharmony_ci### update 5195e41f4b71Sopenharmony_ci 5196e41f4b71Sopenharmony_ciupdate(input: DataBlob): Promise\<void> 5197e41f4b71Sopenharmony_ci 5198e41f4b71Sopenharmony_ciUpdates the message for MAC computation. This API uses a promise to return the result. 5199e41f4b71Sopenharmony_ci 5200e41f4b71Sopenharmony_ci> **NOTE** 5201e41f4b71Sopenharmony_ci> 5202e41f4b71Sopenharmony_ci> For details about the sample code for calling **update** multiple times in an HMAC operation, see [HMAC (Passing In Data by Segment)](../../security/CryptoArchitectureKit/crypto-compute-mac.md#hmac-passing-in-data-by-segment). 5203e41f4b71Sopenharmony_ci 5204e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5205e41f4b71Sopenharmony_ci 5206e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5207e41f4b71Sopenharmony_ci 5208e41f4b71Sopenharmony_ci**Parameters** 5209e41f4b71Sopenharmony_ci 5210e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5211e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 5212e41f4b71Sopenharmony_ci| input | [DataBlob](#datablob) | Yes | Data to pass in. | 5213e41f4b71Sopenharmony_ci 5214e41f4b71Sopenharmony_ci**Return value** 5215e41f4b71Sopenharmony_ci 5216e41f4b71Sopenharmony_ci| Type | Description | 5217e41f4b71Sopenharmony_ci| -------------- | ------------- | 5218e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value. | 5219e41f4b71Sopenharmony_ci 5220e41f4b71Sopenharmony_ci**Error codes** 5221e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5222e41f4b71Sopenharmony_ci 5223e41f4b71Sopenharmony_ci| ID | Error Message | 5224e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5225e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5226e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5227e41f4b71Sopenharmony_ci 5228e41f4b71Sopenharmony_ci### updateSync<sup>12+</sup> 5229e41f4b71Sopenharmony_ci 5230e41f4b71Sopenharmony_ciupdateSync(input: DataBlob): void 5231e41f4b71Sopenharmony_ci 5232e41f4b71Sopenharmony_ciUpdates the message for MAC computation. This API returns the result synchronously. 5233e41f4b71Sopenharmony_ci 5234e41f4b71Sopenharmony_ci> **NOTE** 5235e41f4b71Sopenharmony_ci> 5236e41f4b71Sopenharmony_ci> For details about the sample code for calling **updateSync** multiple times in an HMAC operation, see [HMAC (Passing In Data by Segment)](../../security/CryptoArchitectureKit/crypto-compute-mac.md#hmac-passing-in-data-by-segment). 5237e41f4b71Sopenharmony_ci 5238e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5239e41f4b71Sopenharmony_ci 5240e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5241e41f4b71Sopenharmony_ci 5242e41f4b71Sopenharmony_ci**Parameters** 5243e41f4b71Sopenharmony_ci 5244e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5245e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------- | 5246e41f4b71Sopenharmony_ci| input | [DataBlob](#datablob) | Yes | Data to pass in. | 5247e41f4b71Sopenharmony_ci 5248e41f4b71Sopenharmony_ci**Return value** 5249e41f4b71Sopenharmony_ci 5250e41f4b71Sopenharmony_ci| Type | Description | 5251e41f4b71Sopenharmony_ci| -------------- | ------------- | 5252e41f4b71Sopenharmony_ci| void | No value is returned. | 5253e41f4b71Sopenharmony_ci 5254e41f4b71Sopenharmony_ci**Error codes** 5255e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5256e41f4b71Sopenharmony_ci 5257e41f4b71Sopenharmony_ci| ID | Error Message | 5258e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5259e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 5260e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5261e41f4b71Sopenharmony_ci 5262e41f4b71Sopenharmony_ci### doFinal 5263e41f4b71Sopenharmony_ci 5264e41f4b71Sopenharmony_cidoFinal(callback: AsyncCallback\<DataBlob>): void 5265e41f4b71Sopenharmony_ci 5266e41f4b71Sopenharmony_ciFinishes the MAC computation. This API uses an asynchronous callback to return the result. 5267e41f4b71Sopenharmony_ci 5268e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5269e41f4b71Sopenharmony_ci 5270e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5271e41f4b71Sopenharmony_ci 5272e41f4b71Sopenharmony_ci**Parameters** 5273e41f4b71Sopenharmony_ci 5274e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5275e41f4b71Sopenharmony_ci| -------- | ------------------------ | ---- | -------- | 5276e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[DataBlob](#datablob)> | Yes | Callback invoked to return a **DataBlob** object. | 5277e41f4b71Sopenharmony_ci 5278e41f4b71Sopenharmony_ci**Error codes** 5279e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5280e41f4b71Sopenharmony_ci 5281e41f4b71Sopenharmony_ci| ID | Error Message | 5282e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5283e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5284e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5285e41f4b71Sopenharmony_ci 5286e41f4b71Sopenharmony_ci**Example** 5287e41f4b71Sopenharmony_ci 5288e41f4b71Sopenharmony_ciFor more HMAC operation examples, see [MAC Operation](../../security/CryptoArchitectureKit/crypto-compute-mac.md). 5289e41f4b71Sopenharmony_ci 5290e41f4b71Sopenharmony_ci```ts 5291e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5292e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 5293e41f4b71Sopenharmony_ci 5294e41f4b71Sopenharmony_cifunction hmacByCallback() { 5295e41f4b71Sopenharmony_ci let mac = cryptoFramework.createMac('SHA256'); 5296e41f4b71Sopenharmony_ci let keyBlob: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("12345678abcdefgh", 'utf-8').buffer) }; 5297e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 5298e41f4b71Sopenharmony_ci symKeyGenerator.convertKey(keyBlob, (err, symKey) => { 5299e41f4b71Sopenharmony_ci mac.init(symKey, (err,) => { 5300e41f4b71Sopenharmony_ci mac.update({ data: new Uint8Array(buffer.from("hmacTestMessage", 'utf-8').buffer) }, (err,) => { 5301e41f4b71Sopenharmony_ci mac.doFinal((err, output) => { 5302e41f4b71Sopenharmony_ci console.info('[Callback]: HMAC result: ' + output.data); 5303e41f4b71Sopenharmony_ci console.info('[Callback]: MAC len: ' + mac.getMacLength()); 5304e41f4b71Sopenharmony_ci }); 5305e41f4b71Sopenharmony_ci }); 5306e41f4b71Sopenharmony_ci }); 5307e41f4b71Sopenharmony_ci }); 5308e41f4b71Sopenharmony_ci} 5309e41f4b71Sopenharmony_ci``` 5310e41f4b71Sopenharmony_ci 5311e41f4b71Sopenharmony_ci### doFinal 5312e41f4b71Sopenharmony_ci 5313e41f4b71Sopenharmony_cidoFinal(): Promise\<DataBlob> 5314e41f4b71Sopenharmony_ci 5315e41f4b71Sopenharmony_ciFinishes the MAC computation. This API uses a promise to return the result. 5316e41f4b71Sopenharmony_ci 5317e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5318e41f4b71Sopenharmony_ci 5319e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5320e41f4b71Sopenharmony_ci 5321e41f4b71Sopenharmony_ci**Return value** 5322e41f4b71Sopenharmony_ci 5323e41f4b71Sopenharmony_ci| Type | Description | 5324e41f4b71Sopenharmony_ci| ------------------ | ----------- | 5325e41f4b71Sopenharmony_ci| Promise\<[DataBlob](#datablob)> | Promise used to return the result. | 5326e41f4b71Sopenharmony_ci 5327e41f4b71Sopenharmony_ci**Error codes** 5328e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5329e41f4b71Sopenharmony_ci 5330e41f4b71Sopenharmony_ci| ID | Error Message | 5331e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5332e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5333e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5334e41f4b71Sopenharmony_ci 5335e41f4b71Sopenharmony_ci**Example** 5336e41f4b71Sopenharmony_ci 5337e41f4b71Sopenharmony_ciFor more HMAC operation examples, see [MAC Operation](../../security/CryptoArchitectureKit/crypto-compute-mac.md). 5338e41f4b71Sopenharmony_ci 5339e41f4b71Sopenharmony_ci```ts 5340e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5341e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 5342e41f4b71Sopenharmony_ci 5343e41f4b71Sopenharmony_ciasync function hmacByPromise() { 5344e41f4b71Sopenharmony_ci let mac = cryptoFramework.createMac('SHA256'); 5345e41f4b71Sopenharmony_ci let keyBlob: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("12345678abcdefgh", 'utf-8').buffer) }; 5346e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 5347e41f4b71Sopenharmony_ci let symKey = await symKeyGenerator.convertKey(keyBlob); 5348e41f4b71Sopenharmony_ci await mac.init(symKey); 5349e41f4b71Sopenharmony_ci await mac.update({ data: new Uint8Array(buffer.from("hmacTestMessage", 'utf-8').buffer) }); 5350e41f4b71Sopenharmony_ci let macOutput = await mac.doFinal(); 5351e41f4b71Sopenharmony_ci console.info('[Promise]: HMAC result: ' + macOutput.data); 5352e41f4b71Sopenharmony_ci console.info('[Promise]: MAC len: ' + mac.getMacLength()); 5353e41f4b71Sopenharmony_ci} 5354e41f4b71Sopenharmony_ci``` 5355e41f4b71Sopenharmony_ci 5356e41f4b71Sopenharmony_ci### doFinalSync<sup>12+</sup> 5357e41f4b71Sopenharmony_ci 5358e41f4b71Sopenharmony_cidoFinalSync(): DataBlob 5359e41f4b71Sopenharmony_ci 5360e41f4b71Sopenharmony_ciFinishes the MAC computation. This API returns the result synchronously. 5361e41f4b71Sopenharmony_ci 5362e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5363e41f4b71Sopenharmony_ci 5364e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5365e41f4b71Sopenharmony_ci 5366e41f4b71Sopenharmony_ci**Return value** 5367e41f4b71Sopenharmony_ci 5368e41f4b71Sopenharmony_ci| Type | Description | 5369e41f4b71Sopenharmony_ci| ------------------ | ----------- | 5370e41f4b71Sopenharmony_ci| [DataBlob](#datablob) | MAC computation result. | 5371e41f4b71Sopenharmony_ci 5372e41f4b71Sopenharmony_ci**Error codes** 5373e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5374e41f4b71Sopenharmony_ci 5375e41f4b71Sopenharmony_ci| ID | Error Message | 5376e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5377e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 5378e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5379e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 5380e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5381e41f4b71Sopenharmony_ci 5382e41f4b71Sopenharmony_ci**Example** 5383e41f4b71Sopenharmony_ci 5384e41f4b71Sopenharmony_ciFor more HMAC operation examples, see [MAC Operation](../../security/CryptoArchitectureKit/crypto-compute-mac.md). 5385e41f4b71Sopenharmony_ci 5386e41f4b71Sopenharmony_ci```ts 5387e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5388e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 5389e41f4b71Sopenharmony_ci 5390e41f4b71Sopenharmony_cifunction hmacBySync() { 5391e41f4b71Sopenharmony_ci let mac = cryptoFramework.createMac('SHA256'); 5392e41f4b71Sopenharmony_ci let keyBlob: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("12345678abcdefgh", 'utf-8').buffer) }; 5393e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 5394e41f4b71Sopenharmony_ci let symKey = symKeyGenerator.convertKeySync(keyBlob); 5395e41f4b71Sopenharmony_ci mac.initSync(symKey); 5396e41f4b71Sopenharmony_ci mac.updateSync({ data: new Uint8Array(buffer.from("hmacTestMessage", 'utf-8').buffer) }); 5397e41f4b71Sopenharmony_ci let macOutput = mac.doFinalSync(); 5398e41f4b71Sopenharmony_ci console.info('[Sync]: HMAC result: ' + macOutput.data); 5399e41f4b71Sopenharmony_ci console.info('[Sync]: MAC len: ' + mac.getMacLength()); 5400e41f4b71Sopenharmony_ci} 5401e41f4b71Sopenharmony_ci``` 5402e41f4b71Sopenharmony_ci 5403e41f4b71Sopenharmony_ci### getMacLength 5404e41f4b71Sopenharmony_ci 5405e41f4b71Sopenharmony_cigetMacLength(): number 5406e41f4b71Sopenharmony_ci 5407e41f4b71Sopenharmony_ciObtains the MAC length, in bytes. 5408e41f4b71Sopenharmony_ci 5409e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5410e41f4b71Sopenharmony_ci 5411e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5412e41f4b71Sopenharmony_ci 5413e41f4b71Sopenharmony_ci**Return value** 5414e41f4b71Sopenharmony_ci 5415e41f4b71Sopenharmony_ci| Type | Description | 5416e41f4b71Sopenharmony_ci| ------ | --------------------------- | 5417e41f4b71Sopenharmony_ci| number | MAC length obtained. | 5418e41f4b71Sopenharmony_ci 5419e41f4b71Sopenharmony_ci**Error codes** 5420e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5421e41f4b71Sopenharmony_ci 5422e41f4b71Sopenharmony_ci| ID | Error Message | 5423e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5424e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5425e41f4b71Sopenharmony_ci 5426e41f4b71Sopenharmony_ci**Example** 5427e41f4b71Sopenharmony_ci 5428e41f4b71Sopenharmony_ci<!--code_no_check--> 5429e41f4b71Sopenharmony_ci```ts 5430e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5431e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5432e41f4b71Sopenharmony_ci 5433e41f4b71Sopenharmony_cilet mac = cryptoFramework.createMac('SHA256'); 5434e41f4b71Sopenharmony_ciconsole.info('Mac algName is: ' + mac.algName); 5435e41f4b71Sopenharmony_cilet keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); 5436e41f4b71Sopenharmony_cilet keyBlob: cryptoFramework.DataBlob = { data: keyData }; 5437e41f4b71Sopenharmony_cilet symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 5438e41f4b71Sopenharmony_cilet promiseConvertKey = symKeyGenerator.convertKey(keyBlob); 5439e41f4b71Sopenharmony_cipromiseConvertKey.then(symKey => { 5440e41f4b71Sopenharmony_ci let promiseMacInit = mac.init(symKey); 5441e41f4b71Sopenharmony_ci return promiseMacInit; 5442e41f4b71Sopenharmony_ci}).then(() => { 5443e41f4b71Sopenharmony_ci let blob: cryptoFramework.DataBlob = { data : new Uint8Array([83])}; 5444e41f4b71Sopenharmony_ci let promiseMacUpdate = mac.update(blob); 5445e41f4b71Sopenharmony_ci return promiseMacUpdate; 5446e41f4b71Sopenharmony_ci}).then(() => { 5447e41f4b71Sopenharmony_ci let promiseMacDoFinal = mac.doFinal(); 5448e41f4b71Sopenharmony_ci return promiseMacDoFinal; 5449e41f4b71Sopenharmony_ci}).then(macOutput => { 5450e41f4b71Sopenharmony_ci console.info('[Promise]: HMAC result: ' + macOutput.data); 5451e41f4b71Sopenharmony_ci let macLen = mac.getMacLength(); 5452e41f4b71Sopenharmony_ci console.info('MAC len: ' + macLen); 5453e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 5454e41f4b71Sopenharmony_ci console.error("[Promise]: error: " + error.message); 5455e41f4b71Sopenharmony_ci}); 5456e41f4b71Sopenharmony_ci``` 5457e41f4b71Sopenharmony_ci 5458e41f4b71Sopenharmony_ci## cryptoFramework.createRandom 5459e41f4b71Sopenharmony_ci 5460e41f4b71Sopenharmony_cicreateRandom(): Random 5461e41f4b71Sopenharmony_ci 5462e41f4b71Sopenharmony_ciCreates a **Random** instance for generating random numbers and setting seeds. 5463e41f4b71Sopenharmony_ci 5464e41f4b71Sopenharmony_ciFor details about the supported specifications, see [Supported Algorithms and Specifications](../../security/CryptoArchitectureKit/crypto-generate-random-number.md#supported-algorithms-and-specifications). 5465e41f4b71Sopenharmony_ci 5466e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 5467e41f4b71Sopenharmony_ci 5468e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5469e41f4b71Sopenharmony_ci 5470e41f4b71Sopenharmony_ci**Return value** 5471e41f4b71Sopenharmony_ci 5472e41f4b71Sopenharmony_ci| Type | Description | 5473e41f4b71Sopenharmony_ci| ------ | ----------------------------------------------- | 5474e41f4b71Sopenharmony_ci| Random | Returns the [Random](#random) instance created. | 5475e41f4b71Sopenharmony_ci 5476e41f4b71Sopenharmony_ci**Error codes** 5477e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5478e41f4b71Sopenharmony_ci 5479e41f4b71Sopenharmony_ci| ID | Error Message | 5480e41f4b71Sopenharmony_ci| -------- | ------------ | 5481e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5482e41f4b71Sopenharmony_ci 5483e41f4b71Sopenharmony_ci**Example** 5484e41f4b71Sopenharmony_ci 5485e41f4b71Sopenharmony_ci```ts 5486e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5487e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5488e41f4b71Sopenharmony_ci 5489e41f4b71Sopenharmony_citry { 5490e41f4b71Sopenharmony_ci let rand = cryptoFramework.createRandom(); 5491e41f4b71Sopenharmony_ci} catch (error) { 5492e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 5493e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 5494e41f4b71Sopenharmony_ci} 5495e41f4b71Sopenharmony_ci``` 5496e41f4b71Sopenharmony_ci 5497e41f4b71Sopenharmony_ci## Random 5498e41f4b71Sopenharmony_ci 5499e41f4b71Sopenharmony_ciProvides APIs for computing random numbers and setting seeds. Before using any API of the **Random** class, you must create a **Random** instance by using [createRandom](#cryptoframeworkcreaterandom). 5500e41f4b71Sopenharmony_ci 5501e41f4b71Sopenharmony_ci### Attributes 5502e41f4b71Sopenharmony_ci 5503e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 5504e41f4b71Sopenharmony_ci 5505e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5506e41f4b71Sopenharmony_ci 5507e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 5508e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | -------------------- | 5509e41f4b71Sopenharmony_ci| algName<sup>10+</sup> | string | Yes | No | Algorithm used to generate the random number. Currently, only **CTR_DRBG** is supported. | 5510e41f4b71Sopenharmony_ci 5511e41f4b71Sopenharmony_ci### generateRandom 5512e41f4b71Sopenharmony_ci 5513e41f4b71Sopenharmony_cigenerateRandom(len: number, callback: AsyncCallback\<DataBlob>): void 5514e41f4b71Sopenharmony_ci 5515e41f4b71Sopenharmony_ciGenerates a random number of the specified length. This API uses an asynchronous callback to return the result. 5516e41f4b71Sopenharmony_ci 5517e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 5518e41f4b71Sopenharmony_ci 5519e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5520e41f4b71Sopenharmony_ci 5521e41f4b71Sopenharmony_ci**Parameters** 5522e41f4b71Sopenharmony_ci 5523e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5524e41f4b71Sopenharmony_ci| -------- | ------------------------ | ---- | -------------------- | 5525e41f4b71Sopenharmony_ci| len | number | Yes | Length of the random number to generate, in bytes. The value range is [1, INT_MAX]. | 5526e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[DataBlob](#datablob)> | Yes | Callback invoked to return a **DataBlob** object. | 5527e41f4b71Sopenharmony_ci 5528e41f4b71Sopenharmony_ci**Error codes** 5529e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5530e41f4b71Sopenharmony_ci 5531e41f4b71Sopenharmony_ci| ID | Error Message | 5532e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5533e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5534e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5535e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5536e41f4b71Sopenharmony_ci 5537e41f4b71Sopenharmony_ci**Example** 5538e41f4b71Sopenharmony_ci 5539e41f4b71Sopenharmony_ci```ts 5540e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5541e41f4b71Sopenharmony_ci 5542e41f4b71Sopenharmony_cilet rand = cryptoFramework.createRandom(); 5543e41f4b71Sopenharmony_cirand.generateRandom(12, (err, randData) => { 5544e41f4b71Sopenharmony_ci if (err) { 5545e41f4b71Sopenharmony_ci console.error("[Callback] err: " + err.code); 5546e41f4b71Sopenharmony_ci } else { 5547e41f4b71Sopenharmony_ci console.info('[Callback]: generate random result: ' + randData.data); 5548e41f4b71Sopenharmony_ci } 5549e41f4b71Sopenharmony_ci}); 5550e41f4b71Sopenharmony_ci``` 5551e41f4b71Sopenharmony_ci 5552e41f4b71Sopenharmony_ci### generateRandom 5553e41f4b71Sopenharmony_ci 5554e41f4b71Sopenharmony_cigenerateRandom(len: number): Promise\<DataBlob> 5555e41f4b71Sopenharmony_ci 5556e41f4b71Sopenharmony_ciGenerates a random number of the specified length. This API uses a promise to return the result. 5557e41f4b71Sopenharmony_ci 5558e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 5559e41f4b71Sopenharmony_ci 5560e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5561e41f4b71Sopenharmony_ci 5562e41f4b71Sopenharmony_ci**Parameters** 5563e41f4b71Sopenharmony_ci 5564e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5565e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------ | 5566e41f4b71Sopenharmony_ci| len | number | Yes | Length of the random number to generate, in bytes. The value range is [1, INT_MAX]. | 5567e41f4b71Sopenharmony_ci 5568e41f4b71Sopenharmony_ci**Return value** 5569e41f4b71Sopenharmony_ci 5570e41f4b71Sopenharmony_ci| Type | Description | 5571e41f4b71Sopenharmony_ci| ------------------ | ----------- | 5572e41f4b71Sopenharmony_ci| Promise\<[DataBlob](#datablob)> | Promise used to return the result. | 5573e41f4b71Sopenharmony_ci 5574e41f4b71Sopenharmony_ci**Error codes** 5575e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5576e41f4b71Sopenharmony_ci 5577e41f4b71Sopenharmony_ci| ID | Error Message | 5578e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5579e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5580e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5581e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5582e41f4b71Sopenharmony_ci 5583e41f4b71Sopenharmony_ci**Example** 5584e41f4b71Sopenharmony_ci 5585e41f4b71Sopenharmony_ci```ts 5586e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5587e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5588e41f4b71Sopenharmony_ci 5589e41f4b71Sopenharmony_cilet rand = cryptoFramework.createRandom(); 5590e41f4b71Sopenharmony_cilet promiseGenerateRand = rand.generateRandom(12); 5591e41f4b71Sopenharmony_cipromiseGenerateRand.then(randData => { 5592e41f4b71Sopenharmony_ci console.info('[Promise]: rand result: ' + randData.data); 5593e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 5594e41f4b71Sopenharmony_ci console.error("[Promise]: error: " + error.message); 5595e41f4b71Sopenharmony_ci}); 5596e41f4b71Sopenharmony_ci``` 5597e41f4b71Sopenharmony_ci 5598e41f4b71Sopenharmony_ci### generateRandomSync<sup>10+</sup> 5599e41f4b71Sopenharmony_ci 5600e41f4b71Sopenharmony_cigenerateRandomSync(len: number): DataBlob 5601e41f4b71Sopenharmony_ci 5602e41f4b71Sopenharmony_ciGenerates a random number of the specified length. This API returns the result synchronously. 5603e41f4b71Sopenharmony_ci 5604e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 5605e41f4b71Sopenharmony_ci 5606e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5607e41f4b71Sopenharmony_ci 5608e41f4b71Sopenharmony_ci**Parameters** 5609e41f4b71Sopenharmony_ci 5610e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5611e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------- | 5612e41f4b71Sopenharmony_ci| len | number | Yes | Length of the random number to generate, in bytes. The value range is [1, INT_MAX]. | 5613e41f4b71Sopenharmony_ci 5614e41f4b71Sopenharmony_ci**Return value** 5615e41f4b71Sopenharmony_ci 5616e41f4b71Sopenharmony_ci| Type | Description | 5617e41f4b71Sopenharmony_ci| ------------------ | ----------- | 5618e41f4b71Sopenharmony_ci|[DataBlob](#datablob) | Returns the generated random number. | 5619e41f4b71Sopenharmony_ci 5620e41f4b71Sopenharmony_ci**Error codes** 5621e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5622e41f4b71Sopenharmony_ci 5623e41f4b71Sopenharmony_ci| ID | Error Message | 5624e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5625e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5626e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5627e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5628e41f4b71Sopenharmony_ci 5629e41f4b71Sopenharmony_ci**Example** 5630e41f4b71Sopenharmony_ci 5631e41f4b71Sopenharmony_ci```ts 5632e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5633e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5634e41f4b71Sopenharmony_ci 5635e41f4b71Sopenharmony_cilet rand = cryptoFramework.createRandom(); 5636e41f4b71Sopenharmony_citry { 5637e41f4b71Sopenharmony_ci let randData = rand.generateRandomSync(12); 5638e41f4b71Sopenharmony_ci if (randData != null) { 5639e41f4b71Sopenharmony_ci console.info('[Sync]: rand result: ' + randData.data); 5640e41f4b71Sopenharmony_ci } else { 5641e41f4b71Sopenharmony_ci console.error("[Sync]: get rand result fail!"); 5642e41f4b71Sopenharmony_ci } 5643e41f4b71Sopenharmony_ci} catch (error) { 5644e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 5645e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 5646e41f4b71Sopenharmony_ci} 5647e41f4b71Sopenharmony_ci``` 5648e41f4b71Sopenharmony_ci 5649e41f4b71Sopenharmony_ci### setSeed 5650e41f4b71Sopenharmony_ci 5651e41f4b71Sopenharmony_cisetSeed(seed: DataBlob): void 5652e41f4b71Sopenharmony_ci 5653e41f4b71Sopenharmony_ciSets a seed. 5654e41f4b71Sopenharmony_ci 5655e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 5656e41f4b71Sopenharmony_ci 5657e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5658e41f4b71Sopenharmony_ci 5659e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5660e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------ | 5661e41f4b71Sopenharmony_ci| seed | [DataBlob](#datablob) | Yes | Seed to set. | 5662e41f4b71Sopenharmony_ci 5663e41f4b71Sopenharmony_ci**Error codes** 5664e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5665e41f4b71Sopenharmony_ci 5666e41f4b71Sopenharmony_ci| ID | Error Message | 5667e41f4b71Sopenharmony_ci| -------- | ----------------- | 5668e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5669e41f4b71Sopenharmony_ci 5670e41f4b71Sopenharmony_ci**Example** 5671e41f4b71Sopenharmony_ci 5672e41f4b71Sopenharmony_ci```ts 5673e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5674e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5675e41f4b71Sopenharmony_ci 5676e41f4b71Sopenharmony_cilet rand = cryptoFramework.createRandom(); 5677e41f4b71Sopenharmony_cirand.generateRandom(12, (err, randData) => { 5678e41f4b71Sopenharmony_ci if (err) { 5679e41f4b71Sopenharmony_ci console.error("[Callback] err: " + err.code); 5680e41f4b71Sopenharmony_ci } else { 5681e41f4b71Sopenharmony_ci console.info('[Callback]: generate random result: ' + randData.data); 5682e41f4b71Sopenharmony_ci try { 5683e41f4b71Sopenharmony_ci rand.setSeed(randData); 5684e41f4b71Sopenharmony_ci } catch (error) { 5685e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 5686e41f4b71Sopenharmony_ci console.error(`sync error, ${e.code}, ${e.message}`); 5687e41f4b71Sopenharmony_ci } 5688e41f4b71Sopenharmony_ci } 5689e41f4b71Sopenharmony_ci}); 5690e41f4b71Sopenharmony_ci``` 5691e41f4b71Sopenharmony_ci 5692e41f4b71Sopenharmony_ci## cryptoFramework.createKdf<sup>11+</sup> 5693e41f4b71Sopenharmony_ci 5694e41f4b71Sopenharmony_cicreateKdf(algName: string): Kdf 5695e41f4b71Sopenharmony_ci 5696e41f4b71Sopenharmony_ciCreates a key derivation function instance.<br>For details about the supported specifications, see [Key Derivation Overview and Algorithm Specifications](../../security/CryptoArchitectureKit/crypto-key-derivation-overview.md). 5697e41f4b71Sopenharmony_ci 5698e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5699e41f4b71Sopenharmony_ci 5700e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5701e41f4b71Sopenharmony_ci 5702e41f4b71Sopenharmony_ci**Parameters** 5703e41f4b71Sopenharmony_ci 5704e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5705e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------------- | 5706e41f4b71Sopenharmony_ci| algName | string | Yes | Key derivation algorithm (including the hash function for the HMAC). Currently, only PBKDF2 and HKDF are supported. For example, **PBKDF2\|SHA256** and **HKDF\|SHA256**. | 5707e41f4b71Sopenharmony_ci 5708e41f4b71Sopenharmony_ci**Return value** 5709e41f4b71Sopenharmony_ci 5710e41f4b71Sopenharmony_ci| Type | Description | 5711e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------ | 5712e41f4b71Sopenharmony_ci| [Kdf](#kdf11) | Key derivation function instance created. | 5713e41f4b71Sopenharmony_ci 5714e41f4b71Sopenharmony_ci**Error codes** 5715e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5716e41f4b71Sopenharmony_ci 5717e41f4b71Sopenharmony_ci| ID | Error Message | 5718e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5719e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5720e41f4b71Sopenharmony_ci| 801 | this operation is not supported. | 5721e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5722e41f4b71Sopenharmony_ci 5723e41f4b71Sopenharmony_ci**Example** 5724e41f4b71Sopenharmony_ci- PBKDF2 5725e41f4b71Sopenharmony_ci```ts 5726e41f4b71Sopenharmony_ciimport { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5727e41f4b71Sopenharmony_ci 5728e41f4b71Sopenharmony_cilet kdf = cryptoFramework.createKdf('PBKDF2|SHA256'); 5729e41f4b71Sopenharmony_ci``` 5730e41f4b71Sopenharmony_ci 5731e41f4b71Sopenharmony_ci## Kdf<sup>11+</sup> 5732e41f4b71Sopenharmony_ci 5733e41f4b71Sopenharmony_ciDefines the key derivation function class. Before using APIs of this class, you need to create an instance of this class by using **createKdf(algName: string): Kdf**. 5734e41f4b71Sopenharmony_ci 5735e41f4b71Sopenharmony_ci### Attributes 5736e41f4b71Sopenharmony_ci 5737e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5738e41f4b71Sopenharmony_ci 5739e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5740e41f4b71Sopenharmony_ci 5741e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 5742e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---- | ---------------------------- | 5743e41f4b71Sopenharmony_ci| algName | string | Yes | No | Algorithm of the key derivation function. | 5744e41f4b71Sopenharmony_ci 5745e41f4b71Sopenharmony_ci### generateSecret 5746e41f4b71Sopenharmony_ci 5747e41f4b71Sopenharmony_cigenerateSecret(params: KdfSpec, callback: AsyncCallback\<DataBlob>): void 5748e41f4b71Sopenharmony_ci 5749e41f4b71Sopenharmony_ciGenerates a key based on the specified key derivation parameters. This API uses an asynchronous callback to return the result. 5750e41f4b71Sopenharmony_ci 5751e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5752e41f4b71Sopenharmony_ci 5753e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5754e41f4b71Sopenharmony_ci 5755e41f4b71Sopenharmony_ci**Parameters** 5756e41f4b71Sopenharmony_ci 5757e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5758e41f4b71Sopenharmony_ci| -------- | ------------------------ | ---- | ---------------------- | 5759e41f4b71Sopenharmony_ci| params | [KdfSpec](#kdfspec11) | Yes | Parameters of the key derivation function. | 5760e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[DataBlob](#datablob)> | Yes | Callback invoked to return the derived key generated. | 5761e41f4b71Sopenharmony_ci 5762e41f4b71Sopenharmony_ci**Error codes** 5763e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5764e41f4b71Sopenharmony_ci 5765e41f4b71Sopenharmony_ci| ID | Error Message | 5766e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5767e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5768e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5769e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5770e41f4b71Sopenharmony_ci 5771e41f4b71Sopenharmony_ci**Example** 5772e41f4b71Sopenharmony_ci 5773e41f4b71Sopenharmony_ci- PBKDF2 5774e41f4b71Sopenharmony_ci ```ts 5775e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5776e41f4b71Sopenharmony_ci 5777e41f4b71Sopenharmony_ci let spec: cryptoFramework.PBKDF2Spec = { 5778e41f4b71Sopenharmony_ci algName: 'PBKDF2', 5779e41f4b71Sopenharmony_ci password: '123456', 5780e41f4b71Sopenharmony_ci salt: new Uint8Array(16), 5781e41f4b71Sopenharmony_ci iterations: 10000, 5782e41f4b71Sopenharmony_ci keySize: 32 5783e41f4b71Sopenharmony_ci }; 5784e41f4b71Sopenharmony_ci let kdf = cryptoFramework.createKdf('PBKDF2|SHA256'); 5785e41f4b71Sopenharmony_ci kdf.generateSecret(spec, (err, secret) => { 5786e41f4b71Sopenharmony_ci if (err) { 5787e41f4b71Sopenharmony_ci console.error("key derivation error."); 5788e41f4b71Sopenharmony_ci return; 5789e41f4b71Sopenharmony_ci } 5790e41f4b71Sopenharmony_ci console.info('key derivation output is ' + secret.data); 5791e41f4b71Sopenharmony_ci }); 5792e41f4b71Sopenharmony_ci ``` 5793e41f4b71Sopenharmony_ci 5794e41f4b71Sopenharmony_ci- HKDF 5795e41f4b71Sopenharmony_ci ```ts 5796e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5797e41f4b71Sopenharmony_ci 5798e41f4b71Sopenharmony_ci let spec: cryptoFramework.HKDFSpec = { 5799e41f4b71Sopenharmony_ci algName: 'HKDF', 5800e41f4b71Sopenharmony_ci key: '123456', 5801e41f4b71Sopenharmony_ci salt: new Uint8Array(16), 5802e41f4b71Sopenharmony_ci info: new Uint8Array(16), 5803e41f4b71Sopenharmony_ci keySize: 32 5804e41f4b71Sopenharmony_ci }; 5805e41f4b71Sopenharmony_ci let kdf = cryptoFramework.createKdf('HKDF|SHA256|EXTRACT_AND_EXPAND'); 5806e41f4b71Sopenharmony_ci kdf.generateSecret(spec, (err, secret) => { 5807e41f4b71Sopenharmony_ci if (err) { 5808e41f4b71Sopenharmony_ci console.error("key derivation error."); 5809e41f4b71Sopenharmony_ci return; 5810e41f4b71Sopenharmony_ci } 5811e41f4b71Sopenharmony_ci console.info('key derivation output is ' + secret.data); 5812e41f4b71Sopenharmony_ci }); 5813e41f4b71Sopenharmony_ci ``` 5814e41f4b71Sopenharmony_ci 5815e41f4b71Sopenharmony_ci### generateSecret 5816e41f4b71Sopenharmony_ci 5817e41f4b71Sopenharmony_cigenerateSecret(params: KdfSpec): Promise\<DataBlob> 5818e41f4b71Sopenharmony_ci 5819e41f4b71Sopenharmony_ciGenerates a key based on the specified key derivation parameters. This API uses a promise to return the result. 5820e41f4b71Sopenharmony_ci 5821e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5822e41f4b71Sopenharmony_ci 5823e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5824e41f4b71Sopenharmony_ci 5825e41f4b71Sopenharmony_ci**Parameters** 5826e41f4b71Sopenharmony_ci 5827e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5828e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------- | 5829e41f4b71Sopenharmony_ci| params | [KdfSpec](#kdfspec11) | Yes | Parameters of the key derivation function. | 5830e41f4b71Sopenharmony_ci 5831e41f4b71Sopenharmony_ci**Return value** 5832e41f4b71Sopenharmony_ci 5833e41f4b71Sopenharmony_ci| Type | Description | 5834e41f4b71Sopenharmony_ci| ------------------ | -------- | 5835e41f4b71Sopenharmony_ci| Promise\<[DataBlob](#datablob)> | Promise used to return the derived key generated. | 5836e41f4b71Sopenharmony_ci 5837e41f4b71Sopenharmony_ci**Error codes** 5838e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5839e41f4b71Sopenharmony_ci 5840e41f4b71Sopenharmony_ci| ID | Error Message | 5841e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5842e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed.| 5843e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5844e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5845e41f4b71Sopenharmony_ci 5846e41f4b71Sopenharmony_ci**Example** 5847e41f4b71Sopenharmony_ci 5848e41f4b71Sopenharmony_ci- PBKDF2 5849e41f4b71Sopenharmony_ci ```ts 5850e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5851e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 5852e41f4b71Sopenharmony_ci 5853e41f4b71Sopenharmony_ci let spec: cryptoFramework.PBKDF2Spec = { 5854e41f4b71Sopenharmony_ci algName: 'PBKDF2', 5855e41f4b71Sopenharmony_ci password: '123456', 5856e41f4b71Sopenharmony_ci salt: new Uint8Array(16), 5857e41f4b71Sopenharmony_ci iterations: 10000, 5858e41f4b71Sopenharmony_ci keySize: 32 5859e41f4b71Sopenharmony_ci }; 5860e41f4b71Sopenharmony_ci let kdf = cryptoFramework.createKdf('PBKDF2|SHA256'); 5861e41f4b71Sopenharmony_ci let kdfPromise = kdf.generateSecret(spec); 5862e41f4b71Sopenharmony_ci kdfPromise.then(secret => { 5863e41f4b71Sopenharmony_ci console.info('key derivation output is ' + secret.data); 5864e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 5865e41f4b71Sopenharmony_ci console.error("key derivation error, " + error.message); 5866e41f4b71Sopenharmony_ci }); 5867e41f4b71Sopenharmony_ci ``` 5868e41f4b71Sopenharmony_ci 5869e41f4b71Sopenharmony_ci- HKDF 5870e41f4b71Sopenharmony_ci ```ts 5871e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5872e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 5873e41f4b71Sopenharmony_ci 5874e41f4b71Sopenharmony_ci let spec: cryptoFramework.HKDFSpec = { 5875e41f4b71Sopenharmony_ci algName: 'HKDF', 5876e41f4b71Sopenharmony_ci key: '123456', 5877e41f4b71Sopenharmony_ci salt: new Uint8Array(16), 5878e41f4b71Sopenharmony_ci info: new Uint8Array(16), 5879e41f4b71Sopenharmony_ci keySize: 32 5880e41f4b71Sopenharmony_ci }; 5881e41f4b71Sopenharmony_ci let kdf = cryptoFramework.createKdf('HKDF|SHA256|EXTRACT_AND_EXPAND'); 5882e41f4b71Sopenharmony_ci let kdfPromise = kdf.generateSecret(spec); 5883e41f4b71Sopenharmony_ci kdfPromise.then(secret => { 5884e41f4b71Sopenharmony_ci console.info('key derivation output is ' + secret.data); 5885e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 5886e41f4b71Sopenharmony_ci console.error("key derivation error, " + error.message); 5887e41f4b71Sopenharmony_ci }); 5888e41f4b71Sopenharmony_ci ``` 5889e41f4b71Sopenharmony_ci 5890e41f4b71Sopenharmony_ci### generateSecretSync<sup>12+</sup> 5891e41f4b71Sopenharmony_ci 5892e41f4b71Sopenharmony_cigenerateSecretSync(params: KdfSpec): DataBlob 5893e41f4b71Sopenharmony_ci 5894e41f4b71Sopenharmony_ciGenerates a key based on the specified key derivation parameters. This API returns the result synchronously. 5895e41f4b71Sopenharmony_ci 5896e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5897e41f4b71Sopenharmony_ci 5898e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Security.CryptoFramework 5899e41f4b71Sopenharmony_ci 5900e41f4b71Sopenharmony_ci**Parameters** 5901e41f4b71Sopenharmony_ci 5902e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5903e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------- | 5904e41f4b71Sopenharmony_ci| params | [KdfSpec](#kdfspec11) | Yes | Parameters of the key derivation function. | 5905e41f4b71Sopenharmony_ci 5906e41f4b71Sopenharmony_ci**Return value** 5907e41f4b71Sopenharmony_ci 5908e41f4b71Sopenharmony_ci| Type | Description | 5909e41f4b71Sopenharmony_ci| ------------------ | -------- | 5910e41f4b71Sopenharmony_ci| [DataBlob](#datablob) | Key derived. | 5911e41f4b71Sopenharmony_ci 5912e41f4b71Sopenharmony_ci**Error codes** 5913e41f4b71Sopenharmony_ciFor details about the error codes, see [Crypto Framework Error Codes](errorcode-crypto-framework.md). 5914e41f4b71Sopenharmony_ci 5915e41f4b71Sopenharmony_ci| ID | Error Message | 5916e41f4b71Sopenharmony_ci| -------- | ---------------------- | 5917e41f4b71Sopenharmony_ci| 401 | invalid parameters. Possible causes: <br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 5918e41f4b71Sopenharmony_ci| 17620001 | memory error. | 5919e41f4b71Sopenharmony_ci| 17620002 | runtime error. | 5920e41f4b71Sopenharmony_ci| 17630001 | crypto operation error. | 5921e41f4b71Sopenharmony_ci 5922e41f4b71Sopenharmony_ci**Example** 5923e41f4b71Sopenharmony_ci 5924e41f4b71Sopenharmony_ci- PBKDF2 5925e41f4b71Sopenharmony_ci ```ts 5926e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5927e41f4b71Sopenharmony_ci 5928e41f4b71Sopenharmony_ci let spec: cryptoFramework.PBKDF2Spec = { 5929e41f4b71Sopenharmony_ci algName: 'PBKDF2', 5930e41f4b71Sopenharmony_ci password: '123456', 5931e41f4b71Sopenharmony_ci salt: new Uint8Array(16), 5932e41f4b71Sopenharmony_ci iterations: 10000, 5933e41f4b71Sopenharmony_ci keySize: 32 5934e41f4b71Sopenharmony_ci }; 5935e41f4b71Sopenharmony_ci let kdf = cryptoFramework.createKdf('PBKDF2|SHA256'); 5936e41f4b71Sopenharmony_ci let secret = kdf.generateSecretSync(spec); 5937e41f4b71Sopenharmony_ci console.info("[Sync]key derivation output is " + secret.data); 5938e41f4b71Sopenharmony_ci ``` 5939e41f4b71Sopenharmony_ci 5940e41f4b71Sopenharmony_ci- HKDF 5941e41f4b71Sopenharmony_ci ```ts 5942e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 5943e41f4b71Sopenharmony_ci 5944e41f4b71Sopenharmony_ci let spec: cryptoFramework.HKDFSpec = { 5945e41f4b71Sopenharmony_ci algName: 'HKDF', 5946e41f4b71Sopenharmony_ci key: '123456', 5947e41f4b71Sopenharmony_ci salt: new Uint8Array(16), 5948e41f4b71Sopenharmony_ci info: new Uint8Array(16), 5949e41f4b71Sopenharmony_ci keySize: 32 5950e41f4b71Sopenharmony_ci }; 5951e41f4b71Sopenharmony_ci let kdf = cryptoFramework.createKdf('HKDF|SHA256|EXTRACT_AND_EXPAND'); 5952e41f4b71Sopenharmony_ci let secret = kdf.generateSecretSync(spec); 5953e41f4b71Sopenharmony_ci console.info("[Sync]key derivation output is " + secret.data); 5954e41f4b71Sopenharmony_ci ``` 5955