Search code examples
cryptographydiffie-hellmannode.js

How to use Diffie Hellman algorithm at the browser?


I want to use nodeJS as a server side language. NodeJS have crypto module where DiffieHellman is a class. So, I can use this method to generate key and compute key.

But, client also need to create another instance of diffiehellman class. But how to do that? Can I use crypto module on client side? If yes then how, any solution? Here are my client side code...

const crypto = require('crypto');
const express = require('express');
const app = express();

// Generate server's keys...
const server = crypto.createDiffieHellman(139);
const serverKey = server.generateKeys();
//send p=prime and g=generator to the client

Solution

  • Not sure what you're planning to achieve here, but it appears there's such port for browser: https://www.npmjs.com/package/crypto-browserify. createDiffieHellman is on the list of already ported modules. Especially take a look at the example: https://github.com/crypto-browserify/crypto-browserify/tree/master/example. Instead of createHash I believe you could try invoking createDiffieHellman.