1# Signing and Signature Verification 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. Use [Sign.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-3) to pass in the data to be signed. 19 20 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. 21 22 - If a small amount of data is to be signed, call **Sign.sign()** immediately after **Sign.init()**. 23 - If a large amount of data is to be signed, call **Sign.update()** multiple times to [pass in data by segment](crypto-rsa-sign-sig-verify-pkcs1-by-segment.md). 24 255. Use [Sign.sign](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#sign-1) to generate a signature. 26 27 28**Signature Verification** 29 30 311. 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. 32 332. Use [Verify.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-5) to initialize the **Verify** instance using the public key (**PubKey**). 34 353. Use [Verify.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-5) to pass in the data to be verified. 36Currently, 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. 37- If the data to be verified is short, call **Verify.verify()** immediately after **Verify.init()**. 38 - If a large amount of data is to be verified, call **Verify.update()** multiple times to [pass in data by segment](crypto-rsa-sign-sig-verify-pkcs1-by-segment.md). 39 404. Use [Verify.verify](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#verify-1) to verify the data signature. 41 42 43- Example (using asynchronous APIs): 44 45 ```ts 46 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 47 import { buffer } from '@kit.ArkTS'; 48 // The plaintext is split into input1 and input2. 49 let input1: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 50 let input2: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 51 async function signMessagePromise(priKey: cryptoFramework.PriKey) { 52 let signAlg = "RSA1024|PKCS1|SHA256"; 53 let signer = cryptoFramework.createSign(signAlg); 54 await signer.init(priKey); 55 await signer.update(input1); // If the plaintext is short, you can use sign() to pass in the full data at a time. 56 let signData = await signer.sign(input2); 57 return signData; 58 } 59 async function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) { 60 let verifyAlg = "RSA1024|PKCS1|SHA256"; 61 let verifier = cryptoFramework.createVerify(verifyAlg); 62 await verifier.init(pubKey); 63 await verifier.update(input1); // If the plaintext is short, you can use verify() to pass in the full data at a time. 64 let res = await verifier.verify(input2, signMessageBlob); 65 console.info("verify result is " + res); 66 return res; 67 } 68 async function main() { 69 let keyGenAlg = "RSA1024"; 70 let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg); 71 let keyPair = await generator.generateKeyPair(); 72 let signData = await signMessagePromise(keyPair.priKey); 73 let verifyResult = await verifyMessagePromise(signData, keyPair.pubKey); 74 if (verifyResult == true) { 75 console.info('verify success'); 76 } else { 77 console.error('verify failed'); 78 } 79 } 80 ``` 81 82- Example (using synchronous APIs): 83 84 ```ts 85 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 86 import { buffer } from '@kit.ArkTS'; 87 // The plaintext is split into input1 and input2. 88 let input1: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 89 let input2: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 90 function signMessagePromise(priKey: cryptoFramework.PriKey) { 91 let signAlg = "RSA1024|PKCS1|SHA256"; 92 let signer = cryptoFramework.createSign(signAlg); 93 signer.initSync(priKey); 94 signer.updateSync(input1); // If the plaintext is short, you can use sign() to pass in the full data at a time. 95 let signData = signer.signSync(input2); 96 return signData; 97 } 98 function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) { 99 let verifyAlg = "RSA1024|PKCS1|SHA256"; 100 let verifier = cryptoFramework.createVerify(verifyAlg); 101 verifier.initSync(pubKey); 102 verifier.updateSync(input1); // If the plaintext is short, you can use verify() to pass in the full data at a time. 103 let res = verifier.verifySync(input2, signMessageBlob); 104 console.info("verify result is " + res); 105 return res; 106 } 107 function main() { 108 let keyGenAlg = "RSA1024"; 109 let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg); 110 let keyPair = generator.generateKeyPairSync(); 111 let signData = signMessagePromise(keyPair.priKey); 112 let verifyResult = verifyMessagePromise(signData, keyPair.pubKey); 113 if (verifyResult == true) { 114 console.info('verify success'); 115 } else { 116 console.error('verify failed'); 117 } 118 } 119 ``` 120