Skip to content

Commit

Permalink
crypto: Add crypto.DEFAULT_ENCODING (defaults to 'buffer')
Browse files Browse the repository at this point in the history
This is a flag to make it easier for users to upgrade through the
breaking crypto change, and easier for us to switch it back if it's a
problem.

Explicitly set default encoding to 'buffer' in other tests, in case it
ever changes back.
  • Loading branch information
isaacs committed Oct 23, 2012
1 parent 4266f5c commit 76b0bdf
Show file tree
Hide file tree
Showing 8 changed files with 929 additions and 148 deletions.
333 changes: 189 additions & 144 deletions doc/api/crypto.markdown

Large diffs are not rendered by default.

44 changes: 40 additions & 4 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// Note: In 0.8 and before, crypto functions all defaulted to using
// binary-encoded strings rather than buffers.

exports.DEFAULT_ENCODING = 'buffer';

try {
var binding = process.binding('crypto');
Expand Down Expand Up @@ -137,15 +141,17 @@ function Hash(algorithm) {
}

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

Hash.prototype.digest = function(outputEncoding) {
outputEncoding = outputEncoding || exports.DEFAULT_ENCODING;
var result = this._binding.digest('buffer');
if (outputEncoding && outputEncoding !== 'buffer')
result = result.toString(outputEncoding);
Expand Down Expand Up @@ -191,6 +197,8 @@ function Cipher(cipher, password) {
}

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);

Expand All @@ -205,6 +213,7 @@ Cipher.prototype.update = function(data, inputEncoding, outputEncoding) {
};

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

if (outputEncoding && outputEncoding !== 'buffer') {
Expand Down Expand Up @@ -296,6 +305,7 @@ Sign.prototype.sign = function(key, encoding) {
if (typeof key === 'string')
key = new Buffer(key, 'binary');

encoding = encoding || exports.DEFAULT_ENCODING;
var ret = this._binding.sign(key, 'buffer');
if (encoding && encoding !== 'buffer')
ret = ret.toString(encoding);
Expand All @@ -319,6 +329,7 @@ Verify.prototype.verify = function(object, signature, sigEncoding) {
if (typeof object === 'string')
object = new Buffer(object, 'binary');

sigEncoding = sigEncoding || exports.DEFAULT_ENCODING;
if (sigEncoding === 'buffer')
sigEncoding = null;
if (sigEncoding || typeof signature === 'string')
Expand All @@ -336,22 +347,26 @@ function DiffieHellman(sizeOrKey, encoding) {
if (!sizeOrKey)
this._binding = new binding.DiffieHellman();
else {
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding === 'buffer')
encoding = null;
if (encoding || typeof sizeOrKey === 'string')
if (typeof sizeOrKey === 'string')
sizeOrKey = new Buffer(sizeOrKey, encoding);
this._binding = new binding.DiffieHellman(sizeOrKey, 'buffer');
}
}

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

DiffieHellman.prototype.computeSecret = function(key, inEnc, outEnc) {
inEnc = inEnc || exports.DEFAULT_ENCODING;
outEnc = outEnc || exports.DEFAULT_ENCODING;
if (inEnc === 'buffer')
inEnc = null;
if (outEnc === 'buffer')
Expand All @@ -366,33 +381,38 @@ DiffieHellman.prototype.computeSecret = function(key, inEnc, outEnc) {

DiffieHellman.prototype.getPrime = function(encoding) {
var prime = this._binding.getPrime('buffer');
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');
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');
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');
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding && encoding !== 'buffer')
key = key.toString(encoding);
return key;
};

DiffieHellman.prototype.setPublicKey = function(key, encoding) {
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding === 'buffer')
encoding = null;
if (encoding || typeof key === 'string')
Expand All @@ -402,6 +422,7 @@ DiffieHellman.prototype.setPublicKey = function(key, encoding) {
};

DiffieHellman.prototype.setPrivateKey = function(key, encoding) {
encoding = encoding || exports.DEFAULT_ENCODING;
if (encoding === 'buffer')
encoding = null;
if (encoding || typeof key === 'string')
Expand Down Expand Up @@ -445,7 +466,22 @@ exports.pbkdf2 = function(password, salt, iterations, keylen, callback) {
password = new Buffer(password, 'binary');
if (typeof salt === 'string')
salt = new Buffer(salt, 'binary');
return binding.PBKDF2(password, salt, iterations, keylen, callback);

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

// at this point, we need to handle encodings.
var encoding = exports.DEFAULT_ENCODING;
if (callback) {
binding.PBKDF2(password, salt, iterations, keylen, function(er, ret) {
if (ret)
ret = ret.toString(encoding);
callback(er, ret);
});
} else {
var ret = binding.PBKDF2(password, salt, iterations, keylen);
return ret.toString(encoding);
}
};

exports.pbkdf2Sync = function(password, salt, iterations, keylen) {
Expand Down
Loading

0 comments on commit 76b0bdf

Please sign in to comment.