Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Phương thức crypto.createDiffieHellman(primeLength, [generator]) trong Node.js

Mô tả

Phương thức crypto.createDiffieHellmanGroup(primeLength, [generator]) dùng để tạo đối tượng trao đổi khóa sinh ra số nguyên tố có độ dài primeLength bit dùng generator số. Giá trị mặc định là 2 khi generator không được định nghĩa.

Cú pháp

crypto.createDiffieHelmmanGroup(primeLength, [generator])

Tham số

  • primeLength – Số bit nguyên tố sẽ được sinh. Giá trị đầu vào kiểu number.

  • generator – Generator để tạo đối tượng trao đổi khóa. Giá trị mặc định: 2.

Ví dụ

Tạo file tên index.js và sao chép đoạn mã dưới. Sau khi tạo file, dùng lệnh sau để chạy:

node index.js

index.js

// crypto.createDiffieHellman(primeLength, [generator]) Demo Example // Importing the crypto module const crypto = require('crypto'); // Initializing the variable primeLength var primeLength = 29; // Creating DiffieHellman keyexchange object var exchangeKey = crypto.createDiffieHellman(primeLength); // Printing the exchange keys console.log("DiffieHellman key is: " + exchangeKey.generateKeys('base64'));

Đầu ra

C:\home\node>> node index.js DiffieHellman key is: BaRoaA==

Ví dụ khác

Xem thêm một ví dụ nữa.

// crypto.createDiffieHellman(primeLength, [generator]) Demo Example // Importing the crypto module const crypto = require('crypto'); // Initializing the variable primeLength var primeLength = 29; var generator = 3; //Default value is 2 // Creating DiffieHellman keyexchange object var exchangeKey = crypto.createDiffieHellman(primeLength, generator); // Printing the exchange keys console.log("DiffieHellman keys are: " + exchangeKey.generateKeys('hex')); // Displays public and private keys console.log("Public Key is: ", exchangeKey.getPublicKey('hex')); console.log("Private Key: ", exchangeKey.getPrivateKey('hex'));

Đầu ra

C:\home\node>> node index.js DiffieHellman keys are: 1a21670d Public Key is: 1a21670d Private Key: 0d4a1a3c