11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.hasCrypto)
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst crypto = require('crypto').webcrypto;
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci(async () => {
121cb0ef41Sopenharmony_ci  const k = await crypto.subtle.importKey(
131cb0ef41Sopenharmony_ci    'raw',
141cb0ef41Sopenharmony_ci    new Uint8Array(32),
151cb0ef41Sopenharmony_ci    { name: 'AES-GCM' },
161cb0ef41Sopenharmony_ci    false,
171cb0ef41Sopenharmony_ci    [ 'encrypt', 'decrypt' ]);
181cb0ef41Sopenharmony_ci  assert(k instanceof crypto.CryptoKey);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  const e = await crypto.subtle.encrypt({
211cb0ef41Sopenharmony_ci    name: 'AES-GCM',
221cb0ef41Sopenharmony_ci    iv: new Uint8Array(12),
231cb0ef41Sopenharmony_ci  }, k, new Uint8Array(0));
241cb0ef41Sopenharmony_ci  assert(e instanceof ArrayBuffer);
251cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
261cb0ef41Sopenharmony_ci    Buffer.from(e),
271cb0ef41Sopenharmony_ci    Buffer.from([
281cb0ef41Sopenharmony_ci      0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
291cb0ef41Sopenharmony_ci      0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b ]));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  const v = await crypto.subtle.decrypt({
321cb0ef41Sopenharmony_ci    name: 'AES-GCM',
331cb0ef41Sopenharmony_ci    iv: new Uint8Array(12),
341cb0ef41Sopenharmony_ci  }, k, e);
351cb0ef41Sopenharmony_ci  assert(v instanceof ArrayBuffer);
361cb0ef41Sopenharmony_ci  assert.strictEqual(v.byteLength, 0);
371cb0ef41Sopenharmony_ci})().then(common.mustCall()).catch((e) => {
381cb0ef41Sopenharmony_ci  assert.ifError(e);
391cb0ef41Sopenharmony_ci});
40