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

Commit

Permalink
crypto: Binding only accepts buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Oct 13, 2012
1 parent 189e33f commit 8165a38
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 48 deletions.
4 changes: 4 additions & 0 deletions doc/api/crypto.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ Asynchronous PBKDF2 applies pseudorandom function HMAC-SHA1 to derive
a key of given length from the given password, salt and iterations.
The callback gets two arguments `(err, derivedKey)`.

## crypto.pbkdf2Sync(password, salt, iterations, keylen)

Synchronous PBKDF2 function. Returns derivedKey or throws error.

## crypto.randomBytes(size, [callback])

Generates cryptographically strong pseudo-random data. Usage:
Expand Down
89 changes: 69 additions & 20 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
try {
var binding = process.binding('crypto');
var SecureContext = binding.SecureContext;
var PBKDF2 = binding.PBKDF2;
var randomBytes = binding.randomBytes;
var pseudoRandomBytes = binding.pseudoRandomBytes;
var crypto = true;
Expand Down Expand Up @@ -110,10 +109,17 @@ exports.createCredentials = function(options, context) {
}

if (options.pfx) {
if (options.passphrase) {
c.context.loadPKCS12(options.pfx, options.passphrase);
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');
if (passphrase) {
c.context.loadPKCS12(pfx, passphrase);
} else {
c.context.loadPKCS12(options.pfx);
c.context.loadPKCS12(pfx);
}
}

Expand Down Expand Up @@ -151,8 +157,11 @@ 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);
};
}

Hmac.prototype.update = Hash.prototype.update;
Hmac.prototype.digest = Hash.prototype.digest;
Expand All @@ -170,9 +179,14 @@ function Cipher(cipher, password) {
if (!(this instanceof Cipher))
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._decoder = null;
};
}

Cipher.prototype.update = function(data, inputEncoding, outputEncoding) {
if (inputEncoding && inputEncoding !== 'buffer')
Expand Down Expand Up @@ -210,6 +224,11 @@ 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._decoder = null;
Expand All @@ -224,10 +243,15 @@ exports.createDecipher = exports.Decipher = Decipher;
function Decipher(cipher, password) {
if (!(this instanceof Decipher))
return new Decipher(cipher, password);
this._binding = new binding.Decipher

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

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

Decipher.prototype.update = Cipher.prototype.update;
Decipher.prototype.final = Cipher.prototype.final;
Expand All @@ -239,10 +263,15 @@ 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._decoder = null;
};
}

Decipheriv.prototype.update = Cipher.prototype.update;
Decipheriv.prototype.final = Cipher.prototype.final;
Expand All @@ -256,11 +285,15 @@ function Sign(algorithm) {
return new Sign(algorithm);
this._binding = new binding.Sign();
this._binding.init(algorithm);
};
}

Sign.prototype.update = Hash.prototype.update;

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

var ret = this._binding.sign(key, 'buffer');
if (encoding && encoding !== 'buffer')
ret = ret.toString(encoding);
Expand All @@ -280,10 +313,15 @@ 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');

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

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

Expand Down Expand Up @@ -373,34 +411,45 @@ DiffieHellman.prototype.setPrivateKey = function(key, encoding) {


exports.DiffieHellmanGroup =
exports.createDiffieHellmanGroup =
exports.getDiffieHellman = 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;
DiffieHellman.prototype.generateKeys;

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

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

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

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

DiffieHellmanGroup.prototype.getPrivateKey =
DiffieHellman.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');
return binding.PBKDF2(password, salt, iterations, keylen, callback);
};

exports.pbkdf2Sync = function(password, salt, iterations, keylen) {
return exports.pbkdf2(password, salt, iterations, keylen);
};

exports.pbkdf2 = PBKDF2;

exports.randomBytes = randomBytes;
exports.pseudoRandomBytes = pseudoRandomBytes;
Expand Down
Loading

0 comments on commit 8165a38

Please sign in to comment.