1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-30 02:59:11 -05:00
denoland-deno/tests/node_compat/test/parallel/test-webcrypto-encrypt-decrypt.js
Yoshiya Hinosawa fb24fd37c9
test: add node compat test cases (#27134)
This PR enables node compat test cases found passing by using the tool
added in #27122

The percentage of passing test case increases from 16.16% to 30.43% by
this change.
2024-12-04 11:37:20 +09:00

131 lines
3.3 KiB
JavaScript

// deno-fmt-ignore-file
// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 20.11.1
// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const { subtle } = globalThis.crypto;
// This is only a partial test. The WebCrypto Web Platform Tests
// will provide much greater coverage.
// Test Encrypt/Decrypt RSA-OAEP
{
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
async function test() {
const ec = new TextEncoder();
const { publicKey, privateKey } = await subtle.generateKey({
name: 'RSA-OAEP',
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: 'SHA-384',
}, true, ['encrypt', 'decrypt']);
const ciphertext = await subtle.encrypt({
name: 'RSA-OAEP',
label: ec.encode('a label')
}, publicKey, buf);
const plaintext = await subtle.decrypt({
name: 'RSA-OAEP',
label: ec.encode('a label')
}, privateKey, ciphertext);
assert.strictEqual(
Buffer.from(plaintext).toString('hex'),
Buffer.from(buf).toString('hex'));
}
test().then(common.mustCall());
}
// Test Encrypt/Decrypt AES-CTR
{
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
const counter = globalThis.crypto.getRandomValues(new Uint8Array(16));
async function test() {
const key = await subtle.generateKey({
name: 'AES-CTR',
length: 256
}, true, ['encrypt', 'decrypt']);
const ciphertext = await subtle.encrypt(
{ name: 'AES-CTR', counter, length: 64 }, key, buf,
);
const plaintext = await subtle.decrypt(
{ name: 'AES-CTR', counter, length: 64 }, key, ciphertext,
);
assert.strictEqual(
Buffer.from(plaintext).toString('hex'),
Buffer.from(buf).toString('hex'));
}
test().then(common.mustCall());
}
// Test Encrypt/Decrypt AES-CBC
{
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
const iv = globalThis.crypto.getRandomValues(new Uint8Array(16));
async function test() {
const key = await subtle.generateKey({
name: 'AES-CBC',
length: 256
}, true, ['encrypt', 'decrypt']);
const ciphertext = await subtle.encrypt(
{ name: 'AES-CBC', iv }, key, buf,
);
const plaintext = await subtle.decrypt(
{ name: 'AES-CBC', iv }, key, ciphertext,
);
assert.strictEqual(
Buffer.from(plaintext).toString('hex'),
Buffer.from(buf).toString('hex'));
}
test().then(common.mustCall());
}
// Test Encrypt/Decrypt AES-GCM
{
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
async function test() {
const key = await subtle.generateKey({
name: 'AES-GCM',
length: 256
}, true, ['encrypt', 'decrypt']);
const ciphertext = await subtle.encrypt(
{ name: 'AES-GCM', iv }, key, buf,
);
const plaintext = await subtle.decrypt(
{ name: 'AES-GCM', iv }, key, ciphertext,
);
assert.strictEqual(
Buffer.from(plaintext).toString('hex'),
Buffer.from(buf).toString('hex'));
}
test().then(common.mustCall());
}