1# Signing and Signature Verification with an ECDSA Key Pair 2 3 4For details about the algorithm specifications, see [ECDSA](crypto-sign-sig-verify-overview.md#ecdsa). 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 ECC. 11 12 In addition to the example in this topic, [ECC](crypto-asym-key-generation-conversion-spec.md#ecc) and [Randomly Generating an Asymmetric Key Pair](crypto-generate-asym-key-pair-randomly.md) may help you better understand how to generate an ECC 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 **'ECC256|SHA256'** to create a **Sign** instance. The key type is **ECC256**, 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 Currently, the data to be passed in by a single **update()** is not size bound. 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-2) 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 **'ECC256|SHA256'** to create a **Verify** instance. The key type is **ECC256**, and MD algorithm is **SHA256**. 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 data to be passed in by a single **update()** is not size bound. 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-2) to verify the data signature. 35 36 37```ts 38import cryptoFramework from '@ohos.security.cryptoFramework'; 39import buffer from '@ohos.buffer'; 40// The plaintext is split into input1 and input2. 41let input1: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; 42let input2: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; 43async function signMessagePromise(priKey: cryptoFramework.PriKey) { 44 let signAlg = "ECC256|SHA256"; 45 let signer = cryptoFramework.createSign(signAlg); 46 await signer.init(priKey); 47 await signer.update(input1); // If the plaintext is short, you can use sign() to pass in the full data at a time. 48 let signData = await signer.sign(input2); 49 return signData; 50} 51async function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) { 52 let verifyAlg = "ECC256|SHA256"; 53 let verifier = cryptoFramework.createVerify(verifyAlg); 54 await verifier.init(pubKey); 55 await verifier.update(input1); // If the plaintext is short, you can use verify() to pass in the full data at a time. 56 let res = await verifier.verify(input2, signMessageBlob); 57 console.info("verify result is " + res); 58 return res; 59} 60async function main() { 61 let keyGenAlg = "ECC256"; 62 let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg); 63 let keyPair = await generator.generateKeyPair(); 64 let signData = await signMessagePromise(keyPair.priKey); 65 let verifyResult = await verifyMessagePromise(signData, keyPair.pubKey); 66 if (verifyResult == true) { 67 console.info('verify success'); 68 } else { 69 console.error('verify failed'); 70 } 71} 72``` 73