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

Commit

Permalink
crypto: Cleanup
Browse files Browse the repository at this point in the history
1. Remove extraneous 'buffer' arguments.
2. Abstract out the 'toBuf' method.
3. Nicely organize DiffieHellman/DiffieHellmanGroup methods.

/cc @indutny
  • Loading branch information
isaacs committed Oct 22, 2012
1 parent 135e849 commit 32c2f18
Showing 1 changed file with 98 additions and 121 deletions.
219 changes: 98 additions & 121 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ try {
var crypto = false;
}

// This is here because many functions accepted binary strings without
// any explicit encoding in older versions of node, and we don't want
// to break them unnecessarily.
function toBuf(str, encoding) {
encoding = encoding || 'binary';
if (typeof str === 'string' && encoding !== 'buffer')

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Oct 23, 2012

Member

Why the encoding !== 'buffer' check? If str is a string and encoding is 'buffer', shouldn't it do str = new Buffer(str, 'binary')?

This comment has been minimized.

Copy link
@isaacs

isaacs Oct 23, 2012

Author

If the encoding is 'buffer', then we expect that it already is a buffer, so just pass it through.

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Oct 23, 2012

Member

Oh, right. It's slightly less than obvious. Maybe add an assert?

str = new Buffer(str, encoding);
return str;
}

var assert = require('assert');
var StringDecoder = require('string_decoder').StringDecoder;

