dhkea-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Dhkea.JS

A high performance javascript implementation of Diffie–Hellman Key Exchange algorithm. Dhkea.JS implements all MODP groups defined in RFC2409 and RFC3526.

Features

  • 🐰 Implements all MODP groups defined in RFC2409 and RFC3526
  • 🐰 High performance
  • 🐰 Support both Node.JS and browser side
  • 🐰 Full typescript support is provided

Installation

npm install dhkea-js

If you are using yarn, you can use

yarn add dhkea-js

instead.

Usage

  const { generateIndividualKey, getSharedKey } = require('dhkea-js');

  // Alice's individual key, containing private key and public key
  const aliceKey = generateIndividualKey(); 

  // Bob's individual key
  const bobKey = generateIndividualKey();

  // Alice can get the shared key via her private key and bob's public key
  const sharedKeyCaculatedByAlice = getSharedKey(aliceKey.privateKey, bobKey.publicKey);

  // Bob can get the shared key via his private key and alice's public key
  const sharedKeyCaculatedByBob = getSharedKey(bobKey.privateKey, aliceKey.publicKey);

  // Diffie–Hellman Key Exchange algorithm gurantees that these two keys are the same
  console.log(sharedKeyCaculatedByAlice === sharedKeyCaculatedByBob); // true

MODP Group

MODP group is first defined in RFC2409 to do the Diffie-Hellman Key Exchange and is later extended in RFC 3526. Each MODP group contains the prime and its generator.

Dhkea.JS implements all the MODP groups defined in RFC2409 and RFC3526, with id 1, 2, 5, 14, 15, 16, 17, 18 whose bit length of the prime is 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192 respectively.

API

generateIndividualKey()

Syntax

  generateIndiviualKey(bitLen?: ModpBitLen): {
    privateKey: bigint;
    publicKey: bigInt;
  }

Generate an indiviual key pair with the MODP group with bit length bitLen. If bitLen is omited, the 2048-bits MODP Group(id = 14) will be used.

getSharedKey()

Syntax

getSharedKey(myPrivatedKey: bigint, theirPublicKey: bigint, bitLen?: ModpBitLen): bigint

Calculate the shared keys from my private key and their public key, using the MODP Group with bit length bitLen. If bitLen is omited, use the default 2048-bits MODP Group(id = 14).

getModpGroupInfo()

Syntax

getModpGroupInfo(bitLen: ModpBitLen): {
  id: ModpGroupId;
  generator: bigint;
  bitLen: ModpBitLen;
  prime: bigint;
}

Get the info of the MODP Group with bit length bitLen.

License

Dhkea.JS is licensed under GNU Lesser General Public License 2.1.

Readme

Keywords

none

Package Sidebar

Install

npm i dhkea-js

Weekly Downloads

1

Version

1.0.0

License

LGPL-2.1-only

Unpacked Size

41.1 kB

Total Files

11

Last publish

Collaborators

  • cherrai