The following table describes the APIs used in typical key generation operations. For more information about the APIs, see [Crypto Framework](../reference/apis/js-apis-cryptoFramework.md).
The following table describes the APIs used in typical key generation operations. For more information about the APIs, see [Crypto Framework](../reference/apis/js-apis-cryptoFramework.md).
|Instance|API|Description|
|Instance|API|Description|
|---|---|---|
|---|---|---|
|cryptoFramework|createAsyKeyGenerator(algName : string) : AsyKeyGenerator|Creates an **AsyKeyGenerator** instance based on the asymmetric key pair specifications specified by **algName**.|
|cryptoFramework|createAsyKeyGenerator(algName : string) : AsyKeyGenerator|Creates an **AsyKeyGenerator** instance based on the asymmetric key pair specifications specified by **algName**.|
...
@@ -54,17 +55,17 @@ import cryptoFramework from '@ohos.security.cryptoFramework';
...
@@ -54,17 +55,17 @@ import cryptoFramework from '@ohos.security.cryptoFramework';
if(error){// If the service logic fails to be executed, return the error information in the first parameter of the callback.
if(error){// If the service logic fails to be executed, the first parameter of callback returns error information, that is, an exception is thrown asynchronously.
letencodedKey=key.getEncoded();// Obtain the binary data of the symmetric key and output a 192-bit byte stream.
letencodedKey=key.getEncoded();// Obtain the binary data of the symmetric key and output a 192-bit byte stream in hexadecimal format. The length is 48, that is, 24 bytes.
## Generating an Asymmetric Key Object and Obtaining Key Parameters
## Generating an Asymmetric Key Object and Obtaining Key Parameters
### When to Use
### When to Use
...
@@ -225,12 +329,12 @@ The following table describes the APIs used in typical key generation operations
...
@@ -225,12 +329,12 @@ The following table describes the APIs used in typical key generation operations
|Instance|API|Description|
|Instance|API|Description|
|---|---|---|
|---|---|---|
|AsyKeyGeneratorBySpec|generateKeyPair(callback: AsyncCallback<KeyPair>): void;|Generates a **KeyPair** instance based on the key parameters. This API uses an asynchronous callback to return the result.|
|AsyKeyGeneratorBySpec|generateKeyPair(callback: AsyncCallback\<KeyPair>): void;|Generates a **KeyPair** instance based on the key parameters. This API uses an asynchronous callback to return the result.|
|AsyKeyGeneratorBySpec|generateKeyPair(): Promise<KeyPair>;|Generates a **KeyPair** instance based on the key parameters. This API uses a promise to return the result.|
|AsyKeyGeneratorBySpec|generateKeyPair(): Promise\<KeyPair>;|Generates a **KeyPair** instance based on the key parameters. This API uses a promise to return the result.|
|AsyKeyGeneratorBySpec|generatePriKey(callback: AsyncCallback<KeyPair>): void;|Generates a **PriKey** instance based on the key parameters. This API uses an asynchronous callback to return the result.|
|AsyKeyGeneratorBySpec|generatePriKey(callback: AsyncCallback\<KeyPair>): void;|Generates a **PriKey** instance based on the key parameters. This API uses an asynchronous callback to return the result.|
|AsyKeyGeneratorBySpec|generatePriKey(): Promise<KeyPair>;|Generates a **PriKey** instance based on the key parameters. This API uses a promise to return the result.|
|AsyKeyGeneratorBySpec|generatePriKey(): Promise\<KeyPair>;|Generates a **PriKey** instance based on the key parameters. This API uses a promise to return the result.|
|AsyKeyGeneratorBySpec|generatePubKey(callback: AsyncCallback<KeyPair>): void;|Generates a **PubKey** instance based on the key parameters. This API uses an asynchronous callback to return the result.|
|AsyKeyGeneratorBySpec|generatePubKey(callback: AsyncCallback\<KeyPair>): void;|Generates a **PubKey** instance based on the key parameters. This API uses an asynchronous callback to return the result.|
|AsyKeyGeneratorBySpec|generatePubKey(): Promise<KeyPair>;|Generates a **PubKey** instance based on the key parameters. This API uses a promise to return the result.|
|AsyKeyGeneratorBySpec|generatePubKey(): Promise\<KeyPair>;|Generates a **PubKey** instance based on the key parameters. This API uses a promise to return the result.|
| PriKey | getAsyKeySpec(itemType: AsyKeySpecItem): bigint \| string \| number; | Obtains the key specifications of a **PriKey** instance.|
| PriKey | getAsyKeySpec(itemType: AsyKeySpecItem): bigint \| string \| number; | Obtains the key specifications of a **PriKey** instance.|
| PubKey | getAsyKeySpec(itemType: AsyKeySpecItem): bigint \| string \| number; | Obtains the key specifications of a **PubKey** instance.|
| PubKey | getAsyKeySpec(itemType: AsyKeySpecItem): bigint \| string \| number; | Obtains the key specifications of a **PubKey** instance.|
...
@@ -258,24 +362,24 @@ function showBigIntInfo(bnName, bnValue) {
...
@@ -258,24 +362,24 @@ function showBigIntInfo(bnName, bnValue) {
// Construct the EccCommonSpec struct based on the key specifications. The EccCommonSpec struct defines the common parameters of the ECC private key and public key.
// Construct the EccCommonSpec struct based on the key specifications. The EccCommonSpec struct defines the common parameters of the ECC private key and public key.
AlertDialog.show({message:"error pub key big number"});
AlertDialog.show({message:"error pub key big number"});
}else{
}else{
console.info("n, e in the pubKey are same as the spec.");
console.info("n, e in the pubKey are same as the spec.");
}
}
...
@@ -424,7 +532,6 @@ Important data needs to be encrypted in data storage or transmission for securit
...
@@ -424,7 +532,6 @@ Important data needs to be encrypted in data storage or transmission for securit
### Available APIs
### Available APIs
The following table describes the APIs used in the typical encryption and decryption operations. For details about the APIs, see [Crypto Framework](../reference/apis/js-apis-cryptoFramework.md).
The following table describes the APIs used in the typical encryption and decryption operations. For details about the APIs, see [Crypto Framework](../reference/apis/js-apis-cryptoFramework.md).
> **NOTE**
> **NOTE**
>
>
> Due to complexity of cryptographic algorithms, the implementation varies depending on the key specifications and parameters you use, and cannot be enumerated by sample code. Before you start, understand the APIs to ensure correct use of these APIs.
> Due to complexity of cryptographic algorithms, the implementation varies depending on the key specifications and parameters you use, and cannot be enumerated by sample code. Before you start, understand the APIs to ensure correct use of these APIs.
...
@@ -433,9 +540,9 @@ The following table describes the APIs used in the typical encryption and decryp
...
@@ -433,9 +540,9 @@ The following table describes the APIs used in the typical encryption and decryp
|Instance|API|Description|
|Instance|API|Description|
|---|---|---|
|---|---|---|
|cryptoFramework|createCipher(transformation : string) : Cipher|Creates a **Cipher** instance based on the algorithm specified by **transformation**.|
|cryptoFramework|createCipher(transformation : string) : Cipher|Creates a **Cipher** instance.|
|Cipher|init(opMode : CryptoMode, key : Key, params : ParamsSpec, callback : AsyncCallback\<void>) : void|Initializes the **Cipher** instance. This API uses an asynchronous callback to return the result.|
|Cipher|init(opMode : CryptoMode, key : Key, params : ParamsSpec, callback : AsyncCallback\<void>) : void|Sets a key and initializes the **Cipher** instance. This API uses an asynchronous callback to return the result.|
|Cipher|init(opMode : CryptoMode, key : Key, params : ParamsSpec) : Promise\<void>|Initializes the **Cipher** instance. This API uses a promise to return the result.|
|Cipher|init(opMode : CryptoMode, key : Key, params : ParamsSpec) : Promise\<void>|Sets a key and initializes the **Cipher** instance. This API uses a promise to return the result.|
|Cipher|update(data : DataBlob, callback : AsyncCallback\<DataBlob>) : void|Updates the data for encryption and decryption. This API uses an asynchronous callback to return the result.|
|Cipher|update(data : DataBlob, callback : AsyncCallback\<DataBlob>) : void|Updates the data for encryption and decryption. This API uses an asynchronous callback to return the result.|
|Cipher|update(data : DataBlob) : Promise\<DataBlob>|Updates the data for encryption and decryption. This API uses a promise to return the result.|
|Cipher|update(data : DataBlob) : Promise\<DataBlob>|Updates the data for encryption and decryption. This API uses a promise to return the result.|
|Cipher|doFinal(data : DataBlob, callback : AsyncCallback\<DataBlob>) : void|Finalizes the encryption or decryption. This API uses an asynchronous callback to return the result.|
|Cipher|doFinal(data : DataBlob, callback : AsyncCallback\<DataBlob>) : void|Finalizes the encryption or decryption. This API uses an asynchronous callback to return the result.|
...
@@ -461,19 +568,19 @@ var globalKey;
...
@@ -461,19 +568,19 @@ var globalKey;
varglobalCipherText;
varglobalCipherText;
functiongenGcmParamsSpec(){
functiongenGcmParamsSpec(){
letarr=[0,0,0,0,0,0,0,0,0,0,0,0];// 12 bytes
letarr=[0,0,0,0,0,0,0,0,0,0,0,0];// 12 bytes
letdataIv=newUint8Array(arr);
letdataIv=newUint8Array(arr);
letivBlob={data:dataIv};
letivBlob={data:dataIv};
arr=[0,0,0,0,0,0,0,0];// 8 bytes
arr=[0,0,0,0,0,0,0,0];// 8 bytes
letdataAad=newUint8Array(arr);
letdataAad=newUint8Array(arr);
letaadBlob={data:dataAad};
letaadBlob={data:dataAad};
arr=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];// 16 bytes
arr=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];// 16 bytes
letdataTag=newUint8Array(arr);
letdataTag=newUint8Array(arr);
lettagBlob={data:dataTag};// The authTag of GCM is obtained by doFinal() in encryption and passed in params of init() in decryption.
lettagBlob={data:dataTag};// The GCM authTag is obtained by doFinal() in encryption and passed in params of init() in decryption.
// In GCM mode, the encrypted authentication information needs to be obtained from the output of doFinal() and passed in globalGcmParams of init() in decryption.
// In GCM mode, the encrypted authentication information needs to be obtained from the output of doFinal() and passed in globalGcmParams of init() in decryption.
globalGcmParams.authTag=authTag;
globalGcmParams.authTag=authTag;
return;
return;
}).then(()=>{
}).then(()=>{
// Initialize the Cipher instance and start decryption.
// Initialize the Cipher instance and start decryption.
letupdateOutput=awaitglobalCipher.update(messageBlob);// Update by segment.
// Combine the result of each update() to obtain the ciphertext. In certain cases, the doFinal() results need to be combined, which depends on the cipher block mode
// and padding mode you use. In this example, the doFinal() result in GCM mode contains authTag but not ciphertext. Therefore, there is no need to combine the results.
letupdateOutput=awaitglobalCipher.update(messageBlob);// Update by segment.
letupdateOutput=awaitglobalCipher.update(messageBlob);// Update by segment.
// Combine the result of each update() to obtain the ciphertext. In certain cases, the doFinal() results need to be combined, which depends on the cipher block mode
globalPlainText+=uint8ArrayToString(updateOutput.data);// Restore the original plaintext.
// and padding mode you use. In this example, the doFinal() result in GCM mode contains authTag but not ciphertext. Therefore, there is no need to combine the results.
// The get() and set() operations can be performed before the init() operation of the Cipher object and are equivalent to those after the init() operation. For example, set and get the decoder.
// The get() and set() operations can be performed before the init() operation of the Cipher object and are equivalent to those after the init() operation. For example, set and get the decoder.
// Check whether the decrypted data is consistent with the original data.
if(input.data.toString()===data.data.toString()){
AlertDialog.show({message:"decrype success"});
return;
}
AlertDialog.show({message:"decrype fail"});
});
});
});
});
});
});
}
}
```
```
...
@@ -1218,6 +1575,7 @@ A digital signature can be used to verify the authenticity of a message. Typical
...
@@ -1218,6 +1575,7 @@ A digital signature can be used to verify the authenticity of a message. Typical
1. Use RSA to generate a signature and verify the signature.
1. Use RSA to generate a signature and verify the signature.
2. Use ECC to generate a signature and verify the signature.
2. Use ECC to generate a signature and verify the signature.
3. Use RSA to generate a signature and verify the signature. Obtain and set **SignSpecItem** when the PSS padding mode is used.
3. Use RSA to generate a signature and verify the signature. Obtain and set **SignSpecItem** when the PSS padding mode is used.
4. Use SM2 to generate a signature and verify the signature.
> **NOTE**
> **NOTE**
>
>
...
@@ -1227,7 +1585,6 @@ A digital signature can be used to verify the authenticity of a message. Typical
...
@@ -1227,7 +1585,6 @@ A digital signature can be used to verify the authenticity of a message. Typical
### Available APIs
### Available APIs
The following table describes the APIs used in typical signing and signature verification operations. For more information about the APIs, see [Crypto Framework](../reference/apis/js-apis-cryptoFramework.md).
The following table describes the APIs used in typical signing and signature verification operations. For more information about the APIs, see [Crypto Framework](../reference/apis/js-apis-cryptoFramework.md).
> **NOTE**
> **NOTE**
>
>
> Due to complexity of cryptographic algorithms, the implementation varies depending on the specifications and parameters you use, and cannot be enumerated by sample code. Before you start, understand the APIs to ensure correct use of these APIs.
> Due to complexity of cryptographic algorithms, the implementation varies depending on the specifications and parameters you use, and cannot be enumerated by sample code. Before you start, understand the APIs to ensure correct use of these APIs.
...
@@ -1261,7 +1618,7 @@ Use RSA to sign data and verify the signature.
...
@@ -1261,7 +1618,7 @@ Use RSA to sign data and verify the signature.
2. Create a **Sign** instance.<br>Call **createSign()** to create a **Sign** instance, initialize the **Sign** instance, and set a private key for signing.
2. Create a **Sign** instance.<br>Call **createSign()** to create a **Sign** instance, initialize the **Sign** instance, and set a private key for signing.
3. Generate a signature.<br>Call **update()** provided by the **Sign** class to pass in the data for signing and call **sign()** to generate a signature.
3. Generate a signature.<br>Call **update()** provided by the **Sign** class to pass in the data for signing and call **sign()** to generate a signature.
4. Create a **Verify** instance.<br>Call **createVerify()** to create a **Verify** instance, initialize the instance, and set a public key for signature verification.
4. Create a **Verify** instance.<br>Call **createVerify()** to create a **Verify** instance, initialize the instance, and set a public key for signature verification.
5. Verify the signature.<br>Call **update()** provided by the **Verify** class to pass in the signature data and call **verify()** to verify the signature.
5. Verify the signature.<br>Call **update()** provided by the **Verify** class to pass in the signature data and call **verify()** to verify the signature.
letsigner=cryptoFramework.createSign("RSA1024|PKCS1|SHA256");// From API version 10, a Sign instance can be created by specifying a string parameter defining the key specifications.
letsigner=cryptoFramework.createSign("RSA1024|PKCS1|SHA256");// From API version 10, a Sign instance can be created by specifying a string parameter defining the key specifications.
letkeyGenPromise=rsaGenerator.generateKeyPair();
letkeyGenPromise=rsaGenerator.generateKeyPair();
keyGenPromise.then(keyPair=>{
keyGenPromise.then(keyPair=>{
globalKeyPair=keyPair;
globalKeyPair=keyPair;
letpriKey=globalKeyPair.priKey;
letpriKey=globalKeyPair.priKey;
returnsigner.init(priKey);
returnsigner.init(priKey);
...
@@ -1300,6 +1657,7 @@ function signMessagePromise() {
...
@@ -1300,6 +1657,7 @@ function signMessagePromise() {
@@ -1437,7 +1796,7 @@ Use RSA to sign data and verify the signature by segment.
...
@@ -1437,7 +1796,7 @@ Use RSA to sign data and verify the signature by segment.
2. Create a **Sign** instance.<br>Call **createSign()** to create a **Sign** instance, initialize the **Sign** instance, and set a private key for signing.
2. Create a **Sign** instance.<br>Call **createSign()** to create a **Sign** instance, initialize the **Sign** instance, and set a private key for signing.
3. Generate a signature.<br>Call the **update()** provided by the **Sign** class multiple times to pass in data by segment and call **sign()** to generate a signature.
3. Generate a signature.<br>Call the **update()** provided by the **Sign** class multiple times to pass in data by segment and call **sign()** to generate a signature.
4. Create a **Verify** instance.<br>Call **createVerify()** to create a **Verify** instance, initialize the instance, and set a public key for signature verification.
4. Create a **Verify** instance.<br>Call **createVerify()** to create a **Verify** instance, initialize the instance, and set a public key for signature verification.
5. Verify the signature.<br>Call the **update()** provided by the **Verify** class multiple times to pass in data by segment and call **verify()** to verify the signature.
5. Verify the signature.<br>Call the **update()**method provided by the **Verify** class multiple times to pass in data by segment and call **verify()** to verify the signature.
@@ -1603,10 +1968,72 @@ function verifyMessageCallbackPSS() {
...
@@ -1603,10 +1968,72 @@ function verifyMessageCallbackPSS() {
}
}
```
```
### Signing and Signature Verification Using SM2
> **NOTE**
>
> SM2 signing and signature verification are supported from API version 10.
Use SM2 to sign data and verify the signature.
1. Generate an SM2 key pair. Call **createAsyKeyGenerator()** to create an **AsyKeyGenerator** instance and generate an SM2 asymmetric key pair.
2. Create a **Sign** instance.<br>Call **createSign()** to create a **Sign** instance, initialize the **Sign** instance, and set a private key for signing.
3. Generate a signature.<br>Call **update()** provided by the **Sign** class to pass in the data for signing and call **doFinal()** to generate a signature.
4. Create a **Verify** instance.<br>Call **createVerify()** to create a **Verify** instance, initialize the instance, and set a public key for signature verification.
5. Verify the signature.<br>Call **update()** provided by the **Verify** class to pass in the signature data and call **doFinal()** to verify the signature.
| cryptoFramework | function createMac(algName : string) : Mac; | Creates a **Mac** instance. |
| cryptoFramework | function createMac(algName : string) : Mac; | Creates a **Mac** instance. |
| Mac | init(key : SymKey, callback : AsyncCallback\<void>) : void; | Initializes the **Mac** instance. This API uses an asynchronous callback to return the result.|
| Mac | init(key : SymKey, callback : AsyncCallback\<void>) : void; | Initializes the **Mac** instance. This API uses an asynchronous callback to return the result.|
| Mac | init(key : SymKey) : Promise\<void>; | Initializes the **Mac** instance. This API uses a promise to return the result. |
| Mac | init(key : SymKey) : Promise\<void>; | Initializes the **Mac** instance. This API uses a promise to return the result. |
| Mac | update(input : DataBlob, callback : AsyncCallback\<void>) : void; | Updates the data for the MAC operation. This API uses an asynchronous callback to return the result. |
| Mac | update(input : DataBlob, callback : AsyncCallback\<void>) : void; | Updates the data for the MAC operation. This API uses an asynchronous callback to return the result. |
| Mac | update(input : DataBlob) : Promise\<void>; | Updates the data for the MAC operation. This API uses a promise to return the result. |
| Mac | update(input : DataBlob) : Promise\<void>; | Updates the data for the MAC operation. This API uses a promise to return the result. |
| Mac | doFinal(callback : AsyncCallback\<DataBlob>) : void; | Finalizes the MAC operation to generate a MAC. This API uses an asynchronous callback to return the result. |
| Mac | doFinal(callback : AsyncCallback\<DataBlob>) : void; | Finalizes the MAC operation to generate a MAC. This API uses an asynchronous callback to return the result. |
| Mac | doFinal() : Promise\<DataBlob>; | Finalizes the MAC operation to generate a MAC. This API uses a promise to return the result. |
| Mac | doFinal() : Promise\<DataBlob>; | Finalizes the MAC operation to generate a MAC. This API uses a promise to return the result. |
| Mac | getMacLength() : number; | Obtains the length of the MAC based on the specified algorithm. |
| Mac | getMacLength() : number; | Obtains the length of the MAC based on the specified algorithm. |
| Mac | readonly algName : string; | Obtains the digest algorithm. |
| Mac | readonly algName : string; | Obtains the digest algorithm. |
...
@@ -1895,7 +2322,7 @@ function doHmacByPromise() {
...
@@ -1895,7 +2322,7 @@ function doHmacByPromise() {
console.info("[Promise]: Mac algName is: "+mac.algName);
console.info("[Promise]: Mac algName is: "+mac.algName);