• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// This test ensures that CryptoKey instances can be correctly
4// sent to a Worker via postMessage.
5
6const common = require('../common');
7if (!common.hasCrypto)
8  common.skip('missing crypto');
9
10const assert = require('assert');
11const { subtle } = require('crypto').webcrypto;
12const { once } = require('events');
13
14const {
15  Worker,
16  parentPort,
17} = require('worker_threads');
18
19const keyData =
20  Buffer.from(
21    '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', 'hex');
22
23const sig = '13691a79fb55a0417e4d6699a32f91ad29283fa2c1439865cc0632931f4f48dc';
24
25async function doSig(key) {
26  const signature = await subtle.sign({
27    name: 'HMAC'
28  }, key, Buffer.from('some data'));
29  assert.strictEqual(Buffer.from(signature).toString('hex'), sig);
30}
31
32if (process.env.HAS_STARTED_WORKER) {
33  return parentPort.once('message', (key) => {
34    assert.strictEqual(key.algorithm.name, 'HMAC');
35    doSig(key).then(common.mustCall());
36  });
37}
38
39// Don't use isMainThread to allow running this test inside a worker.
40process.env.HAS_STARTED_WORKER = 1;
41
42(async function() {
43  const worker = new Worker(__filename);
44
45  await once(worker, 'online');
46
47  const key = await subtle.importKey(
48    'raw',
49    keyData,
50    { name: 'HMAC', hash: 'SHA-256' },
51    true, ['sign', 'verify']);
52
53  worker.postMessage(key);
54
55  await doSig(key);
56})().then(common.mustCall());
57