()
| 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 | } |
no test coverage detected
searching dependent graphs…