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.<br>
19   Currently, the amount of data to be passed in by a single **Sign.update()** is not limited. You can determine how to pass in data based on the data volume.
20
215. Use [Sign.sign](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#sign-1) to generate a signature.
22
23
24**Signature Verification**
25
26
271. 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.
28
292. Use [Verify.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-5) to initialize the **Verify** instance using the public key (**PubKey**).
30
313. Use [Verify.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-5) to pass in the data to be verified.<br>
32   Currently, the amount of data to be passed in by a single **Verify.update()** is not limited. You can determine how to pass in data based on the data volume.
33
344. Use [Verify.verify](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#verify-1) to verify the data signature.
35
36
37- Example (using asynchronous APIs):
38
39  ```ts
40  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
41  import { buffer } from '@kit.ArkTS';
42
43  async function signMessageBySegment(priKey: cryptoFramework.PriKey, plainText: Uint8Array) {
44    let signAlg = "RSA1024|PKCS1|SHA256";
45    let signer = cryptoFramework.createSign(signAlg);
46    await signer.init(priKey);
47    let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
48    for (let i = 0; i < plainText.length; i += textSplitLen) {
49      let updateMessage = plainText.subarray(i, i + textSplitLen);
50      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
51      // Call update() multiple times to pass in data by segment.
52      await signer.update(updateMessageBlob);
53    }
54    // Pass in null here because all the plaintext has been passed in by segment.
55    let signData = await signer.sign(null);
56    return signData;
57  }
58  async function verifyMessagBySegment(pubKey: cryptoFramework.PubKey, plainText: Uint8Array, signMessageBlob: cryptoFramework.DataBlob) {
59    let verifyAlg = "RSA1024|PKCS1|SHA256";
60    let verifier = cryptoFramework.createVerify(verifyAlg);
61    await verifier.init(pubKey);
62    let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
63    for (let i = 0; i < plainText.length; i += textSplitLen) {
64      let updateMessage = plainText.subarray(i, i + textSplitLen);
65      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
66      // Call update() multiple times to pass in data by segment.
67      await verifier.update(updateMessageBlob);
68    }
69    // Pass in null in the first parameter of verify() because all the plaintext has been passed in by segment.
70    let res = await verifier.verify(null, signMessageBlob);
71    console.info("verify result is " + res);
72    return res;
73  }
74  async function rsaSignatureBySegment() {
75    let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
76      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
77      "This is a long plainTest! 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    let keyGenAlg = "RSA1024";
84    let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
85    let keyPair = await generator.generateKeyPair();
86    let messageData = new Uint8Array(buffer.from(message, 'utf-8').buffer);
87    let signData = await signMessageBySegment(keyPair.priKey, messageData);
88    let verifyResult = await verifyMessagBySegment(keyPair.pubKey, messageData, signData);
89    if (verifyResult == true) {
90      console.info('verify success');
91    } else {
92      console.error('verify failed');
93    }
94  }
95  ```
96
97- Example (using synchronous APIs):
98
99  ```ts
100  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
101  import { buffer } from '@kit.ArkTS';
102
103  function signMessageBySegment(priKey: cryptoFramework.PriKey, plainText: Uint8Array) {
104    let signAlg = "RSA1024|PKCS1|SHA256";
105    let signer = cryptoFramework.createSign(signAlg);
106    signer.initSync(priKey);
107    let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
108    for (let i = 0; i < plainText.length; i += textSplitLen) {
109      let updateMessage = plainText.subarray(i, i + textSplitLen);
110      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
111      // Call update() multiple times to pass in data by segment.
112      signer.updateSync(updateMessageBlob);
113    }
114    // Pass in null here because all the plaintext has been passed in by segment.
115    let signData = signer.signSync(null);
116    return signData;
117  }
118  function verifyMessagBySegment(pubKey: cryptoFramework.PubKey, plainText: Uint8Array, signMessageBlob: cryptoFramework.DataBlob) {
119    let verifyAlg = "RSA1024|PKCS1|SHA256";
120    let verifier = cryptoFramework.createVerify(verifyAlg);
121    verifier.initSync(pubKey);
122    let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
123    for (let i = 0; i < plainText.length; i += textSplitLen) {
124      let updateMessage = plainText.subarray(i, i + textSplitLen);
125      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
126      // Call update() multiple times to pass in data by segment.
127      verifier.updateSync(updateMessageBlob);
128    }
129    // Pass in null in the first parameter of verify() because all the plaintext has been passed in by segment.
130    let res = verifier.verifySync(null, signMessageBlob);
131    console.info("verify result is " + res);
132    return res;
133  }
134  function rsaSignatureBySegment() {
135    let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
136      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
137      "This is a long plainTest! 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    let keyGenAlg = "RSA1024";
144    let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
145    let keyPair = generator.generateKeyPairSync();
146    let messageData = new Uint8Array(buffer.from(message, 'utf-8').buffer);
147    let signData = signMessageBySegment(keyPair.priKey, messageData);
148    let verifyResult = verifyMessagBySegment(keyPair.pubKey, messageData, signData);
149    if (verifyResult == true) {
150      console.info('verify success');
151    } else {
152      console.error('verify failed');
153    }
154  }
155  ```
156