Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: add scrypt() and scryptSync() methods #20816

Merged
merged 8 commits into from
Jun 13, 2018
Prev Previous commit
lib: rename checkIsArrayBufferView()
Rename it to validateArrayBufferView() to align with validateInt32()
and friends.

Swap the name and the value in the argument list for consistency,
although any reasonable person will agree it's a crime against
humanity to put the value before the name.

PR-URL: #20816
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
bnoordhuis committed Jun 13, 2018
commit 59ace5752a13136eee7ae07ca173bc2addda2e9f
6 changes: 3 additions & 3 deletions lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const {
ERR_INVALID_CALLBACK,
} = require('internal/errors').codes;
const {
checkIsArrayBufferView,
getDefaultEncoding,
validateArrayBufferView,
} = require('internal/crypto/util');

function pbkdf2(password, salt, iterations, keylen, digest, callback) {
Expand Down Expand Up @@ -57,8 +57,8 @@ function check(password, salt, iterations, keylen, digest, callback) {
digest = 'sha1';
}

password = checkIsArrayBufferView('password', password);
salt = checkIsArrayBufferView('salt', salt);
password = validateArrayBufferView(password, 'password');
salt = validateArrayBufferView(salt, 'salt');
iterations = validateInt32(iterations, 'iterations', 0, INT_MAX);
keylen = validateInt32(keylen, 'keylen', 0, INT_MAX);

Expand Down
6 changes: 3 additions & 3 deletions lib/internal/crypto/scrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const {
ERR_INVALID_CALLBACK,
} = require('internal/errors').codes;
const {
checkIsArrayBufferView,
getDefaultEncoding,
validateArrayBufferView,
} = require('internal/crypto/util');

const defaults = {
Expand Down Expand Up @@ -74,8 +74,8 @@ function check(password, salt, keylen, options, callback) {
if (_scrypt === undefined)
throw new ERR_CRYPTO_SCRYPT_NOT_SUPPORTED();

password = checkIsArrayBufferView('password', password);
salt = checkIsArrayBufferView(salt, 'salt');
password = validateArrayBufferView(password, 'password');
salt = validateArrayBufferView(salt, 'salt');
keylen = validateInt32(keylen, 'keylen', 0, INT_MAX);

let { N, r, p, maxmem } = defaults;
Expand Down
15 changes: 8 additions & 7 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const {
RSA_PKCS1_PADDING
} = process.binding('constants').crypto;
const {
checkIsArrayBufferView,
getDefaultEncoding,
toBuf
toBuf,
validateArrayBufferView,
} = require('internal/crypto/util');
const { Writable } = require('stream');
const { inherits } = require('util');
Expand All @@ -41,7 +41,8 @@ Sign.prototype._write = function _write(chunk, encoding, callback) {

Sign.prototype.update = function update(data, encoding) {
encoding = encoding || getDefaultEncoding();
data = checkIsArrayBufferView('data', toBuf(data, encoding));
data = validateArrayBufferView(toBuf(data, encoding),
'data');
this._handle.update(data);
return this;
};
Expand Down Expand Up @@ -77,7 +78,7 @@ Sign.prototype.sign = function sign(options, encoding) {

var pssSaltLength = getSaltLength(options);

key = checkIsArrayBufferView('key', key);
key = validateArrayBufferView(key, 'key');

var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength);

Expand Down Expand Up @@ -114,10 +115,10 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {

var pssSaltLength = getSaltLength(options);

key = checkIsArrayBufferView('key', key);
key = validateArrayBufferView(key, 'key');

signature = checkIsArrayBufferView('signature',
toBuf(signature, sigEncoding));
signature = validateArrayBufferView(toBuf(signature, sigEncoding),
'signature');

return this._handle.verify(key, signature, rsaPadding, pssSaltLength);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function timingSafeEqual(buf1, buf2) {
return _timingSafeEqual(buf1, buf2);
}

function checkIsArrayBufferView(name, buffer) {
function validateArrayBufferView(buffer, name) {
buffer = toBuf(buffer);
if (!isArrayBufferView(buffer)) {
throw new ERR_INVALID_ARG_TYPE(
Expand All @@ -96,7 +96,7 @@ function checkIsArrayBufferView(name, buffer) {
}

module.exports = {
checkIsArrayBufferView,
validateArrayBufferView,
getCiphers,
getCurves,
getDefaultEncoding,
Expand Down