GeezerVison is showing github.com — crypto: create key usage arrays without species · nodejs/node@6d97ccd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/kem_hybrids.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | 4 | | - ArrayPrototypeSlice, | 5 | 4 | BigInt, | 6 | 5 | PromiseWithResolvers, | 7 | 6 | SafeSet, | @@ -24,7 +23,7 @@ const { | 24 | 23 | getCryptoKeySeedData, | 25 | 24 | getCryptoKeySecondaryHandle, | 26 | 25 | getCryptoKeyType, | 27 | | - getCryptoKeyUsages, | | 26 | + getCryptoKeyUsagesMask, | 28 | 27 | InternalCryptoKey, | 29 | 28 | } = require('internal/crypto/keys'); | 30 | 29 |
| @@ -51,6 +50,7 @@ const { | 51 | 50 | const { | 52 | 51 | getBufferSourceBytes, | 53 | 52 | getUsagesMask, | | 53 | + getUsagesFromMask, | 54 | 54 | jobPromise, | 55 | 55 | jobPromiseThen, | 56 | 56 | resolveWebCryptoResult, | @@ -860,7 +860,7 @@ function exportJwkKey(key, rawEncapsulationKey, config) { | 860 | 860 | kty: 'AKP', | 861 | 861 | alg: config.name, | 862 | 862 | pub: base64urlEncode(rawEncapsulationKey), | 863 | | - key_ops: ArrayPrototypeSlice(getCryptoKeyUsages(key), 0), | | 863 | + key_ops: getUsagesFromMask(getCryptoKeyUsagesMask(key)), | 864 | 864 | ext: getCryptoKeyExtractable(key), | 865 | 865 | }; | 866 | 866 | if (getCryptoKeyType(key) === 'private') | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/keys.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | ArrayPrototypeMap, | 5 | | - ArrayPrototypeSlice, | 6 | 5 | ObjectDefineProperties, | 7 | 6 | ObjectPrototypeHasOwnProperty, | 8 | 7 | ObjectSetPrototypeOf, | @@ -1252,7 +1251,7 @@ const { | 1252 | 1251 | type: getCryptoKeyType(this), | 1253 | 1252 | extractable: getCryptoKeyExtractable(this), | 1254 | 1253 | algorithm: cloneAlgorithm(getCryptoKeyAlgorithm(this)), | 1255 | | - usages: ArrayPrototypeSlice(getCryptoKeyUsages(this), 0), | | 1254 | + usages: getUsagesFromMask(getCryptoKeyUsagesMask(this)), | 1256 | 1255 | }, opts)}`; | 1257 | 1256 | } | 1258 | 1257 |
| @@ -1286,7 +1285,7 @@ const { | 1286 | 1285 | const slots = getSlots(this); | 1287 | 1286 | let cached = slots[kSlotClonedUsages]; | 1288 | 1287 | if (cached === undefined) { | 1289 | | - cached = ArrayPrototypeSlice(getCryptoKeyUsagesFromSlots(slots), 0); | | 1288 | + cached = getUsagesFromMask(slots[kSlotUsagesMask]); | 1290 | 1289 | slots[kSlotClonedUsages] = cached; | 1291 | 1290 | } | 1292 | 1291 | return cached; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | const { | 4 | 4 | ArrayBufferIsView, | 5 | 5 | ArrayIsArray, | 6 | | - ArrayPrototypeSlice, | 7 | 6 | FunctionPrototypeCall, | 8 | 7 | JSONParse, | 9 | 8 | JSONStringify, | @@ -51,7 +50,6 @@ const { | 51 | 50 | getCryptoKeyExtractable, | 52 | 51 | getCryptoKeyHandle, | 53 | 52 | getCryptoKeyType, | 54 | | - getCryptoKeyUsages, | 55 | 53 | getCryptoKeyUsagesMask, | 56 | 54 | hasCryptoKeyUsage, | 57 | 55 | importGenericSecretKey, | @@ -65,6 +63,7 @@ const { | 65 | 63 | const { | 66 | 64 | cleanupWebCryptoResult, | 67 | 65 | getBlockSize, | | 66 | + getUsagesFromMask, | 68 | 67 | jobPromiseThen, | 69 | 68 | normalizeAlgorithm, | 70 | 69 | normalizeHashName, | @@ -795,7 +794,7 @@ function exportKeyJWK(key) { | 795 | 794 | // `parameters` before native export populates key material. Delete it for | 796 | 795 | // algorithms without a JWK alg value to keep the expected shape. | 797 | 796 | const parameters = { | 798 | | - key_ops: ArrayPrototypeSlice(getCryptoKeyUsages(key), 0), | | 797 | + key_ops: getUsagesFromMask(getCryptoKeyUsagesMask(key)), | 799 | 798 | ext: getCryptoKeyExtractable(key), | 800 | 799 | alg, | 801 | 800 | }; | |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const { subtle } = globalThis.crypto; | | 9 | + | | 10 | +(async () => { | | 11 | + const key = await subtle.importKey('raw', new Uint8Array(16), 'AES-GCM', true, | | 12 | + ['encrypt', 'decrypt']); | | 13 | + const descriptor = Object.getOwnPropertyDescriptor(Array.prototype, 'constructor'); | | 14 | + let usages; | | 15 | + let jwk; | | 16 | + try { | | 17 | + Object.defineProperty(Array.prototype, 'constructor', { get: common.mustNotCall() }); | | 18 | + usages = key.usages; | | 19 | + jwk = await subtle.exportKey('jwk', key); | | 20 | + } finally { | | 21 | + Object.defineProperty(Array.prototype, 'constructor', descriptor); | | 22 | + } | | 23 | + assert.strictEqual(Object.getPrototypeOf(usages), Array.prototype); | | 24 | + assert.strictEqual(key.usages, usages); | | 25 | + assert.deepStrictEqual(usages, ['encrypt', 'decrypt']); | | 26 | + assert.deepStrictEqual(jwk.key_ops, usages); | | 27 | + usages.push('sign'); | | 28 | + assert.deepStrictEqual((await subtle.exportKey('jwk', key)).key_ops, | | 29 | + ['encrypt', 'decrypt']); | | 30 | +})().then(common.mustCall()); |
|