summaryrefslogtreecommitdiff
path: root/test/parallel/test-crypto-subtle-zero-length.js
blob: f5a84727b02a0156c12a0b0fd4ff4b089562c621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'use strict';

const common = require('../common');

if (!common.hasCrypto)
  common.skip('missing crypto');

const assert = require('assert');
const { subtle } = globalThis.crypto;

(async () => {
  const k = await subtle.importKey(
    'raw',
    new Uint8Array(32),
    { name: 'AES-GCM' },
    false,
    [ 'encrypt', 'decrypt' ]);
  assert(k instanceof CryptoKey);

  const e = await subtle.encrypt({
    name: 'AES-GCM',
    iv: new Uint8Array(12),
  }, k, new Uint8Array(0));
  assert(e instanceof ArrayBuffer);
  assert.deepStrictEqual(
    Buffer.from(e),
    Buffer.from([
      0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
      0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b ]));

  const v = await subtle.decrypt({
    name: 'AES-GCM',
    iv: new Uint8Array(12),
  }, k, e);
  assert(v instanceof ArrayBuffer);
  assert.strictEqual(v.byteLength, 0);
})().then(common.mustCall()).catch((e) => {
  assert.ifError(e);
});