GeezerVison is showing github.com — crypto: DRY type checking · nodejs/node@4957562
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/pbkdf2.js +7-21Lines changed: 7 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,19 +4,15 @@ const { | 4 | 4 | ERR_INVALID_ARG_TYPE, | 5 | 5 | ERR_INVALID_CALLBACK, | 6 | 6 | ERR_CRYPTO_INVALID_DIGEST, | 7 | | - ERR_OUT_OF_RANGE | 8 | 7 | } = require('internal/errors').codes; | 9 | 8 | const { | 10 | 9 | checkIsArrayBufferView, | | 10 | + checkIsUint, | 11 | 11 | getDefaultEncoding, | 12 | | - toBuf | 13 | 12 | } = require('internal/crypto/util'); | 14 | 13 | const { | 15 | 14 | PBKDF2 | 16 | 15 | } = process.binding('crypto'); | 17 | | -const { | 18 | | - INT_MAX | 19 | | -} = process.binding('constants').crypto; | 20 | 16 |
| 21 | 17 | function pbkdf2(password, salt, iterations, keylen, digest, callback) { | 22 | 18 | if (typeof digest === 'function') { | @@ -39,22 +35,12 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) { | 39 | 35 | if (digest !== null && typeof digest !== 'string') | 40 | 36 | throw new ERR_INVALID_ARG_TYPE('digest', ['string', 'null'], digest); | 41 | 37 |
| 42 | | - password = checkIsArrayBufferView('password', toBuf(password)); | 43 | | - salt = checkIsArrayBufferView('salt', toBuf(salt)); | 44 | | - | 45 | | - if (typeof iterations !== 'number') | 46 | | - throw new ERR_INVALID_ARG_TYPE('iterations', 'number', iterations); | 47 | | - | 48 | | - if (iterations < 0) | 49 | | - throw new ERR_OUT_OF_RANGE('iterations', | 50 | | - 'a non-negative number', | 51 | | - iterations); | 52 | | - | 53 | | - if (typeof keylen !== 'number') | 54 | | - throw new ERR_INVALID_ARG_TYPE('keylen', 'number', keylen); | 55 | | - | 56 | | - if (keylen < 0 || !Number.isInteger(keylen) || keylen > INT_MAX) | 57 | | - throw new ERR_OUT_OF_RANGE('keylen', `>= 0 && <= ${INT_MAX}`, keylen); | | 38 | + password = checkIsArrayBufferView('password', password); | | 39 | + salt = checkIsArrayBufferView('salt', salt); | | 40 | + // FIXME(bnoordhuis) The error message is in fact wrong since |iterations| | | 41 | + // cannot be > INT_MAX. Adjust in the next major release. | | 42 | + iterations = checkIsUint('iterations', iterations, 'a non-negative number'); | | 43 | + keylen = checkIsUint('keylen', keylen); | 58 | 44 |
| 59 | 45 | const encoding = getDefaultEncoding(); | 60 | 46 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/sig.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,7 +77,7 @@ Sign.prototype.sign = function sign(options, encoding) { | 77 | 77 |
| 78 | 78 | var pssSaltLength = getSaltLength(options); | 79 | 79 |
| 80 | | - key = checkIsArrayBufferView('key', toBuf(key)); | | 80 | + key = checkIsArrayBufferView('key', key); | 81 | 81 |
| 82 | 82 | var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength); | 83 | 83 |
| @@ -114,7 +114,7 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) { | 114 | 114 |
| 115 | 115 | var pssSaltLength = getSaltLength(options); | 116 | 116 |
| 117 | | - key = checkIsArrayBufferView('key', toBuf(key)); | | 117 | + key = checkIsArrayBufferView('key', key); | 118 | 118 |
| 119 | 119 | signature = checkIsArrayBufferView('signature', | 120 | 120 | toBuf(signature, sigEncoding)); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,8 @@ const { | 15 | 15 | const { | 16 | 16 | ERR_CRYPTO_ENGINE_UNKNOWN, | 17 | 17 | ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, | 18 | | - ERR_INVALID_ARG_TYPE | | 18 | + ERR_INVALID_ARG_TYPE, | | 19 | + ERR_OUT_OF_RANGE, | 19 | 20 | } = require('internal/errors').codes; | 20 | 21 | const { Buffer } = require('buffer'); | 21 | 22 | const { | @@ -25,6 +26,9 @@ const { | 25 | 26 | const { | 26 | 27 | isArrayBufferView | 27 | 28 | } = require('internal/util/types'); | | 29 | +const { | | 30 | + INT_MAX | | 31 | +} = process.binding('constants').crypto; | 28 | 32 |
| 29 | 33 | var defaultEncoding = 'buffer'; | 30 | 34 |
| @@ -84,6 +88,7 @@ function timingSafeEqual(buf1, buf2) { | 84 | 88 | } | 85 | 89 |
| 86 | 90 | function checkIsArrayBufferView(name, buffer) { | | 91 | + buffer = toBuf(buffer); | 87 | 92 | if (!isArrayBufferView(buffer)) { | 88 | 93 | throw new ERR_INVALID_ARG_TYPE( | 89 | 94 | name, | @@ -94,8 +99,19 @@ function checkIsArrayBufferView(name, buffer) { | 94 | 99 | return buffer; | 95 | 100 | } | 96 | 101 |
| | 102 | +function checkIsUint(name, value, errmsg = `>= 0 && <= ${INT_MAX}`) { | | 103 | + if (typeof value !== 'number') | | 104 | + throw new ERR_INVALID_ARG_TYPE(name, 'number', value); | | 105 | + | | 106 | + if (value < 0 || !Number.isInteger(value) || value > INT_MAX) | | 107 | + throw new ERR_OUT_OF_RANGE(name, errmsg, value); | | 108 | + | | 109 | + return value; | | 110 | +} | | 111 | + | 97 | 112 | module.exports = { | 98 | 113 | checkIsArrayBufferView, | | 114 | + checkIsUint, | 99 | 115 | getCiphers, | 100 | 116 | getCurves, | 101 | 117 | getDefaultEncoding, | |
|