1e41f4b71Sopenharmony_ci# Signing and Signature Verification by Segment with an RSA Key Pair (PKCS1 Mode) (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [RSA](crypto-sign-sig-verify-overview.md#rsa).
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci**Signing**
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci1. 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**).
11e41f4b71Sopenharmony_ci   
12e41f4b71Sopenharmony_ci   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.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci2. 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**.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci3. Use [Sign.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-3) to initialize the **Sign** instance with the private key (**PriKey**).
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci4. 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.
19e41f4b71Sopenharmony_ci   
20e41f4b71Sopenharmony_ci   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.
21e41f4b71Sopenharmony_ci   
22e41f4b71Sopenharmony_ci5. Use [Sign.sign](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#sign-2) to generate a signature.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**Signature Verification**
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci1. 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.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci2. Use [Verify.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-5) to initialize the **Verify** instance using the public key (**PubKey**).
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci3. Use [Verify.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-5) to pass in the data to be verified.
33e41f4b71Sopenharmony_ci   
34e41f4b71Sopenharmony_ci   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.
35e41f4b71Sopenharmony_ci   
36e41f4b71Sopenharmony_ci4. Use [Verify.verify](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#verify-2) to verify the data signature.
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci- Example (using asynchronous APIs):
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci  ```ts
42e41f4b71Sopenharmony_ci  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
43e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci  async function signMessageBySegment(priKey: cryptoFramework.PriKey, plainText: Uint8Array) {
46e41f4b71Sopenharmony_ci    let signAlg = "RSA1024|PKCS1|SHA256";
47e41f4b71Sopenharmony_ci    let signer = cryptoFramework.createSign(signAlg);
48e41f4b71Sopenharmony_ci    await signer.init(priKey);
49e41f4b71Sopenharmony_ci    let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
50e41f4b71Sopenharmony_ci    for (let i = 0; i < plainText.length; i += textSplitLen) {
51e41f4b71Sopenharmony_ci      let updateMessage = plainText.subarray(i, i + textSplitLen);
52e41f4b71Sopenharmony_ci      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
53e41f4b71Sopenharmony_ci      // Call update() multiple times to pass in data by segment.
54e41f4b71Sopenharmony_ci      await signer.update(updateMessageBlob);
55e41f4b71Sopenharmony_ci    }
56e41f4b71Sopenharmony_ci    // Pass in null here because all the plaintext has been passed in by segment.
57e41f4b71Sopenharmony_ci    let signData = await signer.sign(null);
58e41f4b71Sopenharmony_ci    return signData;
59e41f4b71Sopenharmony_ci  }
60e41f4b71Sopenharmony_ci  async function verifyMessagBySegment(pubKey: cryptoFramework.PubKey, plainText: Uint8Array, signMessageBlob: cryptoFramework.DataBlob) {
61e41f4b71Sopenharmony_ci    let verifyAlg = "RSA1024|PKCS1|SHA256";
62e41f4b71Sopenharmony_ci    let verifier = cryptoFramework.createVerify(verifyAlg);
63e41f4b71Sopenharmony_ci    await verifier.init(pubKey);
64e41f4b71Sopenharmony_ci    let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
65e41f4b71Sopenharmony_ci    for (let i = 0; i < plainText.length; i += textSplitLen) {
66e41f4b71Sopenharmony_ci      let updateMessage = plainText.subarray(i, i + textSplitLen);
67e41f4b71Sopenharmony_ci      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
68e41f4b71Sopenharmony_ci      // Call update() multiple times to pass in data by segment.
69e41f4b71Sopenharmony_ci      await verifier.update(updateMessageBlob);
70e41f4b71Sopenharmony_ci    }
71e41f4b71Sopenharmony_ci    // Pass in null in the first parameter of verify() because all the plaintext has been passed in by segment.
72e41f4b71Sopenharmony_ci    let res = await verifier.verify(null, signMessageBlob);
73e41f4b71Sopenharmony_ci    console.info("verify result is " + res);
74e41f4b71Sopenharmony_ci    return res;
75e41f4b71Sopenharmony_ci  }
76e41f4b71Sopenharmony_ci  async function rsaSignatureBySegment() {
77e41f4b71Sopenharmony_ci    let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
78e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
79e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
80e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
81e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
82e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
83e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
84e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!";
85e41f4b71Sopenharmony_ci    let keyGenAlg = "RSA1024";
86e41f4b71Sopenharmony_ci    let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
87e41f4b71Sopenharmony_ci    let keyPair = await generator.generateKeyPair();
88e41f4b71Sopenharmony_ci    let messageData = new Uint8Array(buffer.from(message, 'utf-8').buffer);
89e41f4b71Sopenharmony_ci    let signData = await signMessageBySegment(keyPair.priKey, messageData);
90e41f4b71Sopenharmony_ci    let verifyResult = await verifyMessagBySegment(keyPair.pubKey, messageData, signData);
91e41f4b71Sopenharmony_ci    if (verifyResult == true) {
92e41f4b71Sopenharmony_ci      console.info('verify success');
93e41f4b71Sopenharmony_ci    } else {
94e41f4b71Sopenharmony_ci      console.error('verify failed');
95e41f4b71Sopenharmony_ci    }
96e41f4b71Sopenharmony_ci  }
97e41f4b71Sopenharmony_ci  ```
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci- Example (using synchronous APIs):
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci  ```ts
102e41f4b71Sopenharmony_ci  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
103e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci  function signMessageBySegment(priKey: cryptoFramework.PriKey, plainText: Uint8Array) {
106e41f4b71Sopenharmony_ci    let signAlg = "RSA1024|PKCS1|SHA256";
107e41f4b71Sopenharmony_ci    let signer = cryptoFramework.createSign(signAlg);
108e41f4b71Sopenharmony_ci    signer.initSync(priKey);
109e41f4b71Sopenharmony_ci    let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
110e41f4b71Sopenharmony_ci    for (let i = 0; i < plainText.length; i += textSplitLen) {
111e41f4b71Sopenharmony_ci      let updateMessage = plainText.subarray(i, i + textSplitLen);
112e41f4b71Sopenharmony_ci      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
113e41f4b71Sopenharmony_ci      // Call update() multiple times to pass in data by segment.
114e41f4b71Sopenharmony_ci      signer.updateSync(updateMessageBlob);
115e41f4b71Sopenharmony_ci    }
116e41f4b71Sopenharmony_ci    // Pass in null here because all the plaintext has been passed in by segment.
117e41f4b71Sopenharmony_ci    let signData = signer.signSync(null);
118e41f4b71Sopenharmony_ci    return signData;
119e41f4b71Sopenharmony_ci  }
120e41f4b71Sopenharmony_ci  function verifyMessagBySegment(pubKey: cryptoFramework.PubKey, plainText: Uint8Array, signMessageBlob: cryptoFramework.DataBlob) {
121e41f4b71Sopenharmony_ci    let verifyAlg = "RSA1024|PKCS1|SHA256";
122e41f4b71Sopenharmony_ci    let verifier = cryptoFramework.createVerify(verifyAlg);
123e41f4b71Sopenharmony_ci    verifier.initSync(pubKey);
124e41f4b71Sopenharmony_ci    let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
125e41f4b71Sopenharmony_ci    for (let i = 0; i < plainText.length; i += textSplitLen) {
126e41f4b71Sopenharmony_ci      let updateMessage = plainText.subarray(i, i + textSplitLen);
127e41f4b71Sopenharmony_ci      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
128e41f4b71Sopenharmony_ci      // Call update() multiple times to pass in data by segment.
129e41f4b71Sopenharmony_ci      verifier.updateSync(updateMessageBlob);
130e41f4b71Sopenharmony_ci    }
131e41f4b71Sopenharmony_ci    // Pass in null in the first parameter of verify() because all the plaintext has been passed in by segment.
132e41f4b71Sopenharmony_ci    let res = verifier.verifySync(null, signMessageBlob);
133e41f4b71Sopenharmony_ci    console.info("verify result is " + res);
134e41f4b71Sopenharmony_ci    return res;
135e41f4b71Sopenharmony_ci  }
136e41f4b71Sopenharmony_ci  function rsaSignatureBySegment() {
137e41f4b71Sopenharmony_ci    let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
138e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
139e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
140e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
141e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
142e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
143e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
144e41f4b71Sopenharmony_ci      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!";
145e41f4b71Sopenharmony_ci    let keyGenAlg = "RSA1024";
146e41f4b71Sopenharmony_ci    let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
147e41f4b71Sopenharmony_ci    let keyPair = generator.generateKeyPairSync();
148e41f4b71Sopenharmony_ci    let messageData = new Uint8Array(buffer.from(message, 'utf-8').buffer);
149e41f4b71Sopenharmony_ci    let signData = signMessageBySegment(keyPair.priKey, messageData);
150e41f4b71Sopenharmony_ci    let verifyResult = verifyMessagBySegment(keyPair.pubKey, messageData, signData);
151e41f4b71Sopenharmony_ci    if (verifyResult == true) {
152e41f4b71Sopenharmony_ci      console.info('verify success');
153e41f4b71Sopenharmony_ci    } else {
154e41f4b71Sopenharmony_ci      console.error('verify failed');
155e41f4b71Sopenharmony_ci    }
156e41f4b71Sopenharmony_ci  }
157e41f4b71Sopenharmony_ci  ```
158