| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 92ef2ad commit abea0af
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,95 @@ | |||
| 1 | + import * as common from '../common/index.mjs'; | ||
| 2 | + | ||
| 3 | + if (!common.hasCrypto) common.skip('missing crypto'); | ||
| 4 | + | ||
| 5 | + // WebCrypto subtle methods must not leak intermediate values | ||
| 6 | + // through Promise.prototype.then pollution. | ||
| 7 | + // Regression test for https://github.com/nodejs/node/pull/61492 | ||
| 8 | + // and https://github.com/nodejs/node/issues/59699. | ||
| 9 | + | ||
| 10 | + import { hasOpenSSL } from '../common/crypto.js'; | ||
| 11 | + | ||
| 12 | + const { subtle } = globalThis.crypto; | ||
| 13 | + | ||
| 14 | + Promise.prototype.then = common.mustNotCall('Promise.prototype.then'); | ||
| 15 | + | ||
| 16 | + await subtle.digest('SHA-256', new Uint8Array([1, 2, 3])); | ||
| 17 | + | ||
| 18 | + await subtle.generateKey({ name: 'AES-CBC', length: 256 }, true, ['encrypt', 'decrypt']); | ||
| 19 | + | ||
| 20 | + await subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign', 'verify']); | ||
| 21 | + | ||
| 22 | + const rawKey = globalThis.crypto.getRandomValues(new Uint8Array(32)); | ||
| 23 | + | ||
| 24 | + const importedKey = await subtle.importKey( | ||
| 25 | + 'raw', rawKey, { name: 'AES-CBC', length: 256 }, false, ['encrypt', 'decrypt']); | ||
| 26 | + | ||
| 27 | + const exportableKey = await subtle.importKey( | ||
| 28 | + 'raw', rawKey, { name: 'AES-CBC', length: 256 }, true, ['encrypt', 'decrypt']); | ||
| 29 | + | ||
| 30 | + await subtle.exportKey('raw', exportableKey); | ||
| 31 | + | ||
| 32 | + const iv = globalThis.crypto.getRandomValues(new Uint8Array(16)); | ||
| 33 | + const plaintext = new TextEncoder().encode('Hello, world!'); | ||
| 34 | + | ||
| 35 | + const ciphertext = await subtle.encrypt({ name: 'AES-CBC', iv }, importedKey, plaintext); | ||
| 36 | + | ||
| 37 | + await subtle.decrypt({ name: 'AES-CBC', iv }, importedKey, ciphertext); | ||
| 38 | + | ||
| 39 | + const signingKey = await subtle.generateKey( | ||
| 40 | + { name: 'HMAC', hash: 'SHA-256' }, false, ['sign', 'verify']); | ||
| 41 | + | ||
| 42 | + const data = new TextEncoder().encode('test data'); | ||
| 43 | + | ||
| 44 | + const signature = await subtle.sign('HMAC', signingKey, data); | ||
| 45 | + | ||
| 46 | + await subtle.verify('HMAC', signingKey, signature, data); | ||
| 47 | + | ||
| 48 | + const pbkdf2Key = await subtle.importKey( | ||
| 49 | + 'raw', rawKey, 'PBKDF2', false, ['deriveBits', 'deriveKey']); | ||
| 50 | + | ||
| 51 | + await subtle.deriveBits( | ||
| 52 | + { name: 'PBKDF2', salt: rawKey, iterations: 1000, hash: 'SHA-256' }, | ||
| 53 | + pbkdf2Key, 256); | ||
| 54 | + | ||
| 55 | + await subtle.deriveKey( | ||
| 56 | + { name: 'PBKDF2', salt: rawKey, iterations: 1000, hash: 'SHA-256' }, | ||
| 57 | + pbkdf2Key, | ||
| 58 | + { name: 'AES-CBC', length: 256 }, | ||
| 59 | + true, | ||
| 60 | + ['encrypt', 'decrypt']); | ||
| 61 | + | ||
| 62 | + const wrappingKey = await subtle.generateKey( | ||
| 63 | + { name: 'AES-KW', length: 256 }, true, ['wrapKey', 'unwrapKey']); | ||
| 64 | + | ||
| 65 | + const keyToWrap = await subtle.generateKey( | ||
| 66 | + { name: 'AES-CBC', length: 256 }, true, ['encrypt', 'decrypt']); | ||
| 67 | + | ||
| 68 | + const wrapped = await subtle.wrapKey('raw', keyToWrap, wrappingKey, 'AES-KW'); | ||
| 69 | + | ||
| 70 | + await subtle.unwrapKey( | ||
| 71 | + 'raw', wrapped, wrappingKey, 'AES-KW', | ||
| 72 | + { name: 'AES-CBC', length: 256 }, true, ['encrypt', 'decrypt']); | ||
| 73 | + | ||
| 74 | + const { privateKey } = await subtle.generateKey( | ||
| 75 | + { name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign', 'verify']); | ||
| 76 | + | ||
| 77 | + await subtle.getPublicKey(privateKey, ['verify']); | ||
| 78 | + | ||
| 79 | + if (hasOpenSSL(3, 5)) { | ||
| 80 | + const kemPair = await subtle.generateKey( | ||
| 81 | + { name: 'ML-KEM-768' }, false, | ||
| 82 | + ['encapsulateKey', 'encapsulateBits', 'decapsulateKey', 'decapsulateBits']); | ||
| 83 | + | ||
| 84 | + const { ciphertext: ct1 } = await subtle.encapsulateKey( | ||
| 85 | + { name: 'ML-KEM-768' }, kemPair.publicKey, 'HKDF', false, ['deriveBits']); | ||
| 86 | + | ||
| 87 | + await subtle.decapsulateKey( | ||
| 88 | + { name: 'ML-KEM-768' }, kemPair.privateKey, ct1, 'HKDF', false, ['deriveBits']); | ||
| 89 | + | ||
| 90 | + const { ciphertext: ct2 } = await subtle.encapsulateBits( | ||
| 91 | + { name: 'ML-KEM-768' }, kemPair.publicKey); | ||
| 92 | + | ||
| 93 | + await subtle.decapsulateBits( | ||
| 94 | + { name: 'ML-KEM-768' }, kemPair.privateKey, ct2); | ||
| 95 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments