GeezerVison is showing github.com — crypto: align verifyOneShot accepted types · nodejs/node@16d2fd3
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: doc/api/crypto.md +3-3Lines changed: 3 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -6090,7 +6090,7 @@ changes: | 6090 | 6090 | <!--lint disable maximum-line-length remark-lint--> | 6091 | 6091 |
| 6092 | 6092 | * `algorithm` {string | null | undefined} | 6093 | | -* `data` {ArrayBuffer|Buffer|TypedArray|DataView} | | 6093 | +* `data` {ArrayBuffer|Buffer|SharedArrayBuffer|TypedArray|DataView|string} | 6094 | 6094 | * `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} | 6095 | 6095 | * `callback` {Function} | 6096 | 6096 | * `err` {Error} | @@ -6222,9 +6222,9 @@ changes: | 6222 | 6222 | <!--lint disable maximum-line-length remark-lint--> | 6223 | 6223 |
| 6224 | 6224 | * `algorithm` {string|null|undefined} | 6225 | | -* `data` {ArrayBuffer| Buffer|TypedArray|DataView} | | 6225 | +* `data` {ArrayBuffer|Buffer|SharedArrayBuffer|TypedArray|DataView|string} | 6226 | 6226 | * `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} | 6227 | | -* `signature` {ArrayBuffer|Buffer|TypedArray|DataView} | | 6227 | +* `signature` {ArrayBuffer|Buffer|SharedArrayBuffer|TypedArray|DataView} | 6228 | 6228 | * `callback` {Function} | 6229 | 6229 | * `err` {Error} | 6230 | 6230 | * `result` {boolean} | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/sig.js +1-15Lines changed: 1 addition & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -257,14 +257,6 @@ function verifyOneShot(algorithm, data, key, signature, callback) { | 257 | 257 |
| 258 | 258 | data = getArrayBufferOrView(data, 'data'); | 259 | 259 |
| 260 | | - if (!isArrayBufferView(data)) { | 261 | | - throw new ERR_INVALID_ARG_TYPE( | 262 | | - 'data', | 263 | | - ['Buffer', 'TypedArray', 'DataView'], | 264 | | - data, | 265 | | - ); | 266 | | - } | 267 | | - | 268 | 260 | // Options specific to RSA | 269 | 261 | const rsaPadding = getPadding(key); | 270 | 262 | const pssSaltLength = getSaltLength(key); | @@ -275,13 +267,7 @@ function verifyOneShot(algorithm, data, key, signature, callback) { | 275 | 267 | // Options specific to Ed448 and ML-DSA | 276 | 268 | const context = getContext(key); | 277 | 269 |
| 278 | | - if (!isArrayBufferView(signature)) { | 279 | | - throw new ERR_INVALID_ARG_TYPE( | 280 | | - 'signature', | 281 | | - ['Buffer', 'TypedArray', 'DataView'], | 282 | | - signature, | 283 | | - ); | 284 | | - } | | 270 | + signature = getArrayBufferOrView(signature, 'signature'); | 285 | 271 |
| 286 | 272 | const { | 287 | 273 | data: keyData, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-sign-verify.js +38-2Lines changed: 38 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -607,8 +607,8 @@ if (hasOpenSSL(3, 2)) { | 607 | 607 | assert.throws(() => crypto.sign(null, data, input), errObj); | 608 | 608 | assert.throws(() => crypto.verify(null, data, input, sig), errObj); | 609 | 609 |
| 610 | | - errObj.message = 'The "signature" argument must be an instance of ' + | 611 | | - 'Buffer, TypedArray, or DataView.' + | | 610 | + errObj.message = 'The "signature" argument must be of type string or an instance of ' + | | 611 | + 'ArrayBuffer, Buffer, TypedArray, or DataView.' + | 612 | 612 | common.invalidArgTypeHelper(input); | 613 | 613 | assert.throws(() => crypto.verify(null, data, 'test', input), errObj); | 614 | 614 | }); | @@ -932,3 +932,39 @@ if (hasOpenSSL(3, 2)) { | 932 | 932 | }, { code: 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE', message: /operation not supported for this keytype/ }); | 933 | 933 | } | 934 | 934 | } | | 935 | + | | 936 | +// crypto.verify accepts ArrayBuffer and SharedArrayBuffer for data and signature | | 937 | +{ | | 938 | + const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 }); | | 939 | + const dataBuffer = Buffer.from('Hello world'); | | 940 | + | | 941 | + // Data as ArrayBuffer | | 942 | + { | | 943 | + const ab = dataBuffer.buffer.slice(dataBuffer.byteOffset, dataBuffer.byteOffset + dataBuffer.byteLength); | | 944 | + const sig = crypto.sign('SHA256', dataBuffer, privateKey); | | 945 | + assert.strictEqual(crypto.verify('SHA256', ab, publicKey, sig), true); | | 946 | + } | | 947 | + | | 948 | + // Data as SharedArrayBuffer | | 949 | + { | | 950 | + const sab = new SharedArrayBuffer(dataBuffer.length); | | 951 | + new Uint8Array(sab).set(dataBuffer); | | 952 | + const sig = crypto.sign('SHA256', dataBuffer, privateKey); | | 953 | + assert.strictEqual(crypto.verify('SHA256', sab, publicKey, sig), true); | | 954 | + } | | 955 | + | | 956 | + // Signature as ArrayBuffer | | 957 | + { | | 958 | + const sig = crypto.sign('SHA256', dataBuffer, privateKey); | | 959 | + const sigAB = sig.buffer.slice(sig.byteOffset, sig.byteOffset + sig.byteLength); | | 960 | + assert.strictEqual(crypto.verify('SHA256', dataBuffer, publicKey, sigAB), true); | | 961 | + } | | 962 | + | | 963 | + // Signature as SharedArrayBuffer | | 964 | + { | | 965 | + const sig = crypto.sign('SHA256', dataBuffer, privateKey); | | 966 | + const sigSAB = new SharedArrayBuffer(sig.length); | | 967 | + new Uint8Array(sigSAB).set(sig); | | 968 | + assert.strictEqual(crypto.verify('SHA256', dataBuffer, publicKey, sigSAB), true); | | 969 | + } | | 970 | +} |
|