mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
Choose the BufferSource or JsonWebKey branch from the value, then check the requested format after algorithm normalization. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: https://github.com/nodejs/node/pull/66237 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
36 lines
1.6 KiB
JavaScript
36 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
|
|
const assert = require('assert');
|
|
const { subtle } = globalThis.crypto;
|
|
const jwk = { kty: 'oct', k: 'AAAAAAAAAAAAAAAAAAAAAA' };
|
|
|
|
(async () => {
|
|
for (const data of [new ArrayBuffer(16), new Uint8Array(16),
|
|
new DataView(new ArrayBuffer(16)), Buffer.alloc(16)]) {
|
|
await assert.rejects(subtle.importKey('jwk', data, 'AES-GCM', true, ['encrypt']),
|
|
TypeError);
|
|
Object.assign(data, jwk);
|
|
await assert.rejects(subtle.importKey('jwk', data, 'AES-GCM', true, ['encrypt']),
|
|
TypeError);
|
|
await assert.rejects(subtle.importKey('jwk', data, 'unknown', true, ['encrypt']),
|
|
{ name: 'NotSupportedError' });
|
|
}
|
|
await assert.rejects(subtle.importKey('raw', {}, 'unknown', true, ['encrypt']),
|
|
{ name: 'NotSupportedError' });
|
|
await assert.rejects(subtle.importKey('raw', {}, 'AES-GCM', true, ['encrypt']),
|
|
TypeError);
|
|
for (const data of [null, new SharedArrayBuffer(16)]) {
|
|
await assert.rejects(subtle.importKey('jwk', data, 'AES-GCM', true, ['encrypt']),
|
|
{ name: 'DataError' });
|
|
}
|
|
assert.strictEqual((await subtle.importKey('jwk', jwk, 'AES-GCM', true,
|
|
['encrypt'])).type, 'secret');
|
|
await assert.rejects(subtle.importKey('raw', {
|
|
get kty() { throw new Error('converted JWK'); },
|
|
}, 'unknown', true, ['encrypt']), { message: 'converted JWK' });
|
|
})().then(common.mustCall());
|