crypto.createDiffieHellmanGroup()은 미리 결정된 DiffieHellmanGroup 키 교환 객체를 생성하는 데 사용됩니다. 지원되는 DiffieHellmanGroups 중 일부는 modp1, modp2, modp5, modp 14, modp16, modp17 등입니다. 이 방법을 사용하면 당사자가 그룹 계수를 생성하거나 교환할 필요가 없으므로 처리 시간이 절약된다는 이점이 있습니다.
구문
crypto.getDiffieHelmmanGroup(groupName)
매개변수
위의 매개변수는 다음과 같이 설명됩니다. -
-
그룹 이름 – 그룹명을 입력 받습니다. 입력은 '문자열' 유형입니다.
예시
이름이 getdiffieHellman.js인 파일을 만들고 아래 코드 조각을 복사합니다. 파일을 생성한 후 다음 명령을 사용하여 아래 예와 같이 이 코드를 실행하십시오 -
node getDiffieHellman.js
getdiffieHellman.js
// crypto.getDiffieHellman() Demo Example
// Importing the crypto module
const crypto = require('crypto');
const server = crypto.getDiffieHellman('modp1');
const client = crypto.getDiffieHellman('modp1');
// Printing DiffieHellman values
console.log(server);
console.log(client);
// Generating public and private keys
server.generateKeys();
client.generateKeys();
// Gettong public key
const serverSecret = server.computeSecret(client.getPublicKey(), null, 'hex');
const clientSecret = client.computeSecret(server.getPublicKey(), null, 'hex');
/* aliceSecret and bobSecret should be the same */
console.log(serverSecret === clientSecret); 출력
C:\home\node>> node getDiffieHellman.js
DiffieHellmanGroup { _handle: { verifyError: [Getter] }, verifyError: 0 }
DiffieHellmanGroup { _handle: { verifyError: [Getter] }, verifyError: 0 }
true 예시
예를 하나 더 살펴보겠습니다.
// crypto.getDiffieHellman() Demo Example
// Importing the crypto module
const crypto = require('crypto');
const dh1 = crypto.getDiffieHellman('modp17');
const dh2 = crypto.getDiffieHellman('modp14');
// Generating public and private keys
dh1.generateKeys();
dh2.generateKeys();
// Gettong public key
const dh1Key = dh1.computeSecret(dh2.getPublicKey(), null, 'hex');
const dh2Key = dh2.computeSecret(dh1.getPublicKey(), null, 'hex');
/* aliceSecret and bobSecret should be the same */
console.log(dh1Key === dh2Key); 출력
C:\home\node>> node getDiffieHellman.js internal/crypto/diffiehellman.js:102 const ret = this._handle.computeSecret(toBuf(key, inEnc)); ^ Error: Supplied key is too large at DiffieHellmanGroup.dhComputeSecret [as computeSecret] (internal/crypto/diffiehellman.js:102:28) at Object.<anonymous> (/home/node/test/getDiffieHellman .js:15:20) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)