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

Commit 32c2f18

Browse files
committed
crypto: Cleanup
1. Remove extraneous 'buffer' arguments. 2. Abstract out the 'toBuf' method. 3. Nicely organize DiffieHellman/DiffieHellmanGroup methods. /cc @indutny
1 parent 135e849 commit 32c2f18

1 file changed

Lines changed: 98 additions & 121 deletions

File tree

lib/crypto.js

Lines changed: 98 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ try {
3737
var crypto = false;
3838
}
3939

40+
// This is here because many functions accepted binary strings without
41+
// any explicit encoding in older versions of node, and we don't want
42+
// to break them unnecessarily.
43+
function toBuf(str, encoding) {
44+
encoding = encoding || 'binary';
45+
if (typeof str === 'string' && encoding !== 'buffer')
46+
str = new Buffer(str, encoding);
47+
return str;
48+
}
49+
4050
var assert = require('assert');
4151
var StringDecoder = require('string_decoder').StringDecoder;
4252

@@ -117,11 +127,11 @@ exports.createCredentials = function(options, context) {
117127
if (options.pfx) {
118128
var pfx = options.pfx;
119129
var passphrase = options.passphrase;
120-
// legacy
121-
if (typeof pfx === 'string')
122-
pfx = new Buffer(pfx, 'binary');
123-
if (passphrase && typeof passphrase === 'string')
124-
passphrase = new Buffer(passphrase, 'binary');
130+
131+
pfx = toBuf(pfx);
132+
if (passphrase)
133+
passphrase = toBuf(passphrase);
134+
125135
if (passphrase) {
126136
c.context.loadPKCS12(pfx, passphrase);
127137
} else {
@@ -142,17 +152,16 @@ function Hash(algorithm) {
142152

143153
Hash.prototype.update = function(data, encoding) {
144154
encoding = encoding || exports.DEFAULT_ENCODING;
145-
if (encoding === 'buffer')
146-
encoding = null;
147-
if (typeof data === 'string')
148-
data = new Buffer(data, encoding);
155+
if (encoding === 'buffer' && typeof encoding === 'string')
156+
encoding = 'binary';
157+
data = toBuf(data, encoding);
149158
this._binding.update(data);
150159
return this;
151160
};
152161

153162
Hash.prototype.digest = function(outputEncoding) {
154163
outputEncoding = outputEncoding || exports.DEFAULT_ENCODING;
155-
var result = this._binding.digest('buffer');
164+
var result = this._binding.digest();
156165
if (outputEncoding && outputEncoding !== 'buffer')
157166
result = result.toString(outputEncoding);
158167
return result;
@@ -165,10 +174,7 @@ function Hmac(hmac, key) {
165174
if (!(this instanceof Hmac))
166175
return new Hmac(hmac, key);
167176
this._binding = new binding.Hmac();
168-
// legacy
169-
if (typeof key === 'string')
170-
key = new Buffer(key, 'binary');
171-
this._binding.init(hmac, key);
177+
this._binding.init(hmac, toBuf(key));
172178
}
173179

174180
Hmac.prototype.update = Hash.prototype.update;
@@ -188,21 +194,16 @@ function Cipher(cipher, password) {
188194
return new Cipher(cipher, password);
189195
this._binding = new binding.Cipher;
190196

191-
// legacy.
192-
if (typeof password === 'string')
193-
password = new Buffer(password, 'binary');
194-
195-
this._binding.init(cipher, password);
197+
this._binding.init(cipher, toBuf(password));
196198
this._decoder = null;
197199
}
198200

199201
Cipher.prototype.update = function(data, inputEncoding, outputEncoding) {
200202
inputEncoding = inputEncoding || exports.DEFAULT_ENCODING;
201203
outputEncoding = outputEncoding || exports.DEFAULT_ENCODING;
202-
if (inputEncoding && inputEncoding !== 'buffer')
203-
data = new Buffer(data, inputEncoding);
204+
data = toBuf(data, inputEncoding);
204205

205-
var ret = this._binding.update(data, 'buffer', 'buffer');
206+
var ret = this._binding.update(data);
206207

207208
if (outputEncoding && outputEncoding !== 'buffer') {
208209
this._decoder = getDecoder(this._decoder, outputEncoding);
@@ -214,7 +215,7 @@ Cipher.prototype.update = function(data, inputEncoding, outputEncoding) {
214215

215216
Cipher.prototype.final = function(outputEncoding) {
216217
outputEncoding = outputEncoding || exports.DEFAULT_ENCODING;
217-
var ret = this._binding.final('buffer');
218+
var ret = this._binding.final();
218219

219220
if (outputEncoding && outputEncoding !== 'buffer') {
220221
this._decoder = getDecoder(this._decoder, outputEncoding);
@@ -235,13 +236,8 @@ exports.createCipheriv = exports.Cipheriv = Cipheriv;
235236
function Cipheriv(cipher, key, iv) {
236237
if (!(this instanceof Cipheriv))
237238
return new Cipheriv(cipher, key, iv);
238-
// legacy
239-
if (typeof key === 'string')
240-
key = new Buffer(key, 'binary');
241-
if (typeof iv === 'string')
242-
iv = new Buffer(iv, 'binary');
243239
this._binding = new binding.Cipher();
244-
this._binding.initiv(cipher, key, iv);
240+
this._binding.initiv(cipher, toBuf(key), toBuf(iv));
245241
this._decoder = null;
246242
}
247243

@@ -255,12 +251,8 @@ function Decipher(cipher, password) {
255251
if (!(this instanceof Decipher))
256252
return new Decipher(cipher, password);
257253

258-
// legacy.
259-
if (typeof password === 'string')
260-
password = new Buffer(password, 'binary');
261-
262254
this._binding = new binding.Decipher;
263-
this._binding.init(cipher, password);
255+
this._binding.init(cipher, toBuf(password));
264256
this._decoder = null;
265257
}
266258

@@ -274,13 +266,9 @@ exports.createDecipheriv = exports.Decipheriv = Decipheriv;
274266
function Decipheriv(cipher, key, iv) {
275267
if (!(this instanceof Decipheriv))
276268
return new Decipheriv(cipher, key, iv);
277-
// legacy
278-
if (typeof key === 'string')
279-
key = new Buffer(key, 'binary');
280-
if (typeof iv === 'string')
281-
iv = new Buffer(iv, 'binary');
269+
282270
this._binding = new binding.Decipher;
283-
this._binding.initiv(cipher, key, iv);
271+
this._binding.initiv(cipher, toBuf(key), toBuf(iv));
284272
this._decoder = null;
285273
}
286274

@@ -301,14 +289,12 @@ function Sign(algorithm) {
301289
Sign.prototype.update = Hash.prototype.update;
302290

303291
Sign.prototype.sign = function(key, encoding) {
304-
// legacy.
305-
if (typeof key === 'string')
306-
key = new Buffer(key, 'binary');
307-
308292
encoding = encoding || exports.DEFAULT_ENCODING;
309-
var ret = this._binding.sign(key, 'buffer');
293+
var ret = this._binding.sign(toBuf(key));
294+
310295
if (encoding && encoding !== 'buffer')
311296
ret = ret.toString(encoding);
297+
312298
return ret;
313299
};
314300

@@ -325,17 +311,8 @@ function Verify(algorithm) {
325311
Verify.prototype.update = Hash.prototype.update;
326312

327313
Verify.prototype.verify = function(object, signature, sigEncoding) {
328-
// legacy.
329-
if (typeof object === 'string')
330-
object = new Buffer(object, 'binary');
331-
332314
sigEncoding = sigEncoding || exports.DEFAULT_ENCODING;
333-
if (sigEncoding === 'buffer')
334-
sigEncoding = null;
335-
if (sigEncoding || typeof signature === 'string')
336-
signature = new Buffer(signature, sigEncoding);
337-
338-
return this._binding.verify(object, signature, 'buffer');
315+
return this._binding.verify(toBuf(object), toBuf(signature, sigEncoding));
339316
};
340317

341318
exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman;
@@ -348,63 +325,101 @@ function DiffieHellman(sizeOrKey, encoding) {
348325
this._binding = new binding.DiffieHellman();
349326
else {
350327
encoding = encoding || exports.DEFAULT_ENCODING;
351-
if (encoding === 'buffer')
352-
encoding = null;
353-
if (typeof sizeOrKey === 'string')
354-
sizeOrKey = new Buffer(sizeOrKey, encoding);
355-
this._binding = new binding.DiffieHellman(sizeOrKey, 'buffer');
328+
sizeOrKey = toBuf(sizeOrKey, encoding);
329+
this._binding = new binding.DiffieHellman(sizeOrKey);
356330
}
357331
}
358332

359-
DiffieHellman.prototype.generateKeys = function(encoding) {
360-
var keys = this._binding.generateKeys('buffer');
333+
334+
exports.DiffieHellmanGroup =
335+
exports.createDiffieHellmanGroup =
336+
exports.getDiffieHellman = DiffieHellmanGroup;
337+
338+
function DiffieHellmanGroup(name) {
339+
if (!(this instanceof DiffieHellmanGroup))
340+
return new DiffieHellmanGroup(name);
341+
this._binding = new binding.DiffieHellmanGroup(name);
342+
}
343+
344+
345+
346+
DiffieHellmanGroup.prototype.generateKeys =
347+
DiffieHellman.prototype.generateKeys =
348+
dhGenerateKeys;
349+
350+
function dhGenerateKeys(encoding) {
351+
var keys = this._binding.generateKeys();
361352
encoding = encoding || exports.DEFAULT_ENCODING;
362353
if (encoding && encoding !== 'buffer')
363354
keys = keys.toString(encoding);
364355
return keys;
365356
};
366357

367-
DiffieHellman.prototype.computeSecret = function(key, inEnc, outEnc) {
358+
359+
360+
DiffieHellmanGroup.prototype.computeSecret =
361+
DiffieHellman.prototype.computeSecret =
362+
dhComputeSecret;
363+
364+
function dhComputeSecret(key, inEnc, outEnc) {
368365
inEnc = inEnc || exports.DEFAULT_ENCODING;
369366
outEnc = outEnc || exports.DEFAULT_ENCODING;
370-
if (inEnc === 'buffer')
371-
inEnc = null;
372-
if (outEnc === 'buffer')
373-
outEnc = null;
374-
if (inEnc || typeof key === 'string')
375-
key = new Buffer(key, inEnc);
376-
var ret = this._binding.computeSecret(key, 'buffer', 'buffer');
377-
if (outEnc)
367+
var ret = this._binding.computeSecret(toBuf(key, inEnc));
368+
if (outEnc && outEnc !== 'buffer')
378369
ret = ret.toString(outEnc);
379370
return ret;
380371
};
381372

382-
DiffieHellman.prototype.getPrime = function(encoding) {
383-
var prime = this._binding.getPrime('buffer');
373+
374+
375+
DiffieHellmanGroup.prototype.getPrime =
376+
DiffieHellman.prototype.getPrime =
377+
dhGetPrime;
378+
379+
function dhGetPrime(encoding) {
380+
var prime = this._binding.getPrime();
384381
encoding = encoding || exports.DEFAULT_ENCODING;
385382
if (encoding && encoding !== 'buffer')
386383
prime = prime.toString(encoding);
387384
return prime;
388385
};
389386

390-
DiffieHellman.prototype.getGenerator = function(encoding) {
391-
var generator = this._binding.getGenerator('buffer');
387+
388+
389+
DiffieHellmanGroup.prototype.getGenerator =
390+
DiffieHellman.prototype.getGenerator =
391+
dhGetGenerator;
392+
393+
function dhGetGenerator(encoding) {
394+
var generator = this._binding.getGenerator();
392395
encoding = encoding || exports.DEFAULT_ENCODING;
393396
if (encoding && encoding !== 'buffer')
394397
generator = generator.toString(encoding);
395398
return generator;
396399
};
397400

398-
DiffieHellman.prototype.getPublicKey = function(encoding) {
399-
var key = this._binding.getPublicKey('buffer');
401+
402+
403+
DiffieHellmanGroup.prototype.getPublicKey =
404+
DiffieHellman.prototype.getPublicKey =
405+
dhGetPublicKey;
406+
407+
function dhGetPublicKey(encoding) {
408+
var key = this._binding.getPublicKey();
400409
encoding = encoding || exports.DEFAULT_ENCODING;
401410
if (encoding && encoding !== 'buffer')
402411
key = key.toString(encoding);
403412
return key;
404413
};
405414

406-
DiffieHellman.prototype.getPrivateKey = function(encoding) {
407-
var key = this._binding.getPrivateKey('buffer');
415+
416+
417+
DiffieHellmanGroup.prototype.getPrivateKey =
418+
DiffieHellman.prototype.getPrivateKey =
419+
dhGetPrivateKey;
420+
421+
function dhGetPrivateKey (encoding) {
422+
var key = this._binding.getPrivateKey();
408423
encoding = encoding || exports.DEFAULT_ENCODING;
409424
if (encoding && encoding !== 'buffer')
410425
key = key.toString(encoding);
@@ -413,59 +428,21 @@ DiffieHellman.prototype.getPrivateKey = function(encoding) {
413428

414429
DiffieHellman.prototype.setPublicKey = function(key, encoding) {
415430
encoding = encoding || exports.DEFAULT_ENCODING;
416-
if (encoding === 'buffer')
417-
encoding = null;
418-
if (encoding || typeof key === 'string')
419-
key = new Buffer(key, encoding);
420-
this._binding.setPublicKey(key, 'buffer');
431+
this._binding.setPublicKey(toBuf(key, encoding));
421432
return this;
422433
};
423434

424435
DiffieHellman.prototype.setPrivateKey = function(key, encoding) {
425436
encoding = encoding || exports.DEFAULT_ENCODING;
426-
if (encoding === 'buffer')
427-
encoding = null;
428-
if (encoding || typeof key === 'string')
429-
key = new Buffer(key, encoding);
430-
this._binding.setPrivateKey(key, 'buffer');
437+
this._binding.setPrivateKey(toBuf(key, encoding));
431438
return this;
432439
};
433440

434441

435442

436-
exports.DiffieHellmanGroup =
437-
exports.createDiffieHellmanGroup =
438-
exports.getDiffieHellman = DiffieHellmanGroup;
439-
440-
function DiffieHellmanGroup(name) {
441-
if (!(this instanceof DiffieHellmanGroup))
442-
return new DiffieHellmanGroup(name);
443-
this._binding = new binding.DiffieHellmanGroup(name);
444-
}
445-
446-
DiffieHellmanGroup.prototype.generateKeys =
447-
DiffieHellman.prototype.generateKeys;
448-
449-
DiffieHellmanGroup.prototype.computeSecret =
450-
DiffieHellman.prototype.computeSecret;
451-
452-
DiffieHellmanGroup.prototype.getPrime =
453-
DiffieHellman.prototype.getPrime;
454-
455-
DiffieHellmanGroup.prototype.getGenerator =
456-
DiffieHellman.prototype.getGenerator;
457-
458-
DiffieHellmanGroup.prototype.getPublicKey =
459-
DiffieHellman.prototype.getPublicKey;
460-
461-
DiffieHellmanGroup.prototype.getPrivateKey =
462-
DiffieHellman.prototype.getPrivateKey;
463-
464443
exports.pbkdf2 = function(password, salt, iterations, keylen, callback) {
465-
if (typeof password === 'string')
466-
password = new Buffer(password, 'binary');
467-
if (typeof salt === 'string')
468-
salt = new Buffer(salt, 'binary');
444+
password = toBuf(password);
445+
salt = toBuf(salt);
469446

470447
if (exports.DEFAULT_ENCODING === 'buffer')
471448
return binding.PBKDF2(password, salt, iterations, keylen, callback);

0 commit comments

Comments
 (0)