| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,6 @@ const { | |||
| 7 | 7 | ArrayPrototypeIncludes, | |
| 8 | 8 | ArrayPrototypePush, | |
| 9 | 9 | MathFloor, | |
| 10 | - Promise, | ||
| 11 | 10 | SafeSet, | |
| 12 | 11 | TypedArrayPrototypeSlice, | |
| 13 | 12 | } = primordials; | |
@@ -46,6 +45,7 @@ const { | |||
| 46 | 45 | ||
| 47 | 46 | const { | |
| 48 | 47 | lazyDOMException, | |
| 48 | + promisify, | ||
| 49 | 49 | } = require('internal/util'); | |
| 50 | 50 | ||
| 51 | 51 | const { PromiseReject } = primordials; | |
@@ -57,7 +57,7 @@ const { | |||
| 57 | 57 | } = require('internal/crypto/keys'); | |
| 58 | 58 | ||
| 59 | 59 | const { | |
| 60 | - generateKey, | ||
| 60 | + generateKey: _generateKey, | ||
| 61 | 61 | } = require('internal/crypto/keygen'); | |
| 62 | 62 | ||
| 63 | 63 | const { | |
@@ -67,6 +67,7 @@ const { | |||
| 67 | 67 | ||
| 68 | 68 | const kMaxCounterLength = 128; | |
| 69 | 69 | const kTagLengths = [32, 64, 96, 104, 112, 120, 128]; | |
| 70 | + const generateKey = promisify(_generateKey); | ||
| 70 | 71 | ||
| 71 | 72 | function getAlgorithmName(name, length) { | |
| 72 | 73 | switch (name) { | |
@@ -241,22 +242,19 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) { | |||
| 241 | 242 | 'SyntaxError'); | |
| 242 | 243 | } | |
| 243 | 244 | ||
| 244 | - return new Promise((resolve, reject) => { | ||
| 245 | - generateKey('aes', { length }, (err, key) => { | ||
| 246 | - if (err) { | ||
| 247 | - return reject(lazyDOMException( | ||
| 248 | - 'The operation failed for an operation-specific reason ' + | ||
| 249 | - `[${err.message}]`, | ||
| 250 | - 'OperationError')); | ||
| 251 | - } | ||
| 252 | - | ||
| 253 | - resolve(new InternalCryptoKey( | ||
| 254 | - key, | ||
| 255 | - { name, length }, | ||
| 256 | - ArrayFrom(usagesSet), | ||
| 257 | - extractable)); | ||
| 258 | - }); | ||
| 245 | + const key = await generateKey('aes', { length }).catch((err) => { | ||
| 246 | + // TODO(@panva): add err as cause to DOMException | ||
| 247 | + throw lazyDOMException( | ||
| 248 | + 'The operation failed for an operation-specific reason' + | ||
| 249 | + `[${err.message}]`, | ||
| 250 | + 'OperationError'); | ||
| 259 | 251 | }); | |
| 252 | + | ||
| 253 | + return new InternalCryptoKey( | ||
| 254 | + key, | ||
| 255 | + { name, length }, | ||
| 256 | + ArrayFrom(usagesSet), | ||
| 257 | + extractable); | ||
| 260 | 258 | } | |
| 261 | 259 | ||
| 262 | 260 | async function aesImportKey( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,6 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | - Promise, | ||
| 5 | 4 | SafeSet, | |
| 6 | 5 | } = primordials; | |
| 7 | 6 | ||
@@ -31,10 +30,11 @@ const { | |||
| 31 | 30 | const { | |
| 32 | 31 | emitExperimentalWarning, | |
| 33 | 32 | lazyDOMException, | |
| 33 | + promisify, | ||
| 34 | 34 | } = require('internal/util'); | |
| 35 | 35 | ||
| 36 | 36 | const { | |
| 37 | - generateKeyPair, | ||
| 37 | + generateKeyPair: _generateKeyPair, | ||
| 38 | 38 | } = require('internal/crypto/keygen'); | |
| 39 | 39 | ||
| 40 | 40 | const { | |
@@ -45,6 +45,8 @@ const { | |||
| 45 | 45 | createPublicKey, | |
| 46 | 46 | } = require('internal/crypto/keys'); | |
| 47 | 47 | ||
| 48 | + const generateKeyPair = promisify(_generateKeyPair); | ||
| 49 | + | ||
| 48 | 50 | function verifyAcceptableCfrgKeyUse(name, type, usages) { | |
| 49 | 51 | let checkSet; | |
| 50 | 52 | switch (name) { | |
@@ -131,65 +133,63 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) { | |||
| 131 | 133 | } | |
| 132 | 134 | break; | |
| 133 | 135 | } | |
| 134 | - return new Promise((resolve, reject) => { | ||
| 135 | - let genKeyType; | ||
| 136 | - switch (name) { | ||
| 137 | - case 'Ed25519': | ||
| 138 | - genKeyType = 'ed25519'; | ||
| 139 | - break; | ||
| 140 | - case 'Ed448': | ||
| 141 | - genKeyType = 'ed448'; | ||
| 142 | - break; | ||
| 143 | - case 'X25519': | ||
| 144 | - genKeyType = 'x25519'; | ||
| 145 | - break; | ||
| 146 | - case 'X448': | ||
| 147 | - genKeyType = 'x448'; | ||
| 148 | - break; | ||
| 149 | - } | ||
| 150 | - generateKeyPair(genKeyType, undefined, (err, pubKey, privKey) => { | ||
| 151 | - if (err) { | ||
| 152 | - return reject(lazyDOMException( | ||
| 153 | - 'The operation failed for an operation-specific reason', | ||
| 154 | - 'OperationError')); | ||
| 155 | - } | ||
| 136 | + let genKeyType; | ||
| 137 | + switch (name) { | ||
| 138 | + case 'Ed25519': | ||
| 139 | + genKeyType = 'ed25519'; | ||
| 140 | + break; | ||
| 141 | + case 'Ed448': | ||
| 142 | + genKeyType = 'ed448'; | ||
| 143 | + break; | ||
| 144 | + case 'X25519': | ||
| 145 | + genKeyType = 'x25519'; | ||
| 146 | + break; | ||
| 147 | + case 'X448': | ||
| 148 | + genKeyType = 'x448'; | ||
| 149 | + break; | ||
| 150 | + } | ||
| 156 | 151 | ||
| 157 | - const algorithm = { name }; | ||
| 152 | + const keyPair = await generateKeyPair(genKeyType).catch((err) => { | ||
| 153 | + // TODO(@panva): add err as cause to DOMException | ||
| 154 | + throw lazyDOMException( | ||
| 155 | + 'The operation failed for an operation-specific reason', | ||
| 156 | + 'OperationError'); | ||
| 157 | + }); | ||
| 158 | 158 | ||
| 159 | - let publicUsages; | ||
| 160 | - let privateUsages; | ||
| 161 | - switch (name) { | ||
| 162 | - case 'Ed25519': | ||
| 163 | - // Fall through | ||
| 164 | - case 'Ed448': | ||
| 165 | - publicUsages = getUsagesUnion(usageSet, 'verify'); | ||
| 166 | - privateUsages = getUsagesUnion(usageSet, 'sign'); | ||
| 167 | - break; | ||
| 168 | - case 'X25519': | ||
| 169 | - // Fall through | ||
| 170 | - case 'X448': | ||
| 171 | - publicUsages = []; | ||
| 172 | - privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits'); | ||
| 173 | - break; | ||
| 174 | - } | ||
| 159 | + let publicUsages; | ||
| 160 | + let privateUsages; | ||
| 161 | + switch (name) { | ||
| 162 | + case 'Ed25519': | ||
| 163 | + // Fall through | ||
| 164 | + case 'Ed448': | ||
| 165 | + publicUsages = getUsagesUnion(usageSet, 'verify'); | ||
| 166 | + privateUsages = getUsagesUnion(usageSet, 'sign'); | ||
| 167 | + break; | ||
| 168 | + case 'X25519': | ||
| 169 | + // Fall through | ||
| 170 | + case 'X448': | ||
| 171 | + publicUsages = []; | ||
| 172 | + privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits'); | ||
| 173 | + break; | ||
| 174 | + } | ||
| 175 | 175 | ||
| 176 | - const publicKey = | ||
| 177 | - new InternalCryptoKey( | ||
| 178 | - pubKey, | ||
| 179 | - algorithm, | ||
| 180 | - publicUsages, | ||
| 181 | - true); | ||
| 182 | - | ||
| 183 | - const privateKey = | ||
| 184 | - new InternalCryptoKey( | ||
| 185 | - privKey, | ||
| 186 | - algorithm, | ||
| 187 | - privateUsages, | ||
| 188 | - extractable); | ||
| 189 | - | ||
| 190 | - resolve({ publicKey, privateKey }); | ||
| 191 | - }); | ||
| 192 | - }); | ||
| 176 | + const keyAlgorithm = { name }; | ||
| 177 | + | ||
| 178 | + const publicKey = | ||
| 179 | + new InternalCryptoKey( | ||
| 180 | + keyPair.publicKey, | ||
| 181 | + keyAlgorithm, | ||
| 182 | + publicUsages, | ||
| 183 | + true); | ||
| 184 | + | ||
| 185 | + const privateKey = | ||
| 186 | + new InternalCryptoKey( | ||
| 187 | + keyPair.privateKey, | ||
| 188 | + keyAlgorithm, | ||
| 189 | + privateUsages, | ||
| 190 | + extractable); | ||
| 191 | + | ||
| 192 | + return { privateKey, publicKey }; | ||
| 193 | 193 | } | |
| 194 | 194 | ||
| 195 | 195 | function cfrgExportKey(key, format) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,6 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | ObjectKeys, | |
| 5 | - Promise, | ||
| 6 | 5 | SafeSet, | |
| 7 | 6 | } = primordials; | |
| 8 | 7 | ||
@@ -42,10 +41,11 @@ const { | |||
| 42 | 41 | ||
| 43 | 42 | const { | |
| 44 | 43 | lazyDOMException, | |
| 44 | + promisify, | ||
| 45 | 45 | } = require('internal/util'); | |
| 46 | 46 | ||
| 47 | 47 | const { | |
| 48 | - generateKeyPair, | ||
| 48 | + generateKeyPair: _generateKeyPair, | ||
| 49 | 49 | } = require('internal/crypto/keygen'); | |
| 50 | 50 | ||
| 51 | 51 | const { | |
@@ -56,6 +56,8 @@ const { | |||
| 56 | 56 | createPublicKey, | |
| 57 | 57 | } = require('internal/crypto/keys'); | |
| 58 | 58 | ||
| 59 | + const generateKeyPair = promisify(_generateKeyPair); | ||
| 60 | + | ||
| 59 | 61 | function verifyAcceptableEcKeyUse(name, type, usages) { | |
| 60 | 62 | let checkSet; | |
| 61 | 63 | switch (name) { | |
@@ -111,46 +113,44 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) { | |||
| 111 | 113 | } | |
| 112 | 114 | // Fall through | |
| 113 | 115 | } | |
| 114 | - return new Promise((resolve, reject) => { | ||
| 115 | - generateKeyPair('ec', { namedCurve }, (err, pubKey, privKey) => { | ||
| 116 | - if (err) { | ||
| 117 | - return reject(lazyDOMException( | ||
| 118 | - 'The operation failed for an operation-specific reason', | ||
| 119 | - 'OperationError')); | ||
| 120 | - } | ||
| 121 | 116 | ||
| 122 | - const algorithm = { name, namedCurve }; | ||
| 117 | + const keypair = await generateKeyPair('ec', { namedCurve }).catch((err) => { | ||
| 118 | + // TODO(@panva): add err as cause to DOMException | ||
| 119 | + throw lazyDOMException( | ||
| 120 | + 'The operation failed for an operation-specific reason', | ||
| 121 | + 'OperationError'); | ||
| 122 | + }); | ||
| 123 | 123 | ||
| 124 | - let publicUsages; | ||
| 125 | - let privateUsages; | ||
| 126 | - switch (name) { | ||
| 127 | - case 'ECDSA': | ||
| 128 | - publicUsages = getUsagesUnion(usageSet, 'verify'); | ||
| 129 | - privateUsages = getUsagesUnion(usageSet, 'sign'); | ||
| 130 | - break; | ||
| 131 | - case 'ECDH': | ||
| 132 | - publicUsages = []; | ||
| 133 | - privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits'); | ||
| 134 | - break; | ||
| 135 | - } | ||
| 124 | + let publicUsages; | ||
| 125 | + let privateUsages; | ||
| 126 | + switch (name) { | ||
| 127 | + case 'ECDSA': | ||
| 128 | + publicUsages = getUsagesUnion(usageSet, 'verify'); | ||
| 129 | + privateUsages = getUsagesUnion(usageSet, 'sign'); | ||
| 130 | + break; | ||
| 131 | + case 'ECDH': | ||
| 132 | + publicUsages = []; | ||
| 133 | + privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits'); | ||
| 134 | + break; | ||
| 135 | + } | ||
| 136 | 136 | ||
| 137 | - const publicKey = | ||
| 138 | - new InternalCryptoKey( | ||
| 139 | - pubKey, | ||
| 140 | - algorithm, | ||
| 141 | - publicUsages, | ||
| 142 | - true); | ||
| 143 | - | ||
| 144 | - const privateKey = | ||
| 145 | - new InternalCryptoKey( | ||
| 146 | - privKey, | ||
| 147 | - algorithm, | ||
| 148 | - privateUsages, | ||
| 149 | - extractable); | ||
| 150 | - | ||
| 151 | - resolve({ publicKey, privateKey }); | ||
| 152 | - }); | ||
| 153 | - }); | ||
| 137 | + const keyAlgorithm = { name, namedCurve }; | ||
| 138 | + | ||
| 139 | + const publicKey = | ||
| 140 | + new InternalCryptoKey( | ||
| 141 | + keypair.publicKey, | ||
| 142 | + keyAlgorithm, | ||
| 143 | + publicUsages, | ||
| 144 | + true); | ||
| 145 | + | ||
| 146 | + const privateKey = | ||
| 147 | + new InternalCryptoKey( | ||
| 148 | + keypair.privateKey, | ||
| 149 | + keyAlgorithm, | ||
| 150 | + privateUsages, | ||
| 151 | + extractable); | ||
| 152 | + | ||
| 153 | + return { publicKey, privateKey }; | ||
| 154 | 154 | } | |
| 155 | 155 | ||
| 156 | 156 | function ecExportKey(key, format) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,6 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | ArrayFrom, | |
| 5 | - Promise, | ||
| 6 | 5 | SafeSet, | |
| 7 | 6 | } = primordials; | |
| 8 | 7 | ||
@@ -27,6 +26,7 @@ const { | |||
| 27 | 26 | ||
| 28 | 27 | const { | |
| 29 | 28 | lazyDOMException, | |
| 29 | + promisify, | ||
| 30 | 30 | } = require('internal/util'); | |
| 31 | 31 | ||
| 32 | 32 | const { | |
@@ -36,7 +36,7 @@ const { | |||
| 36 | 36 | } = require('internal/errors'); | |
| 37 | 37 | ||
| 38 | 38 | const { | |
| 39 | - generateKey, | ||
| 39 | + generateKey: _generateKey, | ||
| 40 | 40 | } = require('internal/crypto/keygen'); | |
| 41 | 41 | ||
| 42 | 42 | const { | |
@@ -45,6 +45,8 @@ const { | |||
| 45 | 45 | createSecretKey, | |
| 46 | 46 | } = require('internal/crypto/keys'); | |
| 47 | 47 | ||
| 48 | + const generateKey = promisify(_generateKey); | ||
| 49 | + | ||
| 48 | 50 | async function hmacGenerateKey(algorithm, extractable, keyUsages) { | |
| 49 | 51 | const { hash, name } = algorithm; | |
| 50 | 52 | let { length } = algorithm; | |
@@ -62,21 +64,19 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) { | |||
| 62 | 64 | 'Unsupported key usage for an HMAC key', | |
| 63 | 65 | 'SyntaxError'); | |
| 64 | 66 | } | |
| 65 | - return new Promise((resolve, reject) => { | ||
| 66 | - generateKey('hmac', { length }, (err, key) => { | ||
| 67 | - if (err) { | ||
| 68 | - return reject(lazyDOMException( | ||
| 69 | - 'The operation failed for an operation-specific reason', | ||
| 70 | - 'OperationError')); | ||
| 71 | - } | ||
| 72 | 67 | ||
| 73 | - resolve(new InternalCryptoKey( | ||
| 74 | - key, | ||
| 75 | - { name, length, hash: { name: hash.name } }, | ||
| 76 | - ArrayFrom(usageSet), | ||
| 77 | - extractable)); | ||
| 78 | - }); | ||
| 68 | + const key = await generateKey('hmac', { length }).catch((err) => { | ||
| 69 | + // TODO(@panva): add err as cause to DOMException | ||
| 70 | + throw lazyDOMException( | ||
| 71 | + 'The operation failed for an operation-specific reason', | ||
| 72 | + 'OperationError'); | ||
| 79 | 73 | }); | |
| 74 | + | ||
| 75 | + return new InternalCryptoKey( | ||
| 76 | + key, | ||
| 77 | + { name, length, hash: { name: hash.name } }, | ||
| 78 | + ArrayFrom(usageSet), | ||
| 79 | + extractable); | ||
| 80 | 80 | } | |
| 81 | 81 | ||
| 82 | 82 | async function hmacImportKey( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments