1# Key Agreement Using DH 2 3 4For details about the algorithm specifications, see [DH](crypto-key-agreement-overview.md#dh). 5 6 7## How to Develop 8 91. 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 DH asymmetric key pair (**KeyPair**) with the named DH group **modp1536**. 10 In addition to the example in this topic, [DH](crypto-asym-key-generation-conversion-spec.md#dh) and [Randomly Generating an Asymmetric Key Pair](crypto-generate-asym-key-pair-randomly.md) may help you better understand how to generate a DH asymmetric key pair. Note that the input parameters in the reference documents may be different from those in the example below. 11 122. Use [cryptoFramework.createKeyAgreement](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatekeyagreement) with the string parameter **'DH_modp1536'** to create a key agreement (**KeyAgreement**) instance. 13 143. Use [KeyAgreement.generateSecret](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatesecret-1) to perform key agreement with the specified private key (**KeyPair.priKey**) and public key (**KeyPair.pubKey**), and return the shared secret. 15 16- Example: Perform key agreement using **await**. 17 18 ```ts 19 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 20 21 async function dhAwait() { 22 let keyGen = cryptoFramework.createAsyKeyGenerator('DH_modp1536'); 23 // Randomly generate public-private key pair A. 24 let keyPairA = await keyGen.generateKeyPair(); 25 // Randomly generate public-private key pair B with the same specifications. 26 let keyPairB = await keyGen.generateKeyPair(); 27 let keyAgreement = cryptoFramework.createKeyAgreement('DH_modp1536'); 28 // Use the public key of A and the private key of B to perform key agreement. 29 let secret1 = await keyAgreement.generateSecret(keyPairB.priKey, keyPairA.pubKey); 30 // Use the private key of A and the public key of B to perform key agreement. 31 let secret2 = await keyAgreement.generateSecret(keyPairA.priKey, keyPairB.pubKey); 32 // The two key agreement results should be the same. 33 if (secret1.data.toString() == secret2.data.toString()) { 34 console.info('DH success'); 35 console.info('DH output is ' + secret1.data); 36 } else { 37 console.error('DH result is not equal'); 38 } 39 } 40 ``` 41 42- Example (using synchronous APIs): 43 44 ```ts 45 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 46 47 function dhAwait() { 48 let keyGen = cryptoFramework.createAsyKeyGenerator('DH_modp1536'); 49 // Randomly generate public-private key pair A. 50 let keyPairA = keyGen.generateKeyPairSync(); 51 // Randomly generate public-private key pair B with the same specifications. 52 let keyPairB = keyGen.generateKeyPairSync(); 53 let keyAgreement = cryptoFramework.createKeyAgreement('DH_modp1536'); 54 // Use the public key of A and the private key of B to perform key agreement. 55 let secret1 = keyAgreement.generateSecretSync(keyPairB.priKey, keyPairA.pubKey); 56 // Use the private key of A and the public key of B to perform key agreement. 57 let secret2 = keyAgreement.generateSecretSync(keyPairA.priKey, keyPairB.pubKey); 58 // The two key agreement results should be the same. 59 if (secret1.data.toString() == secret2.data.toString()) { 60 console.info('DH success'); 61 console.info('DH output is ' + secret1.data); 62 } else { 63 console.error('DH result is not equal'); 64 } 65 } 66 ``` 67