1# Signing and Signature Verification by Segment with an RSA Key Pair (PKCS1 Mode) (ArkTS) 2 3 4For details about the algorithm specifications, see [RSA](crypto-sign-sig-verify-overview.md#rsa). 5 6 7**Signing** 8 9 101. Use [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator) and [AsyKeyGenerator.generateKeyPair](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypair-1) to generate a 1024-bit RSA key pair (**KeyPair**) with two primes. The **KeyPair** instance consists of a public key (**PubKey**) and a private key (**PriKey**). 11 12 In addition to the example in this topic, [RSA](crypto-asym-key-generation-conversion-spec.md#rsa) and [Randomly Generating an Asymmetric Key Pair](crypto-generate-asym-key-pair-randomly.md) may help you better understand how to generate an RSA asymmetric key pair. Note that the input parameters in the reference documents may be different from those in the example below. 13 142. Use [cryptoFramework.createSign](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatesign) with the string parameter **'RSA1024|PKCS1|SHA256'** to create a **Sign** instance. The key type is RSA1024, the padding mode is **PKCS1**, and the MD algorithm is **SHA256**. 15 163. Use [Sign.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-3) to initialize the **Sign** instance with the private key (**PriKey**). 17 184. Set the data length to be passed in each time to 64 bytes, and call [Sign.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-3) multiple times to pass in the data to be signed. 19 20 Currently, the amount of data to be passed in by a single **update()** is not limited. You can determine how to pass in data based on the data volume. 21 225. Use [Sign.sign](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#sign-2) to generate a signature. 23 24 25**Signature Verification** 26 27 281. Use [cryptoFramework.createVerify](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateverify) with the string parameter **'RSA1024|PKCS1|SHA256'** to create a **Verify** instance. The string parameter must be the same as that used to create the **Sign** instance. 29 302. Use [Verify.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-5) to initialize the **Verify** instance using the public key (**PubKey**). 31 323. Use [Verify.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-5) to pass in the data to be verified. 33 34 Currently, the amount of data to be passed in by a single **update()** is not limited. You can determine how to pass in data based on the data volume. 35 364. Use [Verify.verify](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#verify-2) to verify the data signature. 37 38 39- Example (using asynchronous APIs): 40 41 ```ts 42 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 43 import { buffer } from '@kit.ArkTS'; 44 45 async function signMessageBySegment(priKey: cryptoFramework.PriKey, plainText: Uint8Array) { 46 let signAlg = "RSA1024|PKCS1|SHA256"; 47 let signer = cryptoFramework.createSign(signAlg); 48 await signer.init(priKey); 49 let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64. 50 for (let i = 0; i < plainText.length; i += textSplitLen) { 51 let updateMessage = plainText.subarray(i, i + textSplitLen); 52 let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 53 // Call update() multiple times to pass in data by segment. 54 await signer.update(updateMessageBlob); 55 } 56 // Pass in null here because all the plaintext has been passed in by segment. 57 let signData = await signer.sign(null); 58 return signData; 59 } 60 async function verifyMessagBySegment(pubKey: cryptoFramework.PubKey, plainText: Uint8Array, signMessageBlob: cryptoFramework.DataBlob) { 61 let verifyAlg = "RSA1024|PKCS1|SHA256"; 62 let verifier = cryptoFramework.createVerify(verifyAlg); 63 await verifier.init(pubKey); 64 let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64. 65 for (let i = 0; i < plainText.length; i += textSplitLen) { 66 let updateMessage = plainText.subarray(i, i + textSplitLen); 67 let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 68 // Call update() multiple times to pass in data by segment. 69 await verifier.update(updateMessageBlob); 70 } 71 // Pass in null in the first parameter of verify() because all the plaintext has been passed in by segment. 72 let res = await verifier.verify(null, signMessageBlob); 73 console.info("verify result is " + res); 74 return res; 75 } 76 async function rsaSignatureBySegment() { 77 let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 78 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 79 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 80 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 81 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 82 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 83 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 84 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!"; 85 let keyGenAlg = "RSA1024"; 86 let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg); 87 let keyPair = await generator.generateKeyPair(); 88 let messageData = new Uint8Array(buffer.from(message, 'utf-8').buffer); 89 let signData = await signMessageBySegment(keyPair.priKey, messageData); 90 let verifyResult = await verifyMessagBySegment(keyPair.pubKey, messageData, signData); 91 if (verifyResult == true) { 92 console.info('verify success'); 93 } else { 94 console.error('verify failed'); 95 } 96 } 97 ``` 98 99- Example (using synchronous APIs): 100 101 ```ts 102 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 103 import { buffer } from '@kit.ArkTS'; 104 105 function signMessageBySegment(priKey: cryptoFramework.PriKey, plainText: Uint8Array) { 106 let signAlg = "RSA1024|PKCS1|SHA256"; 107 let signer = cryptoFramework.createSign(signAlg); 108 signer.initSync(priKey); 109 let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64. 110 for (let i = 0; i < plainText.length; i += textSplitLen) { 111 let updateMessage = plainText.subarray(i, i + textSplitLen); 112 let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 113 // Call update() multiple times to pass in data by segment. 114 signer.updateSync(updateMessageBlob); 115 } 116 // Pass in null here because all the plaintext has been passed in by segment. 117 let signData = signer.signSync(null); 118 return signData; 119 } 120 function verifyMessagBySegment(pubKey: cryptoFramework.PubKey, plainText: Uint8Array, signMessageBlob: cryptoFramework.DataBlob) { 121 let verifyAlg = "RSA1024|PKCS1|SHA256"; 122 let verifier = cryptoFramework.createVerify(verifyAlg); 123 verifier.initSync(pubKey); 124 let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64. 125 for (let i = 0; i < plainText.length; i += textSplitLen) { 126 let updateMessage = plainText.subarray(i, i + textSplitLen); 127 let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 128 // Call update() multiple times to pass in data by segment. 129 verifier.updateSync(updateMessageBlob); 130 } 131 // Pass in null in the first parameter of verify() because all the plaintext has been passed in by segment. 132 let res = verifier.verifySync(null, signMessageBlob); 133 console.info("verify result is " + res); 134 return res; 135 } 136 function rsaSignatureBySegment() { 137 let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 138 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 139 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 140 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 141 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 142 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 143 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" + 144 "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!"; 145 let keyGenAlg = "RSA1024"; 146 let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg); 147 let keyPair = generator.generateKeyPairSync(); 148 let messageData = new Uint8Array(buffer.from(message, 'utf-8').buffer); 149 let signData = signMessageBySegment(keyPair.priKey, messageData); 150 let verifyResult = verifyMessagBySegment(keyPair.pubKey, messageData, signData); 151 if (verifyResult == true) { 152 console.info('verify success'); 153 } else { 154 console.error('verify failed'); 155 } 156 } 157 ``` 158