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

Commit 9f0b1a9

Browse files
havardry
authored andcommitted
Add Diffie-Hellman support to crypto module
Fixes #573
1 parent 00aee73 commit 9f0b1a9

File tree

4 files changed

+607
-0
lines changed

4 files changed

+607
-0
lines changed

doc/api/crypto.markdown

+55
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,58 @@ the PEM encoded certificate, and `signature`, which is the previously calculates
146146
signature for the data, in the `signature_format` which can be `'binary'`, `'hex'` or `'base64'`.
147147

148148
Returns true or false depending on the validity of the signature for the data and public key.
149+
150+
### crypto.createDiffieHellman(prime_length)
151+
152+
Creates a Diffie-Hellman key exchange object and generates a prime of the
153+
given bit length. The generator used is `2`.
154+
155+
### crypto.createDiffieHellman(prime, encoding='binary')
156+
157+
Creates a Diffie-Hellman key exchange object using the supplied prime. The
158+
generator used is `2`. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
159+
160+
### diffieHellman.generateKeys(encoding='binary')
161+
162+
Generates private and public Diffie-Hellman key values, and returns the
163+
public key in the specified encoding. This key should be transferred to the
164+
other party. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
165+
166+
### diffieHellman.computeSecret(other_public_key, input_encoding='binary', output_encoding=input_encoding)
167+
168+
Computes the shared secret using `other_public_key` as the other party's
169+
public key and returns the computed shared secret. Supplied key is
170+
interpreted using specified `input_encoding`, and secret is encoded using
171+
specified `output_encoding`. Encodings can be `'binary'`, `'hex'`, or
172+
`'base64'`. If no output encoding is given, the input encoding is used as
173+
output encoding.
174+
175+
### diffieHellman.getPrime(encoding='binary')
176+
177+
Returns the Diffie-Hellman prime in the specified encoding, which can be
178+
`'binary'`, `'hex'`, or `'base64'`.
179+
180+
### diffieHellman.getGenerator(encoding='binary')
181+
182+
Returns the Diffie-Hellman prime in the specified encoding, which can be
183+
`'binary'`, `'hex'`, or `'base64'`.
184+
185+
### diffieHellman.getPublicKey(encoding='binary')
186+
187+
Returns the Diffie-Hellman public key in the specified encoding, which can
188+
be `'binary'`, `'hex'`, or `'base64'`.
189+
190+
### diffieHellman.getPrivateKey(encoding='binary')
191+
192+
Returns the Diffie-Hellman private key in the specified encoding, which can
193+
be `'binary'`, `'hex'`, or `'base64'`.
194+
195+
### diffieHellman.setPublicKey(public_key, encoding='binary')
196+
197+
Sets the Diffie-Hellman public key. Key encoding can be `'binary'`, `'hex'`,
198+
or `'base64'`.
199+
200+
### diffieHellman.setPrivateKey(public_key, encoding='binary')
201+
202+
Sets the Diffie-Hellman private key. Key encoding can be `'binary'`, `'hex'`, or `'base64'`.
203+

lib/crypto.js

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ try {
2929
var Decipher = binding.Decipher;
3030
var Sign = binding.Sign;
3131
var Verify = binding.Verify;
32+
var DiffieHellman = binding.DiffieHellman;
3233
var crypto = true;
3334
} catch (e) {
3435

@@ -139,3 +140,15 @@ exports.Verify = Verify;
139140
exports.createVerify = function(algorithm) {
140141
return (new Verify).init(algorithm);
141142
};
143+
144+
exports.DiffieHellman = DiffieHellman;
145+
exports.createDiffieHellman = function(size_or_key, enc) {
146+
if (!size_or_key) {
147+
return new DiffieHellman();
148+
} else if (!enc) {
149+
return new DiffieHellman(size_or_key);
150+
} else {
151+
return new DiffieHellman(size_or_key, enc);
152+
}
153+
154+
}

0 commit comments

Comments
 (0)