Expand Down Expand Up @@ -117,11 +127,11 @@ exports.createCredentials = function(options, context) {
if (options.pfx) {
var pfx = options.pfx;
var passphrase = options.passphrase;
// legacy
if (typeof pfx === 'string')
pfx = new Buffer(pfx, 'binary');
if (passphrase && typeof passphrase === 'string')
passphrase = new Buffer(passphrase, 'binary');

pfx = toBuf(pfx);
if (passphrase)
passphrase = toBuf(passphrase);

if (passphrase) {
c.context.loadPKCS12(pfx, passphrase);
} else {
Expand All @@ -142,17 +152,16 @@ function Hash(algorithm) {

Hash.prototype.update = function(data, encoding) {
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding === 'buffer')
encoding = null;
if (typeof data === 'string')
data = new Buffer(data, encoding);
if (encoding === 'buffer' && typeof encoding === 'string')
encoding = 'binary';
data = toBuf(data, encoding);
this._binding.update(data);
return this;
};

Hash.prototype.digest = function(outputEncoding) {
outputEncoding = outputEncoding || exports.DEFAULT_ENCODING;
var result = this._binding.digest('buffer');
var result = this._binding.digest();
if (outputEncoding && outputEncoding !== 'buffer')
result = result.toString(outputEncoding);
return result;
Expand All @@ -165,10 +174,7 @@ function Hmac(hmac, key) {
if (!(this instanceof Hmac))
return new Hmac(hmac, key);
this._binding = new binding.Hmac();
// legacy
if (typeof key === 'string')
key = new Buffer(key, 'binary');
this._binding.init(hmac, key);
this._binding.init(hmac, toBuf(key));
}

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

// legacy.
if (typeof password === 'string')
password = new Buffer(password, 'binary');

this._binding.init(cipher, password);
this._binding.init(cipher, toBuf(password));
this._decoder = null;
}

Cipher.prototype.update = function(data, inputEncoding, outputEncoding) {
inputEncoding = inputEncoding || exports.DEFAULT_ENCODING;
outputEncoding = outputEncoding || exports.DEFAULT_ENCODING;
if (inputEncoding && inputEncoding !== 'buffer')
data = new Buffer(data, inputEncoding);
data = toBuf(data, inputEncoding);

var ret = this._binding.update(data, 'buffer', 'buffer');
var ret = this._binding.update(data);

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

Cipher.prototype.final = function(outputEncoding) {
outputEncoding = outputEncoding || exports.DEFAULT_ENCODING;
var ret = this._binding.final('buffer');
var ret = this._binding.final();

if (outputEncoding && outputEncoding !== 'buffer') {
this._decoder = getDecoder(this._decoder, outputEncoding);
Expand All @@ -235,13 +236,8 @@ exports.createCipheriv = exports.Cipheriv = Cipheriv;
function Cipheriv(cipher, key, iv) {
if (!(this instanceof Cipheriv))
return new Cipheriv(cipher, key, iv);
// legacy
if (typeof key === 'string')
key = new Buffer(key, 'binary');
if (typeof iv === 'string')
iv = new Buffer(iv, 'binary');
this._binding = new binding.Cipher();
this._binding.initiv(cipher, key, iv);
this._binding.initiv(cipher, toBuf(key), toBuf(iv));
this._decoder = null;
}

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

// legacy.
if (typeof password === 'string')
password = new Buffer(password, 'binary');

this._binding = new binding.Decipher;
this._binding.init(cipher, password);
this._binding.init(cipher, toBuf(password));
this._decoder = null;
}

Expand All @@ -274,13 +266,9 @@ exports.createDecipheriv = exports.Decipheriv = Decipheriv;
function Decipheriv(cipher, key, iv) {
if (!(this instanceof Decipheriv))
return new Decipheriv(cipher, key, iv);
// legacy
if (typeof key === 'string')
key = new Buffer(key, 'binary');
if (typeof iv === 'string')
iv = new Buffer(iv, 'binary');

this._binding = new binding.Decipher;
this._binding.initiv(cipher, key, iv);
this._binding.initiv(cipher, toBuf(key), toBuf(iv));
this._decoder = null;
}

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

Sign.prototype.sign = function(key, encoding) {
// legacy.
if (typeof key === 'string')
key = new Buffer(key, 'binary');

encoding = encoding || exports.DEFAULT_ENCODING;
var ret = this._binding.sign(key, 'buffer');
var ret = this._binding.sign(toBuf(key));

if (encoding && encoding !== 'buffer')
ret = ret.toString(encoding);

return ret;
};

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

Verify.prototype.verify = function(object, signature, sigEncoding) {
// legacy.
if (typeof object === 'string')
object = new Buffer(object, 'binary');

sigEncoding = sigEncoding || exports.DEFAULT_ENCODING;
if (sigEncoding === 'buffer')
sigEncoding = null;
if (sigEncoding || typeof signature === 'string')
signature = new Buffer(signature, sigEncoding);

return this._binding.verify(object, signature, 'buffer');
return this._binding.verify(toBuf(object), toBuf(signature, sigEncoding));
};

exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman;
Expand All @@ -348,63 +325,101 @@ function DiffieHellman(sizeOrKey, encoding) {
this._binding = new binding.DiffieHellman();
else {
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding === 'buffer')
encoding = null;
if (typeof sizeOrKey === 'string')
sizeOrKey = new Buffer(sizeOrKey, encoding);
this._binding = new binding.DiffieHellman(sizeOrKey, 'buffer');
sizeOrKey = toBuf(sizeOrKey, encoding);
this._binding = new binding.DiffieHellman(sizeOrKey);
}
}

DiffieHellman.prototype.generateKeys = function(encoding) {
var keys = this._binding.generateKeys('buffer');

exports.DiffieHellmanGroup =
exports.createDiffieHellmanGroup =
exports.getDiffieHellman = DiffieHellmanGroup;

function DiffieHellmanGroup(name) {
if (!(this instanceof DiffieHellmanGroup))
return new DiffieHellmanGroup(name);
this._binding = new binding.DiffieHellmanGroup(name);
}



DiffieHellmanGroup.prototype.generateKeys =
DiffieHellman.prototype.generateKeys =
dhGenerateKeys;

function dhGenerateKeys(encoding) {
var keys = this._binding.generateKeys();
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding && encoding !== 'buffer')
keys = keys.toString(encoding);
return keys;
};

DiffieHellman.prototype.computeSecret = function(key, inEnc, outEnc) {


DiffieHellmanGroup.prototype.computeSecret =
DiffieHellman.prototype.computeSecret =

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Oct 23, 2012

Member

Trailing whitespace.

dhComputeSecret;

function dhComputeSecret(key, inEnc, outEnc) {
inEnc = inEnc || exports.DEFAULT_ENCODING;
outEnc = outEnc || exports.DEFAULT_ENCODING;
if (inEnc === 'buffer')
inEnc = null;
if (outEnc === 'buffer')
outEnc = null;
if (inEnc || typeof key === 'string')
key = new Buffer(key, inEnc);
var ret = this._binding.computeSecret(key, 'buffer', 'buffer');
if (outEnc)
var ret = this._binding.computeSecret(toBuf(key, inEnc));
if (outEnc && outEnc !== 'buffer')
ret = ret.toString(outEnc);
return ret;
};

DiffieHellman.prototype.getPrime = function(encoding) {
var prime = this._binding.getPrime('buffer');


DiffieHellmanGroup.prototype.getPrime =
DiffieHellman.prototype.getPrime =
dhGetPrime;

function dhGetPrime(encoding) {
var prime = this._binding.getPrime();
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding && encoding !== 'buffer')
prime = prime.toString(encoding);
return prime;
};

DiffieHellman.prototype.getGenerator = function(encoding) {
var generator = this._binding.getGenerator('buffer');


DiffieHellmanGroup.prototype.getGenerator =
DiffieHellman.prototype.getGenerator =
dhGetGenerator;

function dhGetGenerator(encoding) {
var generator = this._binding.getGenerator();
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding && encoding !== 'buffer')
generator = generator.toString(encoding);
return generator;
};

DiffieHellman.prototype.getPublicKey = function(encoding) {
var key = this._binding.getPublicKey('buffer');


DiffieHellmanGroup.prototype.getPublicKey =
DiffieHellman.prototype.getPublicKey =
dhGetPublicKey;

function dhGetPublicKey(encoding) {
var key = this._binding.getPublicKey();
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding && encoding !== 'buffer')
key = key.toString(encoding);
return key;
};

DiffieHellman.prototype.getPrivateKey = function(encoding) {
var key = this._binding.getPrivateKey('buffer');


DiffieHellmanGroup.prototype.getPrivateKey =
DiffieHellman.prototype.getPrivateKey =
dhGetPrivateKey;

function dhGetPrivateKey (encoding) {
var key = this._binding.getPrivateKey();
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding && encoding !== 'buffer')
key = key.toString(encoding);
Expand All @@ -413,59 +428,21 @@ DiffieHellman.prototype.getPrivateKey = function(encoding) {

DiffieHellman.prototype.setPublicKey = function(key, encoding) {
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding === 'buffer')
encoding = null;
if (encoding || typeof key === 'string')
key = new Buffer(key, encoding);
this._binding.setPublicKey(key, 'buffer');
this._binding.setPublicKey(toBuf(key, encoding));
return this;
};

DiffieHellman.prototype.setPrivateKey = function(key, encoding) {
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding === 'buffer')
encoding = null;
if (encoding || typeof key === 'string')
key = new Buffer(key, encoding);
this._binding.setPrivateKey(key, 'buffer');
this._binding.setPrivateKey(toBuf(key, encoding));
return this;
};



exports.DiffieHellmanGroup =
exports.createDiffieHellmanGroup =
exports.getDiffieHellman = DiffieHellmanGroup;

function DiffieHellmanGroup(name) {
if (!(this instanceof DiffieHellmanGroup))
return new DiffieHellmanGroup(name);
this._binding = new binding.DiffieHellmanGroup(name);
}

DiffieHellmanGroup.prototype.generateKeys =
DiffieHellman.prototype.generateKeys;

DiffieHellmanGroup.prototype.computeSecret =
DiffieHellman.prototype.computeSecret;

DiffieHellmanGroup.prototype.getPrime =
DiffieHellman.prototype.getPrime;

DiffieHellmanGroup.prototype.getGenerator =
DiffieHellman.prototype.getGenerator;

DiffieHellmanGroup.prototype.getPublicKey =
DiffieHellman.prototype.getPublicKey;

DiffieHellmanGroup.prototype.getPrivateKey =
DiffieHellman.prototype.getPrivateKey;

exports.pbkdf2 = function(password, salt, iterations, keylen, callback) {
if (typeof password === 'string')
password = new Buffer(password, 'binary');
if (typeof salt === 'string')
salt = new Buffer(salt, 'binary');
password = toBuf(password);
salt = toBuf(salt);

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

0 comments on commit 32c2f18

Please sign in to comment.