Diffie Hellman Key Exchange 🔑
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 = const alice = // on incoming message const decrypted = alice
Bob
const Dh = const bob = if bob const message = this // send message else bob
Swimming Bob
const Swim = const Dh = const 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 const bob =
API
Dh()
instance.createDH()
instance.generateNewKeys()
instance.initalizeSession()
instance.addApp()
instance.setAppPublicKey()
instance.getAppPublicKey()
instance.encrypt()
instance.decrypt()
instance.dh
instance.crypter
Dh(app_name, opts)
Creates a new instance of Dh.
app_name
, the unique name of this appopts
prime
, the prime for DHgenerator
, the generator for DHlisten
, 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 communicatecb
, 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)