1'use strict';
2
3const common = require('../common');
4
5if (!common.hasCrypto)
6  common.skip('missing crypto');
7
8const assert = require('assert');
9const crypto = require('crypto').webcrypto;
10
11(async () => {
12  const k = await crypto.subtle.importKey(
13    'raw',
14    new Uint8Array(32),
15    { name: 'AES-GCM' },
16    false,
17    [ 'encrypt', 'decrypt' ]);
18  assert(k instanceof crypto.CryptoKey);
19
20  const e = await crypto.subtle.encrypt({
21    name: 'AES-GCM',
22    iv: new Uint8Array(12),
23  }, k, new Uint8Array(0));
24  assert(e instanceof ArrayBuffer);
25  assert.deepStrictEqual(
26    Buffer.from(e),
27    Buffer.from([
28      0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
29      0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b ]));
30
31  const v = await crypto.subtle.decrypt({
32    name: 'AES-GCM',
33    iv: new Uint8Array(12),
34  }, k, e);
35  assert(v instanceof ArrayBuffer);
36  assert.strictEqual(v.byteLength, 0);
37})().then(common.mustCall()).catch((e) => {
38  assert.ifError(e);
39});
40