Skip to content

daack/dh-key-exchange

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Diffie Hellman Key Exchange 🔑

Build Status

Diffie Hellman key exchange framework

Install

To install diffie-hellman-key-exchange, simply use npm:

npm install diffie-hellman-key-exchange --save

Example

Alice

const Dh = require('diffie-hellman-key-exchange')

const alice = Dh('alice', {
  prime: 'prime',
  listen: 8000,
  log_level: 'info',
  apps: {
    'bob': {
      host: '127.0.0.1',
      port: '8001'
    }
  }
})

// on incoming message

const decrypted = alice.decrypt('bob', message)

Bob

const Dh = require('diffie-hellman-key-exchange')

const bob = Dh('bob', {
  prime: 'prime',
  listen: 8001,
  apps: {
    'alice': {
      host: '127.0.0.1',
      port: '8000'
    }
  }
})

if (bob.getAppPublicKey('alice')) {
  const message = this.encrypt('alice', 'hello')

  // send message
} else {
  bob.initalizeSession('alice', function(err, secret) {
    const message = this.encrypt('alice', 'hello')

    // send message
  })
}

Swimming Bob

const Swim = require('swim')
const Dh = require('diffie-hellman-key-exchange')

const swim = new Swim({
    local: {
        host: 'my_host:port',
        meta: {
          // must have this object in order to communicate my connection info
          dh: {
            name: 'bob',
            host: 'my_host',
            port: 8001
          }
        }
    }
})

swim.bootstrap(hostsToJoin)

const bob = Dh('bob', {
  prime: 'prime',
  listen: 8001,
  apps: swim // it will load all the app from swim
})

API


Dh(app_name, opts)

Creates a new instance of Dh.

  • app_name, the unique name of this app
  • opts
    • prime, the prime for DH
    • generator, the generator for DH
    • listen, on which port the application will listen for the handshake public key exchange [default: 8000]
    • log_level, log level for the pino instance [default: warn]
    • crypter
      • algorithm, algorithm used to encrypt [default: aes-256-ctr]
    • apps, could be a Swim instance or object that contains all the apps [es: 'bob': { host: '127.0.0.1', port: 8000 } ]

instance.createDH()

Set a new DH instance, available by the dh attribute. [es: instance.dh]


instance.generateNewKeys()

Generate a new pair of keys


instance.initalizeSession(other_app_name, cb)

Initialize the public key (each other) in order to start the communication

  • other_app_name, app that i want to communicate
  • cb, function(err, secret_key) { assert.ok(this instanceof Dh) }

instance.addApp(app_name, opts)

Add one app connection to che instance configuration

  • app_name
  • opts, [es: 'bob': { host: '127.0.0.1', port: 8000 } ]

instance.setAppPublicKey(app_name, public_key)

Set the public key for the specified app

  • app_name
  • public_key, [type: 'hex']

instance.getAppPublicKey(app_name)

Return the public key if present

  • app_name

instance.encrypt(app_name, data)

Encrypt data for the given app name

  • app_name
  • data, [type: 'string']

instance.decrypt(app_name, data)

Decrypt data with the public key of the given app name

  • app_name
  • data, [type: 'string']

instance.dh

Attribute with the DH nodejs crypto class


instance.crypter

Attribute with the Crypter class

Methods:

  • encrypt(data, secret)
  • decrypt(data, secret)