| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4eb9365 commit 7124b46
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1587,8 +1587,8 @@ changes: | |||
| 1587 | 1587 | description: The default encoding for `password` if it is a string changed | |
| 1588 | 1588 | from `binary` to `utf8`. | |
| 1589 | 1589 | --> | |
| 1590 | - - `password` {string} | ||
| 1591 | - - `salt` {string} | ||
| 1590 | + - `password` {string|Buffer|TypedArray} | ||
| 1591 | + - `salt` {string|Buffer|TypedArray} | ||
| 1592 | 1592 | - `iterations` {number} | |
| 1593 | 1593 | - `keylen` {number} | |
| 1594 | 1594 | - `digest` {string} | |
@@ -1602,8 +1602,10 @@ applied to derive a key of the requested byte length (`keylen`) from the | |||
| 1602 | 1602 | `password`, `salt` and `iterations`. | |
| 1603 | 1603 | ||
| 1604 | 1604 | The supplied `callback` function is called with two arguments: `err` and | |
| 1605 | - `derivedKey`. If an error occurs, `err` will be set; otherwise `err` will be | ||
| 1606 | - null. The successfully generated `derivedKey` will be passed as a [`Buffer`][]. | ||
| 1605 | + `derivedKey`. If an error occurs while deriving the key, `err` will be set; | ||
| 1606 | + otherwise `err` will be null. By default, the successfully generated | ||
| 1607 | + `derivedKey` will be passed to the callback as a [`Buffer`][]. An error will be | ||
| 1608 | + thrown if any of the input arguments specify invalid values or types. | ||
| 1607 | 1609 | ||
| 1608 | 1610 | The `iterations` argument must be a number set as high as possible. The | |
| 1609 | 1611 | higher the number of iterations, the more secure the derived key will be, | |
@@ -1623,6 +1625,18 @@ crypto.pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => { | |||
| 1623 | 1625 | }); | |
| 1624 | 1626 | ``` | |
| 1625 | 1627 | ||
| 1628 | + The `crypto.DEFAULT_ENCODING` may be used to change the way the `derivedKey` | ||
| 1629 | + is passed to the callback: | ||
| 1630 | + | ||
| 1631 | + ```js | ||
| 1632 | + const crypto = require('crypto'); | ||
| 1633 | + crypto.DEFAULT_ENCODING = 'hex'; | ||
| 1634 | + crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, derivedKey) => { | ||
| 1635 | + if (err) throw err; | ||
| 1636 | + console.log(derivedKey); // '3745e48...aa39b34' | ||
| 1637 | + }); | ||
| 1638 | + ``` | ||
| 1639 | + | ||
| 1626 | 1640 | An array of supported digest functions can be retrieved using | |
| 1627 | 1641 | [`crypto.getHashes()`][]. | |
| 1628 | 1642 | ||
@@ -1643,8 +1657,8 @@ changes: | |||
| 1643 | 1657 | description: The default encoding for `password` if it is a string changed | |
| 1644 | 1658 | from `binary` to `utf8`. | |
| 1645 | 1659 | --> | |
| 1646 | - - `password` {string} | ||
| 1647 | - - `salt` {string} | ||
| 1660 | + - `password` {string|Buffer|TypedArray} | ||
| 1661 | + - `salt` {string|Buffer|TypedArray} | ||
| 1648 | 1662 | - `iterations` {number} | |
| 1649 | 1663 | - `keylen` {number} | |
| 1650 | 1664 | - `digest` {string} | |
@@ -1673,6 +1687,16 @@ const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512'); | |||
| 1673 | 1687 | console.log(key.toString('hex')); // '3745e48...08d59ae' | |
| 1674 | 1688 | ``` | |
| 1675 | 1689 | ||
| 1690 | + The `crypto.DEFAULT_ENCODING` may be used to change the way the `derivedKey` | ||
| 1691 | + is returned: | ||
| 1692 | + | ||
| 1693 | + ```js | ||
| 1694 | + const crypto = require('crypto'); | ||
| 1695 | + crypto.DEFAULT_ENCODING = 'hex'; | ||
| 1696 | + const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512'); | ||
| 1697 | + console.log(key); // '3745e48...aa39b34' | ||
| 1698 | + ``` | ||
| 1699 | + | ||
| 1676 | 1700 | An array of supported digest functions can be retrieved using | |
| 1677 | 1701 | [`crypto.getHashes()`][]. | |
| 1678 | 1702 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -637,6 +637,11 @@ Used when the native call from `process.cpuUsage` cannot be processed properly. | |||
| 637 | 637 | Used when an invalid value for the `format` argument has been passed to the | |
| 638 | 638 | `crypto.ECDH()` class `getPublicKey()` method. | |
| 639 | 639 | ||
| 640 | + <a id="ERR_CRYPTO_INVALID_DIGEST"></a> | ||
| 641 | + ### ERR_CRYPTO_INVALID_DIGEST | ||
| 642 | + | ||
| 643 | + Used when an invalid [crypto digest algorithm][] is specified. | ||
| 644 | + | ||
| 640 | 645 | <a id="ERR_DNS_SET_SERVERS_FAILED"></a> | |
| 641 | 646 | ### ERR_DNS_SET_SERVERS_FAILED | |
| 642 | 647 | ||
@@ -1355,6 +1360,7 @@ closed. | |||
| 1355 | 1360 | [Node.js Error Codes]: #nodejs-error-codes | |
| 1356 | 1361 | [V8's stack trace API]: https://github.com/v8/v8/wiki/Stack-Trace-API | |
| 1357 | 1362 | [WHATWG URL API]: url.html#url_the_whatwg_url_api | |
| 1363 | + [crypto digest algorithm]: crypto.html#crypto_crypto_gethashes | ||
| 1358 | 1364 | [domains]: domain.html | |
| 1359 | 1365 | [event emitter-based]: events.html#events_class_eventemitter | |
| 1360 | 1366 | [file descriptors]: https://en.wikipedia.org/wiki/File_descriptor | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,9 +5,13 @@ const { | |||
| 5 | 5 | getDefaultEncoding, | |
| 6 | 6 | toBuf | |
| 7 | 7 | } = require('internal/crypto/util'); | |
| 8 | + const { isArrayBufferView } = require('internal/util/types'); | ||
| 8 | 9 | const { | |
| 9 | 10 | PBKDF2 | |
| 10 | 11 | } = process.binding('crypto'); | |
| 12 | + const { | ||
| 13 | + INT_MAX | ||
| 14 | + } = process.binding('constants').crypto; | ||
| 11 | 15 | ||
| 12 | 16 | function pbkdf2(password, salt, iterations, keylen, digest, callback) { | |
| 13 | 17 | if (typeof digest === 'function') { | |
@@ -34,10 +38,39 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) { | |||
| 34 | 38 | password = toBuf(password); | |
| 35 | 39 | salt = toBuf(salt); | |
| 36 | 40 | ||
| 41 | + if (!isArrayBufferView(password)) { | ||
| 42 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'password', | ||
| 43 | + ['string', 'Buffer', 'TypedArray']); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + if (!isArrayBufferView(salt)) { | ||
| 47 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'salt', | ||
| 48 | + ['string', 'Buffer', 'TypedArray']); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + if (typeof iterations !== 'number') | ||
| 52 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'iterations', 'number'); | ||
| 53 | + | ||
| 54 | + if (iterations < 0) | ||
| 55 | + throw new errors.RangeError('ERR_OUT_OF_RANGE', 'iterations'); | ||
| 56 | + | ||
| 57 | + if (typeof keylen !== 'number') | ||
| 58 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'keylen', 'number'); | ||
| 59 | + | ||
| 60 | + if (keylen < 0 || | ||
| 61 | + !Number.isFinite(keylen) || | ||
| 62 | + keylen > INT_MAX) { | ||
| 63 | + throw new errors.RangeError('ERR_OUT_OF_RANGE', 'keylen'); | ||
| 64 | + } | ||
| 65 | + | ||
| 37 | 66 | const encoding = getDefaultEncoding(); | |
| 38 | 67 | ||
| 39 | - if (encoding === 'buffer') | ||
| 40 | - return PBKDF2(password, salt, iterations, keylen, digest, callback); | ||
| 68 | + if (encoding === 'buffer') { | ||
| 69 | + const ret = PBKDF2(password, salt, iterations, keylen, digest, callback); | ||
| 70 | + if (ret === -1) | ||
| 71 | + throw new errors.TypeError('ERR_CRYPTO_INVALID_DIGEST', digest); | ||
| 72 | + return ret; | ||
| 73 | + } | ||
| 41 | 74 | ||
| 42 | 75 | // at this point, we need to handle encodings. | |
| 43 | 76 | if (callback) { | |
@@ -46,9 +79,12 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) { | |||
| 46 | 79 | ret = ret.toString(encoding); | |
| 47 | 80 | callback(er, ret); | |
| 48 | 81 | } | |
| 49 | - PBKDF2(password, salt, iterations, keylen, digest, next); | ||
| 82 | + if (PBKDF2(password, salt, iterations, keylen, digest, next) === -1) | ||
| 83 | + throw new errors.TypeError('ERR_CRYPTO_INVALID_DIGEST', digest); | ||
| 50 | 84 | } else { | |
| 51 | - var ret = PBKDF2(password, salt, iterations, keylen, digest); | ||
| 85 | + const ret = PBKDF2(password, salt, iterations, keylen, digest); | ||
| 86 | + if (ret === -1) | ||
| 87 | + throw new errors.TypeError('ERR_CRYPTO_INVALID_DIGEST', digest); | ||
| 52 | 88 | return ret.toString(encoding); | |
| 53 | 89 | } | |
| 54 | 90 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -157,6 +157,7 @@ E('ERR_CRYPTO_ECDH_INVALID_FORMAT', 'Invalid ECDH format: %s'); | |||
| 157 | 157 | E('ERR_CRYPTO_HASH_DIGEST_NO_UTF16', 'hash.digest() does not support UTF-16'); | |
| 158 | 158 | E('ERR_CRYPTO_HASH_FINALIZED', 'Digest already called'); | |
| 159 | 159 | E('ERR_CRYPTO_HASH_UPDATE_FAILED', 'Hash update failed'); | |
| 160 | + E('ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: %s'); | ||
| 160 | 161 | E('ERR_CRYPTO_SIGN_KEY_REQUIRED', 'No key provided to sign'); | |
| 161 | 162 | E('ERR_DNS_SET_SERVERS_FAILED', (err, servers) => | |
| 162 | 163 | `c-ares failed to set servers: "${err}" [${servers}]`); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1180,6 +1180,7 @@ void DefineCryptoConstants(Local<Object> target) { | |||
| 1180 | 1180 | "defaultCipherList", | |
| 1181 | 1181 | default_cipher_list); | |
| 1182 | 1182 | #endif | |
| 1183 | + NODE_DEFINE_CONSTANT(target, INT_MAX); | ||
| 1183 | 1184 | } | |
| 1184 | 1185 | ||
| 1185 | 1186 | void DefineZlibConstants(Local<Object> target) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5330,7 +5330,6 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) { | |||
| 5330 | 5330 | Environment* env = Environment::GetCurrent(args); | |
| 5331 | 5331 | ||
| 5332 | 5332 | const EVP_MD* digest = nullptr; | |
| 5333 | - const char* type_error = nullptr; | ||
| 5334 | 5333 | char* pass = nullptr; | |
| 5335 | 5334 | char* salt = nullptr; | |
| 5336 | 5335 | int passlen = -1; | |
@@ -5341,63 +5340,30 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) { | |||
| 5341 | 5340 | PBKDF2Request* req = nullptr; | |
| 5342 | 5341 | Local<Object> obj; | |
| 5343 | 5342 | ||
| 5344 | - if (args.Length() != 5 && args.Length() != 6) { | ||
| 5345 | - type_error = "Bad parameter"; | ||
| 5346 | - goto err; | ||
| 5347 | - } | ||
| 5348 | - | ||
| 5349 | - THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Pass phrase"); | ||
| 5350 | 5343 | passlen = Buffer::Length(args[0]); | |
| 5351 | - if (passlen < 0) { | ||
| 5352 | - type_error = "Bad password"; | ||
| 5353 | - goto err; | ||
| 5354 | - } | ||
| 5355 | - | ||
| 5356 | - THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Salt"); | ||
| 5357 | 5344 | ||
| 5358 | 5345 | pass = node::Malloc(passlen); | |
| 5359 | 5346 | memcpy(pass, Buffer::Data(args[0]), passlen); | |
| 5360 | 5347 | ||
| 5361 | 5348 | saltlen = Buffer::Length(args[1]); | |
| 5362 | - if (saltlen < 0) { | ||
| 5363 | - type_error = "Bad salt"; | ||
| 5364 | - goto err; | ||
| 5365 | - } | ||
| 5366 | 5349 | ||
| 5367 | 5350 | salt = node::Malloc(saltlen); | |
| 5368 | 5351 | memcpy(salt, Buffer::Data(args[1]), saltlen); | |
| 5369 | 5352 | ||
| 5370 | - if (!args[2]->IsNumber()) { | ||
| 5371 | - type_error = "Iterations not a number"; | ||
| 5372 | - goto err; | ||
| 5373 | - } | ||
| 5374 | - | ||
| 5375 | 5353 | iter = args[2]->Int32Value(); | |
| 5376 | - if (iter < 0) { | ||
| 5377 | - type_error = "Bad iterations"; | ||
| 5378 | - goto err; | ||
| 5379 | - } | ||
| 5380 | - | ||
| 5381 | - if (!args[3]->IsNumber()) { | ||
| 5382 | - type_error = "Key length not a number"; | ||
| 5383 | - goto err; | ||
| 5384 | - } | ||
| 5385 | 5354 | ||
| 5386 | 5355 | raw_keylen = args[3]->NumberValue(); | |
| 5387 | - if (raw_keylen < 0.0 || isnan(raw_keylen) || isinf(raw_keylen) || | ||
| 5388 | - raw_keylen > INT_MAX) { | ||
| 5389 | - type_error = "Bad key length"; | ||
| 5390 | - goto err; | ||
| 5391 | - } | ||
| 5392 | 5356 | ||
| 5393 | 5357 | keylen = static_cast<int>(raw_keylen); | |
| 5394 | 5358 | ||
| 5395 | 5359 | if (args[4]->IsString()) { | |
| 5396 | 5360 | node::Utf8Value digest_name(env->isolate(), args[4]); | |
| 5397 | 5361 | digest = EVP_get_digestbyname(*digest_name); | |
| 5398 | 5362 | if (digest == nullptr) { | |
| 5399 | - type_error = "Bad digest name"; | ||
| 5400 | - goto err; | ||
| 5363 | + free(salt); | ||
| 5364 | + free(pass); | ||
| 5365 | + args.GetReturnValue().Set(-1); | ||
| 5366 | + return; | ||
| 5401 | 5367 | } | |
| 5402 | 5368 | } | |
| 5403 | 5369 | ||
@@ -5443,12 +5409,6 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) { | |||
| 5443 | 5409 | else | |
| 5444 | 5410 | args.GetReturnValue().Set(argv[1]); | |
| 5445 | 5411 | } | |
| 5446 | - return; | ||
| 5447 | - | ||
| 5448 | - err: | ||
| 5449 | - free(salt); | ||
| 5450 | - free(pass); | ||
| 5451 | - return env->ThrowTypeError(type_error); | ||
| 5452 | 5412 | } | |
| 5453 | 5413 | ||
| 5454 | 5414 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments