MCPcopy Index your code
hub / github.com/nodejs/node / test

Function test

test/parallel/test-webcrypto-encrypt-decrypt.js:19–55  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17 const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
18
19 async function test() {
20 const ec = new TextEncoder();
21 const { publicKey, privateKey } = await subtle.generateKey({
22 name: 'RSA-OAEP',
23 modulusLength: 2048,
24 publicExponent: new Uint8Array([1, 0, 1]),
25 hash: 'SHA-384',
26 }, true, ['encrypt', 'decrypt']);
27
28 const ciphertext = await subtle.encrypt({
29 name: 'RSA-OAEP',
30 label: ec.encode('a label')
31 }, publicKey, buf);
32
33 const plaintext = await subtle.decrypt({
34 name: 'RSA-OAEP',
35 label: ec.encode('a label')
36 }, privateKey, ciphertext);
37
38 assert.strictEqual(
39 Buffer.from(plaintext).toString('hex'),
40 Buffer.from(buf).toString('hex'));
41
42 await assert.rejects(() => subtle.encrypt({
43 name: 'RSA-OAEP',
44 }, privateKey, buf), {
45 name: 'InvalidAccessError',
46 message: 'Unable to use this key to encrypt'
47 });
48
49 await assert.rejects(() => subtle.decrypt({
50 name: 'RSA-OAEP',
51 }, publicKey, ciphertext), {
52 name: 'InvalidAccessError',
53 message: 'Unable to use this key to decrypt'
54 });
55 }
56
57 test().then(common.mustCall());
58}

Calls 4

encodeMethod · 0.95
encryptMethod · 0.80
toStringMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…