This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Remove extraneous 'buffer' arguments. 2. Abstract out the 'toBuf' method. 3. Nicely organize DiffieHellman/DiffieHellmanGroup methods. /cc @indutny
- Loading branch information
Showing
1 changed file
with
98 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
isaacs
Author
|
||
str = new Buffer(str, encoding); | ||
return str; | ||
} | ||
|
||
var assert = require('assert'); | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
|
||
|
@@ -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 { | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
}; | ||
|
||
|
@@ -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; | ||
|
@@ -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.
Sorry, something went wrong. |
||
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); | ||
|
@@ -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); | ||
|
Why the
encoding !== 'buffer'
check? If str is a string and encoding is 'buffer', shouldn't it dostr = new Buffer(str, 'binary')
?