1# Signing and Signature Verification with an SM2 Key Pair (ArkTS) 2 3 4For details about the algorithm specifications, see [SM2](crypto-sign-sig-verify-overview.md#sm2). 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 256-bit key pair (**KeyPair**) using SM2. 11 12 In addition to the example in this topic, [SM2](crypto-asym-key-generation-conversion-spec.md#sm2) and [Randomly Generating an Asymmetric Key Pair](crypto-generate-asym-key-pair-randomly.md) may help you better understand how to generate an SM2 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 **'SM2_256|SM3'** to create a **Sign** instance. The key type is **SM2_256**, and the MD algorithm is **SM3**. 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 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 **'SM2_256|SM3'** to create a **Verify** instance. The key type is **SM2_256**, and MD algorithm is **SM3**. 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. 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 // The plaintext is split into input1 and input2. 43 let input1: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 44 let input2: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 45 async function signMessagePromise(priKey: cryptoFramework.PriKey) { 46 let signAlg = "SM2_256|SM3"; 47 let signer = cryptoFramework.createSign(signAlg); 48 await signer.init(priKey); 49 await signer.update(input1); // If the plaintext is short, you can use sign() to pass in the full data at a time. 50 let signData = await signer.sign(input2); 51 return signData; 52 } 53 async function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) { 54 let verifyAlg = "SM2_256|SM3"; 55 let verifier = cryptoFramework.createVerify(verifyAlg); 56 await verifier.init(pubKey); 57 await verifier.update(input1); // If the plaintext is short, you can use verify() to pass in the full data at a time. 58 let res = await verifier.verify(input2, signMessageBlob); 59 console.info("verify result is " + res); 60 return res; 61 } 62 async function main() { 63 let keyGenAlg = "SM2_256"; 64 let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg); 65 let keyPair = await generator.generateKeyPair(); 66 let signData = await signMessagePromise(keyPair.priKey); 67 let verifyResult = await verifyMessagePromise(signData, keyPair.pubKey); 68 if (verifyResult == true) { 69 console.info('verify success'); 70 } else { 71 console.error('verify failed'); 72 } 73 } 74 ``` 75 76- Example (using synchronous APIs): 77 78 ```ts 79 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 80 import { buffer } from '@kit.ArkTS'; 81 // The plaintext is split into input1 and input2. 82 let input1: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 83 let input2: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 84 function signMessagePromise(priKey: cryptoFramework.PriKey) { 85 let signAlg = "SM2_256|SM3"; 86 let signer = cryptoFramework.createSign(signAlg); 87 signer.initSync(priKey); 88 signer.updateSync(input1); // If the plaintext is short, you can use sign() to pass in the full data at a time. 89 let signData = signer.signSync(input2); 90 return signData; 91 } 92 function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) { 93 let verifyAlg = "SM2_256|SM3"; 94 let verifier = cryptoFramework.createVerify(verifyAlg); 95 verifier.initSync(pubKey); 96 verifier.updateSync(input1); // If the plaintext is short, you can use verify() to pass in the full data at a time. 97 let res = verifier.verifySync(input2, signMessageBlob); 98 console.info("verify result is " + res); 99 return res; 100 } 101 function main() { 102 let keyGenAlg = "SM2_256"; 103 let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg); 104 let keyPair = generator.generateKeyPairSync(); 105 let signData = signMessagePromise(keyPair.priKey); 106 let verifyResult = verifyMessagePromise(signData, keyPair.pubKey); 107 if (verifyResult == true) { 108 console.info('verify success'); 109 } else { 110 console.error('verify failed'); 111 } 112 } 113 ``` 114