Best JavaScript code snippet using wpt
ecdsa.js
Source:ecdsa.js
...7 // for the algorithm that drives these tests.8 var testVectors = getTestVectors();9 // Test verification first, because signing tests rely on that working10 testVectors.forEach(function(vector) {11 var promise = importVectorKeys(vector, ["verify"], ["sign"])12 .then(function(vectors) {13 var algorithm = {name: vector.algorithmName, hash: vector.hashName};14 promise_test(function(test) {15 var operation = subtle.verify(algorithm, vector.publicKey, vector.signature, vector.plaintext)16 .then(function(is_verified) {17 assert_true(is_verified, "Signature verified");18 }, function(err) {19 assert_unreached("Verification should not throw error " + vector.name + ": " + err.message + "'");20 });21 return operation;22 }, vector.name + " verification");23 }, function(err) {24 // We need a failed test if the importVectorKey operation fails, so25 // we know we never tested verification.26 promise_test(function(test) {27 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");28 }, "importVectorKeys step: " + vector.name + " verification");29 });30 all_promises.push(promise);31 });32 // Test verification with an altered buffer after call33 testVectors.forEach(function(vector) {34 var promise = importVectorKeys(vector, ["verify"], ["sign"])35 .then(function(vectors) {36 var algorithm = {name: vector.algorithmName, hash: vector.hashName};37 promise_test(function(test) {38 var signature = copyBuffer(vector.signature);39 var operation = subtle.verify(algorithm, vector.publicKey, signature, vector.plaintext)40 .then(function(is_verified) {41 assert_true(is_verified, "Signature verified");42 }, function(err) {43 assert_unreached("Verification should not throw error " + vector.name + ": " + err.message + "'");44 });45 signature[0] = 255 - signature[0];46 return operation;47 }, vector.name + " verification with altered signature after call");48 }, function(err) {49 promise_test(function(test) {50 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");51 }, "importVectorKeys step: " + vector.name + " verification with altered signature after call");52 });53 all_promises.push(promise);54 });55 // Check for successful verification even if plaintext is altered after call.56 testVectors.forEach(function(vector) {57 var promise = importVectorKeys(vector, ["verify"], ["sign"])58 .then(function(vectors) {59 var algorithm = {name: vector.algorithmName, hash: vector.hashName};60 promise_test(function(test) {61 var plaintext = copyBuffer(vector.plaintext);62 var operation = subtle.verify(algorithm, vector.publicKey, vector.signature, plaintext)63 .then(function(is_verified) {64 assert_true(is_verified, "Signature verified");65 }, function(err) {66 assert_unreached("Verification should not throw error " + vector.name + ": " + err.message + "'");67 });68 plaintext[0] = 255 - plaintext[0];69 return operation;70 }, vector.name + " with altered plaintext after call");71 }, function(err) {72 promise_test(function(test) {73 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");74 }, "importVectorKeys step: " + vector.name + " with altered plaintext after call");75 });76 all_promises.push(promise);77 });78 // Check for failures due to using privateKey to verify.79 testVectors.forEach(function(vector) {80 var promise = importVectorKeys(vector, ["verify"], ["sign"])81 .then(function(vectors) {82 var algorithm = {name: vector.algorithmName, hash: vector.hashName};83 promise_test(function(test) {84 return subtle.verify(algorithm, vector.privateKey, vector.signature, vector.plaintext)85 .then(function(plaintext) {86 assert_unreached("Should have thrown error for using privateKey to verify in " + vector.name + ": " + err.message + "'");87 }, function(err) {88 assert_equals(err.name, "InvalidAccessError", "Should throw InvalidAccessError instead of '" + err.message + "'");89 });90 }, vector.name + " using privateKey to verify");91 }, function(err) {92 promise_test(function(test) {93 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");94 }, "importVectorKeys step: " + vector.name + " using privateKey to verify");95 });96 all_promises.push(promise);97 });98 // Check for failures due to using publicKey to sign.99 testVectors.forEach(function(vector) {100 var promise = importVectorKeys(vector, ["verify"], ["sign"])101 .then(function(vectors) {102 var algorithm = {name: vector.algorithmName, hash: vector.hashName};103 promise_test(function(test) {104 return subtle.sign(algorithm, vector.publicKey, vector.plaintext)105 .then(function(signature) {106 assert_unreached("Should have thrown error for using publicKey to sign in " + vector.name + ": " + err.message + "'");107 }, function(err) {108 assert_equals(err.name, "InvalidAccessError", "Should throw InvalidAccessError instead of '" + err.message + "'");109 });110 }, vector.name + " using publicKey to sign");111 }, function(err) {112 promise_test(function(test) {113 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");114 }, "importVectorKeys step: " + vector.name + " using publicKey to sign");115 });116 all_promises.push(promise);117 });118 // Check for failures due to no "verify" usage.119 testVectors.forEach(function(originalVector) {120 var vector = Object.assign({}, originalVector);121 var promise = importVectorKeys(vector, [], ["sign"])122 .then(function(vectors) {123 var algorithm = {name: vector.algorithmName, hash: vector.hashName};124 promise_test(function(test) {125 return subtle.verify(algorithm, vector.publicKey, vector.signature, vector.plaintext)126 .then(function(plaintext) {127 assert_unreached("Should have thrown error for no verify usage in " + vector.name + ": " + err.message + "'");128 }, function(err) {129 assert_equals(err.name, "InvalidAccessError", "Should throw InvalidAccessError instead of '" + err.message + "'");130 });131 }, vector.name + " no verify usage");132 }, function(err) {133 promise_test(function(test) {134 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");135 }, "importVectorKeys step: " + vector.name + " no verify usage");136 });137 all_promises.push(promise);138 });139 // Check for successful signing and verification.140 testVectors.forEach(function(vector) {141 var promise = importVectorKeys(vector, ["verify"], ["sign"])142 .then(function(vectors) {143 var algorithm = {name: vector.algorithmName, hash: vector.hashName};144 promise_test(function(test) {145 return subtle.sign(algorithm, vector.privateKey, vector.plaintext)146 .then(function(signature) {147 // Can we verify the signature?148 return subtle.verify(algorithm, vector.publicKey, signature, vector.plaintext)149 .then(function(is_verified) {150 assert_true(is_verified, "Round trip verification works");151 return signature;152 }, function(err) {153 assert_unreached("verify error for test " + vector.name + ": " + err.message + "'");154 });155 }, function(err) {156 assert_unreached("sign error for test " + vector.name + ": '" + err.message + "'");157 });158 }, vector.name + " round trip");159 }, function(err) {160 // We need a failed test if the importVectorKey operation fails, so161 // we know we never tested signing or verifying162 promise_test(function(test) {163 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");164 }, "importVectorKeys step: " + vector.name + " round trip");165 });166 all_promises.push(promise);167 });168 // Test signing with the wrong algorithm169 testVectors.forEach(function(vector) {170 // Want to get the key for the wrong algorithm171 var promise = subtle.generateKey({name: "HMAC", hash: "SHA-1"}, false, ["sign", "verify"])172 .then(function(wrongKey) {173 var algorithm = {name: vector.algorithmName, hash: vector.hashName};174 return importVectorKeys(vector, ["verify"], ["sign"])175 .then(function(vectors) {176 promise_test(function(test) {177 var operation = subtle.sign(algorithm, wrongKey, vector.plaintext)178 .then(function(signature) {179 assert_unreached("Signing should not have succeeded for " + vector.name);180 }, function(err) {181 assert_equals(err.name, "InvalidAccessError", "Should have thrown InvalidAccessError instead of '" + err.message + "'");182 });183 return operation;184 }, vector.name + " signing with wrong algorithm name");185 }, function(err) {186 // We need a failed test if the importVectorKey operation fails, so187 // we know we never tested verification.188 promise_test(function(test) {189 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");190 }, "importVectorKeys step: " + vector.name + " signing with wrong algorithm name");191 });192 }, function(err) {193 promise_test(function(test) {194 assert_unreached("Generate wrong key for test " + vector.name + " failed: '" + err.message + "'");195 }, "generate wrong key step: " + vector.name + " signing with wrong algorithm name");196 });197 all_promises.push(promise);198 });199 // Test verification with the wrong algorithm200 testVectors.forEach(function(vector) {201 // Want to get the key for the wrong algorithm202 var promise = subtle.generateKey({name: "HMAC", hash: "SHA-1"}, false, ["sign", "verify"])203 .then(function(wrongKey) {204 return importVectorKeys(vector, ["verify"], ["sign"])205 .then(function(vectors) {206 var algorithm = {name: vector.algorithmName, hash: vector.hashName};207 promise_test(function(test) {208 var operation = subtle.verify(algorithm, wrongKey, vector.signature, vector.plaintext)209 .then(function(signature) {210 assert_unreached("Verifying should not have succeeded for " + vector.name);211 }, function(err) {212 assert_equals(err.name, "InvalidAccessError", "Should have thrown InvalidAccessError instead of '" + err.message + "'");213 });214 return operation;215 }, vector.name + " verifying with wrong algorithm name");216 }, function(err) {217 // We need a failed test if the importVectorKey operation fails, so218 // we know we never tested verification.219 promise_test(function(test) {220 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");221 }, "importVectorKeys step: " + vector.name + " verifying with wrong algorithm name");222 });223 }, function(err) {224 promise_test(function(test) {225 assert_unreached("Generate wrong key for test " + vector.name + " failed: '" + err.message + "'");226 }, "generate wrong key step: " + vector.name + " verifying with wrong algorithm name");227 });228 all_promises.push(promise);229 });230 // Test verification fails with wrong signature231 testVectors.forEach(function(vector) {232 var promise = importVectorKeys(vector, ["verify"], ["sign"])233 .then(function(vectors) {234 var algorithm = {name: vector.algorithmName, hash: vector.hashName};235 var signature = copyBuffer(vector.signature);236 signature[0] = 255 - signature[0];237 promise_test(function(test) {238 var operation = subtle.verify(algorithm, vector.publicKey, signature, vector.plaintext)239 .then(function(is_verified) {240 assert_false(is_verified, "Signature NOT verified");241 }, function(err) {242 assert_unreached("Verification should not throw error " + vector.name + ": " + err.message + "'");243 });244 return operation;245 }, vector.name + " verification failure due to altered signature");246 }, function(err) {247 // We need a failed test if the importVectorKey operation fails, so248 // we know we never tested verification.249 promise_test(function(test) {250 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");251 }, "importVectorKeys step: " + vector.name + " verification failure due to altered signature");252 });253 all_promises.push(promise);254 });255 // Test verification fails with wrong hash256 testVectors.forEach(function(vector) {257 var promise = importVectorKeys(vector, ["verify"], ["sign"])258 .then(function(vectors) {259 var hashName = "SHA-1";260 if (vector.hashName === "SHA-1") {261 hashName = "SHA-256"262 }263 var algorithm = {name: vector.algorithmName, hash: hashName};264 promise_test(function(test) {265 var operation = subtle.verify(algorithm, vector.publicKey, vector.signature, vector.plaintext)266 .then(function(is_verified) {267 assert_false(is_verified, "Signature NOT verified");268 }, function(err) {269 assert_unreached("Verification should not throw error " + vector.name + ": " + err.message + "'");270 });271 return operation;272 }, vector.name + " verification failure due to wrong hash");273 }, function(err) {274 // We need a failed test if the importVectorKey operation fails, so275 // we know we never tested verification.276 promise_test(function(test) {277 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");278 }, "importVectorKeys step: " + vector.name + " verification failure due to wrong hash");279 });280 all_promises.push(promise);281 });282 // Test verification fails with bad hash name283 testVectors.forEach(function(vector) {284 var promise = importVectorKeys(vector, ["verify"], ["sign"])285 .then(function(vectors) {286 // use the wrong name for the hash287 var hashName = vector.hashName.substring(0, 3) + vector.hashName.substring(4);288 var algorithm = {name: vector.algorithmName, hash: hashName};289 promise_test(function(test) {290 var operation = subtle.verify(algorithm, vector.publicKey, vector.signature, vector.plaintext)291 .then(function(is_verified) {292 assert_unreached("Verification should throw an error");293 }, function(err) {294 assert_equals(err.name, "NotSupportedError", "Correctly throws NotSupportedError for illegal hash name")295 });296 return operation;297 }, vector.name + " verification failure due to bad hash name");298 }, function(err) {299 // We need a failed test if the importVectorKey operation fails, so300 // we know we never tested verification.301 promise_test(function(test) {302 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");303 }, "importVectorKeys step: " + vector.name + " verification failure due to bad hash name");304 });305 all_promises.push(promise);306 });307 // Test verification fails with short (odd length) signature308 testVectors.forEach(function(vector) {309 var promise = importVectorKeys(vector, ["verify"], ["sign"])310 .then(function(vectors) {311 var algorithm = {name: vector.algorithmName, hash: vector.hashName};312 var signature = vector.signature.slice(1); // Skip the first byte313 promise_test(function(test) {314 var operation = subtle.verify(algorithm, vector.publicKey, signature, vector.plaintext)315 .then(function(is_verified) {316 assert_false(is_verified, "Signature NOT verified");317 }, function(err) {318 assert_unreached("Verification should not throw error " + vector.name + ": " + err.message + "'");319 });320 return operation;321 }, vector.name + " verification failure due to shortened signature");322 }, function(err) {323 // We need a failed test if the importVectorKey operation fails, so324 // we know we never tested verification.325 promise_test(function(test) {326 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");327 }, "importVectorKeys step: " + vector.name + " verification failure due to shortened signature");328 });329 all_promises.push(promise);330 });331 // Test verification fails with wrong plaintext332 testVectors.forEach(function(vector) {333 var promise = importVectorKeys(vector, ["verify"], ["sign"])334 .then(function(vectors) {335 var algorithm = {name: vector.algorithmName, hash: vector.hashName};336 var plaintext = copyBuffer(vector.plaintext);337 plaintext[0] = 255 - plaintext[0];338 promise_test(function(test) {339 var operation = subtle.verify(algorithm, vector.publicKey, vector.signature, plaintext)340 .then(function(is_verified) {341 assert_false(is_verified, "Signature NOT verified");342 }, function(err) {343 assert_unreached("Verification should not throw error " + vector.name + ": " + err.message + "'");344 });345 return operation;346 }, vector.name + " verification failure due to altered plaintext");347 }, function(err) {348 // We need a failed test if the importVectorKey operation fails, so349 // we know we never tested verification.350 promise_test(function(test) {351 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");352 }, "importVectorKeys step: " + vector.name + " verification failure due to altered plaintext");353 });354 all_promises.push(promise);355 });356 promise_test(function() {357 return Promise.all(all_promises)358 .then(function() {done();})359 .catch(function() {done();})360 }, "setup");361 // A test vector has all needed fields for signing and verifying, EXCEPT that the362 // key field may be null. This function replaces that null with the Correct363 // CryptoKey object.364 //365 // Returns a Promise that yields an updated vector on success.366 function importVectorKeys(vector, publicKeyUsages, privateKeyUsages) {367 var publicPromise, privatePromise;368 if (vector.publicKey !== null) {369 publicPromise = new Promise(function(resolve, reject) {370 resolve(vector);371 });372 } else {373 publicPromise = subtle.importKey(vector.publicKeyFormat, vector.publicKeyBuffer, {name: vector.algorithmName, namedCurve: vector.namedCurve}, false, publicKeyUsages)374 .then(function(key) {375 vector.publicKey = key;376 return vector;377 }); // Returns a copy of the sourceBuffer it is sent.378 }379 if (vector.privateKey !== null) {380 privatePromise = new Promise(function(resolve, reject) {...
rsa.js
Source:rsa.js
...8 var passingVectors = vectors.passing;9 var failingVectors = vectors.failing;10 // Test decryption, first, because encryption tests rely on that working11 passingVectors.forEach(function(vector) {12 var promise = importVectorKeys(vector, ["encrypt"], ["decrypt"])13 .then(function(vectors) {14 // Get a one byte longer plaintext to encrypt15 if (!("ciphertext" in vector)) {16 return;17 }18 promise_test(function(test) {19 return subtle.decrypt(vector.algorithm, vector.privateKey, vector.ciphertext)20 .then(function(plaintext) {21 assert_true(equalBuffers(plaintext, vector.plaintext, "Decryption works"));22 }, function(err) {23 assert_unreached("Decryption should not throw error " + vector.name + ": " + err.message + "'");24 });25 }, vector.name + " decryption");26 }, function(err) {27 // We need a failed test if the importVectorKey operation fails, so28 // we know we never tested encryption29 promise_test(function(test) {30 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");31 }, "importVectorKeys step: " + vector.name + " decryption");32 });33 all_promises.push(promise);34 });35 // Test decryption with an altered buffer36 passingVectors.forEach(function(vector) {37 var promise = importVectorKeys(vector, ["encrypt"], ["decrypt"])38 .then(function(vectors) {39 // Get a one byte longer plaintext to encrypt40 if (!("ciphertext" in vector)) {41 return;42 }43 promise_test(function(test) {44 var ciphertext = copyBuffer(vector.ciphertext);45 var operation = subtle.decrypt(vector.algorithm, vector.privateKey, ciphertext)46 .then(function(plaintext) {47 assert_true(equalBuffers(plaintext, vector.plaintext, "Decryption works"));48 }, function(err) {49 assert_unreached("Decryption should not throw error " + vector.name + ": " + err.message + "'");50 });51 ciphertext[0] = 255 - ciphertext[0];52 return operation;53 }, vector.name + " decryption with altered ciphertext");54 }, function(err) {55 // We need a failed test if the importVectorKey operation fails, so56 // we know we never tested encryption57 promise_test(function(test) {58 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");59 }, "importVectorKeys step: " + vector.name + " decryption with altered ciphertext");60 });61 all_promises.push(promise);62 });63 // Check for failures due to using publicKey to decrypt.64 passingVectors.forEach(function(vector) {65 var promise = importVectorKeys(vector, ["encrypt"], ["decrypt"])66 .then(function(vectors) {67 promise_test(function(test) {68 return subtle.decrypt(vector.algorithm, vector.publicKey, vector.ciphertext)69 .then(function(plaintext) {70 assert_unreached("Should have thrown error for using publicKey to decrypt in " + vector.name + ": " + err.message + "'");71 }, function(err) {72 assert_equals(err.name, "InvalidAccessError", "Should throw InvalidAccessError instead of " + err.message);73 });74 }, vector.name + " using publicKey to decrypt");75 }, function(err) {76 // We need a failed test if the importVectorKey operation fails, so77 // we know we never tested encryption78 promise_test(function(test) {79 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");80 }, "importVectorKeys step: " + vector.name + " using publicKey to decrypt");81 });82 all_promises.push(promise);83 });84 // Check for failures due to no "decrypt" usage.85 passingVectors.forEach(function(originalVector) {86 var vector = Object.assign({}, originalVector);87 var promise = importVectorKeys(vector, ["encrypt"], ["unwrapKey"])88 .then(function(vectors) {89 // Get a one byte longer plaintext to encrypt90 promise_test(function(test) {91 return subtle.decrypt(vector.algorithm, vector.publicKey, vector.ciphertext)92 .then(function(plaintext) {93 assert_unreached("Should have thrown error for no decrypt usage in " + vector.name + ": " + err.message + "'");94 }, function(err) {95 assert_equals(err.name, "InvalidAccessError", "Should throw InvalidAccessError instead of " + err.message);96 });97 }, vector.name + " no decrypt usage");98 }, function(err) {99 // We need a failed test if the importVectorKey operation fails, so100 // we know we never tested encryption101 promise_test(function(test) {102 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");103 }, "importVectorKeys step: " + vector.name + " no decrypt usage");104 });105 all_promises.push(promise);106 });107 // Check for successful encryption even if plaintext is altered after call.108 passingVectors.forEach(function(vector) {109 var promise = importVectorKeys(vector, ["encrypt"], ["decrypt"])110 .then(function(vectors) {111 promise_test(function(test) {112 var plaintext = copyBuffer(vector.plaintext);113 var operation = subtle.encrypt(vector.algorithm, vector.publicKey, plaintext)114 .then(function(ciphertext) {115 assert_equals(ciphertext.byteLength * 8, vector.privateKey.algorithm.modulusLength, "Ciphertext length matches modulus length");116 // Can we get the original plaintext back via decrypt?117 return subtle.decrypt(vector.algorithm, vector.privateKey, ciphertext)118 .then(function(result) {119 assert_true(equalBuffers(result, vector.plaintext), "Round trip returns original plaintext");120 return ciphertext;121 }, function(err) {122 assert_unreached("decrypt error for test " + vector.name + ": " + err.message + "'");123 });124 })125 .then(function(priorCiphertext) {126 // Will a second encrypt give us different ciphertext, as it should?127 return subtle.encrypt(vector.algorithm, vector.publicKey, vector.plaintext)128 .then(function(ciphertext) {129 assert_false(equalBuffers(priorCiphertext, ciphertext), "Two encrypts give different results")130 }, function(err) {131 assert_unreached("second time encrypt error for test " + vector.name + ": '" + err.message + "'");132 });133 }, function(err) {134 assert_unreached("decrypt error for test " + vector.name + ": '" + err.message + "'");135 });136 plaintext[0] = 255 - plaintext[0];137 return operation;138 }, vector.name + " with altered plaintext");139 }, function(err) {140 // We need a failed test if the importVectorKey operation fails, so141 // we know we never tested encryption142 promise_test(function(test) {143 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");144 }, "importVectorKeys step: " + vector.name + " with altered plaintext");145 });146 all_promises.push(promise);147 });148 // Check for successful encryption.149 passingVectors.forEach(function(vector) {150 var promise = importVectorKeys(vector, ["encrypt"], ["decrypt"])151 .then(function(vectors) {152 promise_test(function(test) {153 return subtle.encrypt(vector.algorithm, vector.publicKey, vector.plaintext)154 .then(function(ciphertext) {155 assert_equals(ciphertext.byteLength * 8, vector.privateKey.algorithm.modulusLength, "Ciphertext length matches modulus length");156 // Can we get the original plaintext back via decrypt?157 return subtle.decrypt(vector.algorithm, vector.privateKey, ciphertext)158 .then(function(result) {159 assert_true(equalBuffers(result, vector.plaintext), "Round trip returns original plaintext");160 return ciphertext;161 }, function(err) {162 assert_unreached("decrypt error for test " + vector.name + ": " + err.message + "'");163 });164 })165 .then(function(priorCiphertext) {166 // Will a second encrypt give us different ciphertext, as it should?167 return subtle.encrypt(vector.algorithm, vector.publicKey, vector.plaintext)168 .then(function(ciphertext) {169 assert_false(equalBuffers(priorCiphertext, ciphertext), "Two encrypts give different results")170 }, function(err) {171 assert_unreached("second time encrypt error for test " + vector.name + ": '" + err.message + "'");172 });173 }, function(err) {174 assert_unreached("decrypt error for test " + vector.name + ": '" + err.message + "'");175 });176 }, vector.name);177 }, function(err) {178 // We need a failed test if the importVectorKey operation fails, so179 // we know we never tested encryption180 promise_test(function(test) {181 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");182 }, "importVectorKeys step: " + vector.name);183 });184 all_promises.push(promise);185 });186 // Check for failures due to too long plaintext.187 passingVectors.forEach(function(vector) {188 var promise = importVectorKeys(vector, ["encrypt"], ["decrypt"])189 .then(function(vectors) {190 // Get a one byte longer plaintext to encrypt191 var plaintext = new Uint8Array(vector.plaintext.byteLength + 1);192 plaintext.set(plaintext, 0);193 plaintext.set(new Uint8Array([32]), vector.plaintext.byteLength);194 promise_test(function(test) {195 return subtle.encrypt(vector.algorithm, vector.publicKey, plaintext)196 .then(function(ciphertext) {197 assert_unreached("Should have thrown error for too long plaintext in " + vector.name + ": " + err.message + "'");198 }, function(err) {199 assert_equals(err.name, "OperationError", "Should throw OperationError instead of " + err.message);200 });201 }, vector.name + " too long plaintext");202 }, function(err) {203 // We need a failed test if the importVectorKey operation fails, so204 // we know we never tested encryption205 promise_test(function(test) {206 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");207 }, "importVectorKeys step: " + vector.name + " too long plaintext");208 });209 all_promises.push(promise);210 });211 // Check for failures due to using privateKey to encrypt.212 passingVectors.forEach(function(vector) {213 var promise = importVectorKeys(vector, ["encrypt"], ["decrypt"])214 .then(function(vectors) {215 promise_test(function(test) {216 return subtle.encrypt(vector.algorithm, vector.privateKey, vector.plaintext)217 .then(function(ciphertext) {218 assert_unreached("Should have thrown error for using privateKey to encrypt in " + vector.name + ": " + err.message + "'");219 }, function(err) {220 assert_equals(err.name, "InvalidAccessError", "Should throw InvalidAccessError instead of " + err.message);221 });222 }, vector.name + " using privateKey to encrypt");223 }, function(err) {224 // We need a failed test if the importVectorKey operation fails, so225 // we know we never tested encryption226 promise_test(function(test) {227 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");228 }, "importVectorKeys step: " + vector.name + " using privateKey to encrypt");229 });230 all_promises.push(promise);231 });232 // Check for failures due to no "encrypt usage".233 passingVectors.forEach(function(originalVector) {234 var vector = Object.assign({}, originalVector);235 var promise = importVectorKeys(vector, [], ["decrypt"])236 .then(function(vectors) {237 // Get a one byte longer plaintext to encrypt238 promise_test(function(test) {239 return subtle.encrypt(vector.algorithm, vector.publicKey, vector.plaintext)240 .then(function(ciphertext) {241 assert_unreached("Should have thrown error for no encrypt usage in " + vector.name + ": " + err.message + "'");242 }, function(err) {243 assert_equals(err.name, "InvalidAccessError", "Should throw InvalidAccessError instead of " + err.message);244 });245 }, vector.name + " no encrypt usage");246 }, function(err) {247 // We need a failed test if the importVectorKey operation fails, so248 // we know we never tested encryption249 promise_test(function(test) {250 assert_unreached("importVectorKeys failed for " + vector.name + ". Message: ''" + err.message + "''");251 }, "importVectorKeys step: " + vector.name + " no encrypt usage");252 });253 all_promises.push(promise);254 });255 Promise.all(all_promises)256 .then(function() {done();})257 .catch(function() {done();})258 // A test vector has all needed fields for encryption, EXCEPT that the259 // key field may be null. This function replaces that null with the Correct260 // CryptoKey object.261 //262 // Returns a Promise that yields an updated vector on success.263 function importVectorKeys(vector, publicKeyUsages, privateKeyUsages) {264 var publicPromise, privatePromise;265 if (vector.publicKey !== null) {266 publicPromise = new Promise(function(resolve, reject) {267 resolve(vector);268 });269 } else {270 publicPromise = subtle.importKey(vector.publicKeyFormat, vector.publicKeyBuffer, {name: vector.algorithm.name, hash: vector.hash}, false, publicKeyUsages)271 .then(function(key) {272 vector.publicKey = key;273 return vector;274 }); // Returns a copy of the sourceBuffer it is sent.275 function copyBuffer(sourceBuffer) {276 var source = new Uint8Array(sourceBuffer);277 var copy = new Uint8Array(sourceBuffer.byteLength)...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var path = require('path');3var fs = require('fs');4var pathToVectorKeys = path.join(__dirname, 'vectorKeys.txt');5var pathToVectorKeys2 = path.join(__dirname, 'vectorKeys2.txt');6var pathToVectorKeys3 = path.join(__dirname, 'vectorKeys3.txt');7var pathToVectorKeys4 = path.join(__dirname, 'vectorKeys4.txt');8var pathToVectorKeys5 = path.join(__dirname, 'vectorKeys5.txt');9var pathToVectorKeys6 = path.join(__dirname, 'vectorKeys6.txt');10var pathToVectorKeys7 = path.join(__dirname, 'vectorKeys7.txt');11var pathToVectorKeys8 = path.join(__dirname, 'vectorKeys8.txt');12var pathToVectorKeys9 = path.join(__dirname, 'vectorKeys9.txt');13var pathToVectorKeys10 = path.join(__dirname, 'vectorKeys10.txt');14var pathToVectorKeys11 = path.join(__dirname, 'vectorKeys11.txt');15var pathToVectorKeys12 = path.join(__dirname, 'vectorKeys12.txt');16var pathToVectorKeys13 = path.join(__dirname, 'vectorKeys13.txt');17var pathToVectorKeys14 = path.join(__dirname, 'vectorKeys14.txt');18var pathToVectorKeys15 = path.join(__dirname, 'vectorKeys15.txt');19var pathToVectorKeys16 = path.join(__dirname, 'vectorKeys16.txt');20var pathToVectorKeys17 = path.join(__dirname, 'vectorKeys17.txt');21var pathToVectorKeys18 = path.join(__dirname, 'vectorKeys18.txt');22var pathToVectorKeys19 = path.join(__dirname, 'vectorKeys19.txt');23var pathToVectorKeys20 = path.join(__dirname, 'vectorKeys20.txt');24var pathToVectorKeys21 = path.join(__dirname, 'vectorKeys21.txt');25var pathToVectorKeys22 = path.join(__dirname, 'vectorKeys22.txt');26var pathToVectorKeys23 = path.join(__dirname, 'vectorKeys23.txt');27var pathToVectorKeys24 = path.join(__dirname, 'vectorKeys24.txt');28var pathToVectorKeys25 = path.join(__dirname, 'vectorKeys25.txt');29var pathToVectorKeys26 = path.join(__dirname, 'vectorKeys26.txt');
Using AI Code Generation
1const wptools = require('wptools');2wptools.importVectorKeys('path/to/your/vector/keys').then(function (res) {3 console.log(res);4}).catch(function (err) {5 console.log(err);6});7const wptools = require('wptools');8wptools.encrypt('path/to/your/vector/keys', 'text to encrypt').then(function (res) {9 console.log(res);10}).catch(function (err) {11 console.log(err);12});13const wptools = require('wptools');14wptools.decrypt('path/to/your/vector/keys', 'text to decrypt').then(function (res) {15 console.log(res);16}).catch(function (err) {17 console.log(err);18});19const wptools = require('wptools');20wptools.encryptFile('path/to/your/vector/keys', 'path/to/your/file').then(function (res) {21 console.log(res);22}).catch(function (err) {23 console.log(err);24});25const wptools = require('wptools');26wptools.decryptFile('path/to/your/vector/keys', 'path/to/your/file').then(function (res) {27 console.log(res);28}).catch(function (err) {29 console.log(err);30});31const wptools = require('wptools');32wptools.encryptFolder('path/to/your/vector/keys', 'path/to/your/folder').then(function (res) {33 console.log(res);34}).catch(function (err) {35 console.log(err);36});
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2wptoolkit.importVectorKeys('./keys','./test.txt');3var wptoolkit = require('wptoolkit');4wptoolkit.encryptFile('./test.txt','./keys');5var wptoolkit = require('wptoolkit');6wptoolkit.decryptFile('./test.txt.enc','./keys');7var wptoolkit = require('wptoolkit');8wptoolkit.encryptFolder('./testFolder','./keys');9var wptoolkit = require('wptoolkit
Using AI Code Generation
1var wptoolkit = require("wptoolkit");2var myVector = wptoolkit.importVectorKeys("./testvector.json");3console.log(myVector);4## importVectorData5var wptoolkit = require("wptoolkit");6var myVector = wptoolkit.importVectorData("./testvector.json");7console.log(myVector);8var wptoolkit = require("wptoolkit");9var myVector = { "key1": "value1", "key2": "value2" };10wptoolkit.exportVectorData(myVector, "./testvector.json");11## importVector12Import vector data from a file and return the vector as a javascript object. This method is used to import vector data from a file and return the vector as a javascript object. The vector data is stored in the file as a JSON object. The method takes a single argument, which is the file path for the vector data file. The method returns a javascript object with the vector data. The method throws an exception if there
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!