Files
node/test/parallel/test-webcrypto-cryptokey-workers.js
Filip Skokan 9d894c17dd crypto: use WebIDL converters in WebCryptoAPI
WebCryptoAPI functions' arguments are now coersed and validated as per
their WebIDL definitions like in other Web Crypto API implementations.
This further improves interoperability with other implementations of
Web Crypto API.

PR-URL: https://github.com/nodejs/node/pull/46067
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Backport-PR-URL: https://github.com/nodejs/node/pull/46252
2023-06-20 17:23:50 -03:00

57 lines
1.3 KiB
JavaScript

'use strict';
// This test ensures that CryptoKey instances can be correctly
// sent to a Worker via postMessage.
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const { subtle } = require('crypto').webcrypto;
const { once } = require('events');
const {
Worker,
parentPort,
} = require('worker_threads');
const keyData =
Buffer.from(
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', 'hex');
const sig = '13691a79fb55a0417e4d6699a32f91ad29283fa2c1439865cc0632931f4f48dc';
async function doSig(key) {
const signature = await subtle.sign({
name: 'HMAC'
}, key, Buffer.from('some data'));
assert.strictEqual(Buffer.from(signature).toString('hex'), sig);
}
if (process.env.HAS_STARTED_WORKER) {
return parentPort.once('message', (key) => {
assert.strictEqual(key.algorithm.name, 'HMAC');
doSig(key).then(common.mustCall());
});
}
// Don't use isMainThread to allow running this test inside a worker.
process.env.HAS_STARTED_WORKER = 1;
(async function() {
const worker = new Worker(__filename);
await once(worker, 'online');
const key = await subtle.importKey(
'raw',
keyData,
{ name: 'HMAC', hash: 'SHA-256' },
true, ['sign', 'verify']);
worker.postMessage(key);
await doSig(key);
})().then(common.mustCall());