Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Add Diffie-Hellman support to crypto module
Browse files Browse the repository at this point in the history
Fixes #573
  • Loading branch information
havard authored and ry committed May 6, 2011
1 parent 00aee73 commit 9f0b1a9
Show file tree
Hide file tree
Showing 4 changed files with 607 additions and 0 deletions.
55 changes: 55 additions & 0 deletions doc/api/crypto.markdown
Expand Up @@ -146,3 +146,58 @@ the PEM encoded certificate, and `signature`, which is the previously calculates
signature for the data, in the `signature_format` which can be `'binary'`, `'hex'` or `'base64'`.

Returns true or false depending on the validity of the signature for the data and public key.

### crypto.createDiffieHellman(prime_length)

Creates a Diffie-Hellman key exchange object and generates a prime of the
given bit length. The generator used is `2`.

### crypto.createDiffieHellman(prime, encoding='binary')

Creates a Diffie-Hellman key exchange object using the supplied prime. The
generator used is `2`. Encoding can be `'binary'`, `'hex'`, or `'base64'`.

### diffieHellman.generateKeys(encoding='binary')

Generates private and public Diffie-Hellman key values, and returns the
public key in the specified encoding. This key should be transferred to the
other party. Encoding can be `'binary'`, `'hex'`, or `'base64'`.

### diffieHellman.computeSecret(other_public_key, input_encoding='binary', output_encoding=input_encoding)

Computes the shared secret using `other_public_key` as the other party's
public key and returns the computed shared secret. Supplied key is
interpreted using specified `input_encoding`, and secret is encoded using
specified `output_encoding`. Encodings can be `'binary'`, `'hex'`, or
`'base64'`. If no output encoding is given, the input encoding is used as
output encoding.

### diffieHellman.getPrime(encoding='binary')

Returns the Diffie-Hellman prime in the specified encoding, which can be
`'binary'`, `'hex'`, or `'base64'`.

### diffieHellman.getGenerator(encoding='binary')

Returns the Diffie-Hellman prime in the specified encoding, which can be
`'binary'`, `'hex'`, or `'base64'`.

### diffieHellman.getPublicKey(encoding='binary')

Returns the Diffie-Hellman public key in the specified encoding, which can
be `'binary'`, `'hex'`, or `'base64'`.

### diffieHellman.getPrivateKey(encoding='binary')

Returns the Diffie-Hellman private key in the specified encoding, which can
be `'binary'`, `'hex'`, or `'base64'`.

### diffieHellman.setPublicKey(public_key, encoding='binary')

Sets the Diffie-Hellman public key. Key encoding can be `'binary'`, `'hex'`,
or `'base64'`.

### diffieHellman.setPrivateKey(public_key, encoding='binary')

Sets the Diffie-Hellman private key. Key encoding can be `'binary'`, `'hex'`, or `'base64'`.

13 changes: 13 additions & 0 deletions lib/crypto.js
Expand Up @@ -29,6 +29,7 @@ try {
var Decipher = binding.Decipher;
var Sign = binding.Sign;
var Verify = binding.Verify;
var DiffieHellman = binding.DiffieHellman;
var crypto = true;
} catch (e) {

Expand Down Expand Up @@ -139,3 +140,15 @@ exports.Verify = Verify;
exports.createVerify = function(algorithm) {
return (new Verify).init(algorithm);
};

exports.DiffieHellman = DiffieHellman;
exports.createDiffieHellman = function(size_or_key, enc) {
if (!size_or_key) {
return new DiffieHellman();
} else if (!enc) {
return new DiffieHellman(size_or_key);
} else {
return new DiffieHellman(size_or_key, enc);
}

}

1 comment on commit 9f0b1a9

@aheckmann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Please sign in to comment.