From 4513adf6c2a531273a3a082703b6a246da0130ff Mon Sep 17 00:00:00 2001 From: Oleg Bespalov Date: Thu, 18 Apr 2024 10:25:47 +0200 Subject: [PATCH] docs: format examples them according k6-docs ESLint --- .../encrypt-decrypt-aes-cbc.js | 9 ++- .../encrypt-decrypt-aes-ctr.js | 9 ++- .../encrypt-decrypt-aes-gcm.js | 7 +- examples/generateKey/generateKey-aes.js | 23 +++--- examples/generateKey/generateKey-ecdh.js | 23 +++--- examples/generateKey/generateKey-ecdsa.js | 20 +++--- examples/generateKey/generateKey-hmac.js | 33 ++++----- examples/import_export/export-ecdsa-keys.js | 1 - .../import_export/import-export-aes-key.js | 40 +++++------ .../import_export/import-export-hmac-key.js | 49 ++++++------- .../import-export-jwk-aes-key.js | 70 +++++++++--------- .../import-export-jwk-aes-static-key.js | 48 ++++++------- .../import_export/import-export-jwk-ecdsa.js | 41 ++++++----- .../import-export-jwk-hmac-key.js | 71 +++++++++---------- .../import-export-jwk-hmac-static-key.js | 59 ++++++++------- examples/sign_verify/sign-verify-ecdsa.js | 2 +- examples/sign_verify/sign-verify-hmac.js | 61 ++++++++-------- 17 files changed, 277 insertions(+), 289 deletions(-) diff --git a/examples/encrypt_decrypt/encrypt-decrypt-aes-cbc.js b/examples/encrypt_decrypt/encrypt-decrypt-aes-cbc.js index 01e8296..1f32cb5 100644 --- a/examples/encrypt_decrypt/encrypt-decrypt-aes-cbc.js +++ b/examples/encrypt_decrypt/encrypt-decrypt-aes-cbc.js @@ -12,7 +12,7 @@ export default async function () { const encoded = stringToArrayBuffer("Hello, World!"); const iv = crypto.getRandomValues(new Uint8Array(16)); - + const ciphertext = await crypto.subtle.encrypt( { name: "AES-CBC", @@ -28,10 +28,13 @@ export default async function () { iv: iv, }, key, - ciphertext, + ciphertext ); - console.log("deciphered text == original text: ", arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)) + console.log( + "deciphered text == original text: ", + arrayBufferToHex(plaintext) === arrayBufferToHex(encoded) + ); } function arrayBufferToHex(buffer) { diff --git a/examples/encrypt_decrypt/encrypt-decrypt-aes-ctr.js b/examples/encrypt_decrypt/encrypt-decrypt-aes-ctr.js index 28ca4b2..7ceacfb 100644 --- a/examples/encrypt_decrypt/encrypt-decrypt-aes-ctr.js +++ b/examples/encrypt_decrypt/encrypt-decrypt-aes-ctr.js @@ -12,7 +12,7 @@ export default async function () { const encoded = string2ArrayBuffer("Hello World"); const counter = crypto.getRandomValues(new Uint8Array(16)); - + const ciphertext = await crypto.subtle.encrypt( { name: "AES-CTR", @@ -30,10 +30,13 @@ export default async function () { length: 64, }, key, - ciphertext, + ciphertext ); - console.log("deciphered text == original text: ", arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)) + console.log( + "deciphered text == original text: ", + arrayBufferToHex(plaintext) === arrayBufferToHex(encoded) + ); } function arrayBufferToHex(buffer) { diff --git a/examples/encrypt_decrypt/encrypt-decrypt-aes-gcm.js b/examples/encrypt_decrypt/encrypt-decrypt-aes-gcm.js index 025a697..4ef5c5f 100644 --- a/examples/encrypt_decrypt/encrypt-decrypt-aes-gcm.js +++ b/examples/encrypt_decrypt/encrypt-decrypt-aes-gcm.js @@ -28,10 +28,13 @@ export default async function () { iv: iv, }, key, - ciphertext, + ciphertext ); - console.log("deciphered text == original text: ", arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)) + console.log( + "deciphered text == original text: ", + arrayBufferToHex(plaintext) === arrayBufferToHex(encoded) + ); } function arrayBufferToHex(buffer) { diff --git a/examples/generateKey/generateKey-aes.js b/examples/generateKey/generateKey-aes.js index 4b5b6c1..4334b16 100644 --- a/examples/generateKey/generateKey-aes.js +++ b/examples/generateKey/generateKey-aes.js @@ -1,17 +1,14 @@ import { crypto } from "k6/x/webcrypto"; export default async function () { - const key = await crypto.subtle.generateKey( - { - name: "AES-CBC", - length: 256 - }, - true, - [ - "encrypt", - "decrypt", - ] - ); + const key = await crypto.subtle.generateKey( + { + name: "AES-CBC", + length: 256, + }, + true, + ["encrypt", "decrypt"] + ); - console.log(JSON.stringify(key)) -} \ No newline at end of file + console.log(JSON.stringify(key)); +} diff --git a/examples/generateKey/generateKey-ecdh.js b/examples/generateKey/generateKey-ecdh.js index 236f138..a2ad175 100644 --- a/examples/generateKey/generateKey-ecdh.js +++ b/examples/generateKey/generateKey-ecdh.js @@ -1,17 +1,14 @@ import { crypto } from "k6/x/webcrypto"; export default async function () { - const key = await crypto.subtle.generateKey( - { - name: "ECDH", - namedCurve: "P-256" - }, - true, - [ - "deriveKey", - "deriveBits" - ] - ); + const key = await crypto.subtle.generateKey( + { + name: "ECDH", + namedCurve: "P-256", + }, + true, + ["deriveKey", "deriveBits"] + ); - console.log(JSON.stringify(key)) -} \ No newline at end of file + console.log(JSON.stringify(key)); +} diff --git a/examples/generateKey/generateKey-ecdsa.js b/examples/generateKey/generateKey-ecdsa.js index b387bea..cc70d23 100644 --- a/examples/generateKey/generateKey-ecdsa.js +++ b/examples/generateKey/generateKey-ecdsa.js @@ -1,14 +1,14 @@ import { crypto } from "k6/x/webcrypto"; export default async function () { - const key = await crypto.subtle.generateKey( - { - name: "ECDSA", - namedCurve: "P-256" - }, - true, - ["sign", "verify"] - ); + const key = await crypto.subtle.generateKey( + { + name: "ECDSA", + namedCurve: "P-256", + }, + true, + ["sign", "verify"] + ); - console.log(JSON.stringify(key)) -} \ No newline at end of file + console.log(JSON.stringify(key)); +} diff --git a/examples/generateKey/generateKey-hmac.js b/examples/generateKey/generateKey-hmac.js index 17d892c..5b2bd17 100644 --- a/examples/generateKey/generateKey-hmac.js +++ b/examples/generateKey/generateKey-hmac.js @@ -1,21 +1,18 @@ import { crypto } from "k6/x/webcrypto"; export default async function () { - try { - const key = await crypto.subtle.generateKey( - { - name: "HMAC", - hash: { name: "SHA-512" }, - length: 256, - }, - true, - [ - "sign", - "verify", - ] - ); - console.log(JSON.stringify(key)) - } catch (e) { - console.log(JSON.stringify(e)) - } -} \ No newline at end of file + try { + const key = await crypto.subtle.generateKey( + { + name: "HMAC", + hash: { name: "SHA-512" }, + length: 256, + }, + true, + ["sign", "verify"] + ); + console.log(JSON.stringify(key)); + } catch (e) { + console.log(JSON.stringify(e)); + } +} diff --git a/examples/import_export/export-ecdsa-keys.js b/examples/import_export/export-ecdsa-keys.js index 2449485..f354ba8 100644 --- a/examples/import_export/export-ecdsa-keys.js +++ b/examples/import_export/export-ecdsa-keys.js @@ -27,4 +27,3 @@ const printArrayBuffer = (buffer) => { let view = new Uint8Array(buffer); return Array.from(view); }; - diff --git a/examples/import_export/import-export-aes-key.js b/examples/import_export/import-export-aes-key.js index 78ac06d..d2ee621 100644 --- a/examples/import_export/import-export-aes-key.js +++ b/examples/import_export/import-export-aes-key.js @@ -1,26 +1,24 @@ import { crypto } from "k6/x/webcrypto"; - export default async function () { - const generatedKey = await crypto.subtle.generateKey( - { - name: "AES-CBC", - length: "256" - }, - true, - [ - "encrypt", - "decrypt", - ] - ); +export default async function () { + const generatedKey = await crypto.subtle.generateKey( + { + name: "AES-CBC", + length: "256", + }, + true, + ["encrypt", "decrypt"] + ); - const exportedKey = await crypto.subtle.exportKey("raw", generatedKey); + const exportedKey = await crypto.subtle.exportKey("raw", generatedKey); - const importedKey = await crypto.subtle.importKey( - "raw", - exportedKey, - "AES-CBC", - true, ["encrypt", "decrypt"] - ); + const importedKey = await crypto.subtle.importKey( + "raw", + exportedKey, + "AES-CBC", + true, + ["encrypt", "decrypt"] + ); - console.log(JSON.stringify(importedKey)) -} \ No newline at end of file + console.log(JSON.stringify(importedKey)); +} diff --git a/examples/import_export/import-export-hmac-key.js b/examples/import_export/import-export-hmac-key.js index eec727d..9eda527 100644 --- a/examples/import_export/import-export-hmac-key.js +++ b/examples/import_export/import-export-hmac-key.js @@ -1,31 +1,28 @@ import { crypto } from "k6/x/webcrypto"; - export default async function () { - try { - const generatedKey = await crypto.subtle.generateKey( - { - name: "HMAC", - hash: { name: "SHA-256" }, - }, - true, - [ - "sign", - "verify", - ] - ); +export default async function () { + try { + const generatedKey = await crypto.subtle.generateKey( + { + name: "HMAC", + hash: { name: "SHA-256" }, + }, + true, + ["sign", "verify"] + ); - const exportedKey = await crypto.subtle.exportKey("raw", generatedKey); + const exportedKey = await crypto.subtle.exportKey("raw", generatedKey); - const importedKey = await crypto.subtle.importKey( - "raw", - exportedKey, - { name: "HMAC", hash: { name: "SHA-256" } }, - true, ["sign", "verify"] - ); + const importedKey = await crypto.subtle.importKey( + "raw", + exportedKey, + { name: "HMAC", hash: { name: "SHA-256" } }, + true, + ["sign", "verify"] + ); - console.log(JSON.stringify(importedKey)) - } catch (err) { - console.log(JSON.stringify(err)); - } - -} \ No newline at end of file + console.log(JSON.stringify(importedKey)); + } catch (err) { + console.log(JSON.stringify(err)); + } +} diff --git a/examples/import_export/import-export-jwk-aes-key.js b/examples/import_export/import-export-jwk-aes-key.js index 6a2e3b2..0d4f494 100644 --- a/examples/import_export/import-export-jwk-aes-key.js +++ b/examples/import_export/import-export-jwk-aes-key.js @@ -1,38 +1,36 @@ import { crypto } from "k6/x/webcrypto"; - export default async function () { - try { - const generatedKey = await crypto.subtle.generateKey( - { - name: "AES-CBC", - length: "256" - }, - true, - [ - "encrypt", - "decrypt", - ] - ); - - console.log("generated: " + JSON.stringify(generatedKey)); - - const exportedKey = await crypto.subtle.exportKey("jwk", generatedKey); - - console.log("exported: " + JSON.stringify(exportedKey)); - - const importedKey = await crypto.subtle.importKey( - "jwk", - exportedKey, - "AES-CBC", - true, ["encrypt", "decrypt"] - ); - - console.log("imported: " + JSON.stringify(importedKey)); - - const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); - - console.log("exported again: " + JSON.stringify(exportedAgain)); - } catch (err) { - console.log(JSON.stringify(err)); - } -} \ No newline at end of file +export default async function () { + try { + const generatedKey = await crypto.subtle.generateKey( + { + name: "AES-CBC", + length: "256", + }, + true, + ["encrypt", "decrypt"] + ); + + console.log("generated: " + JSON.stringify(generatedKey)); + + const exportedKey = await crypto.subtle.exportKey("jwk", generatedKey); + + console.log("exported: " + JSON.stringify(exportedKey)); + + const importedKey = await crypto.subtle.importKey( + "jwk", + exportedKey, + "AES-CBC", + true, + ["encrypt", "decrypt"] + ); + + console.log("imported: " + JSON.stringify(importedKey)); + + const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); + + console.log("exported again: " + JSON.stringify(exportedAgain)); + } catch (err) { + console.log(JSON.stringify(err)); + } +} diff --git a/examples/import_export/import-export-jwk-aes-static-key.js b/examples/import_export/import-export-jwk-aes-static-key.js index 8a87c01..9634733 100644 --- a/examples/import_export/import-export-jwk-aes-static-key.js +++ b/examples/import_export/import-export-jwk-aes-static-key.js @@ -1,31 +1,31 @@ import { crypto } from "k6/x/webcrypto"; - export default async function () { - try { - const jwk = { - alg: "A256CBC", - ext: true, - k: "LhR2VJFb1NJ8HORgOn7LNKLXhUqPsTjC65UAWFb4GKI", - key_ops: ["encrypt","decrypt"], - kty: "oct", - }; +export default async function () { + try { + const jwk = { + alg: "A256CBC", + ext: true, + k: "LhR2VJFb1NJ8HORgOn7LNKLXhUqPsTjC65UAWFb4GKI", + key_ops: ["encrypt", "decrypt"], + kty: "oct", + }; - console.log("static key: " + JSON.stringify(jwk)); + console.log("static key: " + JSON.stringify(jwk)); - const importedKey = await crypto.subtle.importKey( - "jwk", - jwk, - { name: "AES-CBC", length: 256}, - true, ["encrypt","decrypt"] - ); + const importedKey = await crypto.subtle.importKey( + "jwk", + jwk, + { name: "AES-CBC", length: 256 }, + true, + ["encrypt", "decrypt"] + ); - console.log("imported: " + JSON.stringify(importedKey)); + console.log("imported: " + JSON.stringify(importedKey)); - const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); + const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); - console.log("exported again: " + JSON.stringify(exportedAgain)); - } catch (err) { - console.log(JSON.stringify(err)); - } - -} \ No newline at end of file + console.log("exported again: " + JSON.stringify(exportedAgain)); + } catch (err) { + console.log(JSON.stringify(err)); + } +} diff --git a/examples/import_export/import-export-jwk-ecdsa.js b/examples/import_export/import-export-jwk-ecdsa.js index 6554657..9052ae8 100644 --- a/examples/import_export/import-export-jwk-ecdsa.js +++ b/examples/import_export/import-export-jwk-ecdsa.js @@ -1,28 +1,27 @@ import { crypto } from "k6/x/webcrypto"; - export default async function () { - const jwk = { - kty: "EC", - crv: "P-521", - x: "AVb0efjfHiCn_8BM5CDD4VSuJRmWvuQvA0uE1Bt0PzTkXzEbgTqc3sjNpZu7vTHUYLMpJSHnwbci5WZ8A9svrnU_", - y: "AVAXNs_iRzlDINjkr8L9ObWpMxBhuB4iQSgrnheJGCK1t54FL0WXtZZD_Tk3nFG9USXE9IvD8CXOPNNpUyhsyzj7", - d: "APQIdYNoupMPMPdq4FT-XNLOf9osn3am1DbPddZsRAv-YzHHwXKhJHgZPIJRSHvJEmP6UCF_hf9jb1nNVG46tIO0", - }; +export default async function () { + const jwk = { + kty: "EC", + crv: "P-521", + x: "AVb0efjfHiCn_8BM5CDD4VSuJRmWvuQvA0uE1Bt0PzTkXzEbgTqc3sjNpZu7vTHUYLMpJSHnwbci5WZ8A9svrnU_", + y: "AVAXNs_iRzlDINjkr8L9ObWpMxBhuB4iQSgrnheJGCK1t54FL0WXtZZD_Tk3nFG9USXE9IvD8CXOPNNpUyhsyzj7", + d: "APQIdYNoupMPMPdq4FT-XNLOf9osn3am1DbPddZsRAv-YzHHwXKhJHgZPIJRSHvJEmP6UCF_hf9jb1nNVG46tIO0", + }; - console.log("static key: " + JSON.stringify(jwk)); + console.log("static key: " + JSON.stringify(jwk)); + const importedKey = await crypto.subtle.importKey( + "jwk", + jwk, + { name: "ECDSA", namedCurve: "P-256" }, + true, + ["sign"] + ); - const importedKey = await crypto.subtle.importKey( - "jwk", - jwk, - { name: "ECDSA", namedCurve: "P-256" }, - true, - ["sign"] - ); + console.log("imported: " + JSON.stringify(importedKey)); - console.log("imported: " + JSON.stringify(importedKey)); + const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); - const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); - - console.log("exported again: " + JSON.stringify(exportedAgain)); -} \ No newline at end of file + console.log("exported again: " + JSON.stringify(exportedAgain)); +} diff --git a/examples/import_export/import-export-jwk-hmac-key.js b/examples/import_export/import-export-jwk-hmac-key.js index d5a5aa8..aaaf0fd 100644 --- a/examples/import_export/import-export-jwk-hmac-key.js +++ b/examples/import_export/import-export-jwk-hmac-key.js @@ -1,39 +1,36 @@ import { crypto } from "k6/x/webcrypto"; - export default async function () { - try { - const generatedKey = await crypto.subtle.generateKey( - { - name: "HMAC", - hash: { name: "SHA-256" }, - }, - true, - [ - "sign", - "verify", - ] - ); - - console.log("generated: " + JSON.stringify(generatedKey)); - - const exportedKey = await crypto.subtle.exportKey("jwk", generatedKey); - - console.log("exported: " + JSON.stringify(exportedKey)); - - const importedKey = await crypto.subtle.importKey( - "jwk", - exportedKey, - { name: "HMAC", hash: { name: "SHA-256" } }, - true, ["sign", "verify"] - ); - - console.log("imported: " + JSON.stringify(importedKey)); - - const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); - - console.log("exported again: " + JSON.stringify(exportedAgain)); - } catch (err) { - console.log(JSON.stringify(err)); - } - -} \ No newline at end of file +export default async function () { + try { + const generatedKey = await crypto.subtle.generateKey( + { + name: "HMAC", + hash: { name: "SHA-256" }, + }, + true, + ["sign", "verify"] + ); + + console.log("generated: " + JSON.stringify(generatedKey)); + + const exportedKey = await crypto.subtle.exportKey("jwk", generatedKey); + + console.log("exported: " + JSON.stringify(exportedKey)); + + const importedKey = await crypto.subtle.importKey( + "jwk", + exportedKey, + { name: "HMAC", hash: { name: "SHA-256" } }, + true, + ["sign", "verify"] + ); + + console.log("imported: " + JSON.stringify(importedKey)); + + const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); + + console.log("exported again: " + JSON.stringify(exportedAgain)); + } catch (err) { + console.log(JSON.stringify(err)); + } +} diff --git a/examples/import_export/import-export-jwk-hmac-static-key.js b/examples/import_export/import-export-jwk-hmac-static-key.js index a9d875c..7334e2a 100644 --- a/examples/import_export/import-export-jwk-hmac-static-key.js +++ b/examples/import_export/import-export-jwk-hmac-static-key.js @@ -1,32 +1,31 @@ import { crypto } from "k6/x/webcrypto"; - export default async function () { - try { - const jwk = { - alg: "HS256", - ext: true, - k: "H6gLp3lw7w27NrPUn00WpcKU-IJojJdNzhL_8F6se2k", - key_ops: ["sign", "verify"], - kty: "oct", - }; - - console.log("static key: " + JSON.stringify(jwk)); - - - const importedKey = await crypto.subtle.importKey( - "jwk", - jwk, - { name: "HMAC", hash: { name: "SHA-256" } }, - true, ["sign", "verify"] - ); - - console.log("imported: " + JSON.stringify(importedKey)); - - const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); - - console.log("exported again: " + JSON.stringify(exportedAgain)); - } catch (err) { - console.log(JSON.stringify(err)); - } - -} \ No newline at end of file +export default async function () { + try { + const jwk = { + alg: "HS256", + ext: true, + k: "H6gLp3lw7w27NrPUn00WpcKU-IJojJdNzhL_8F6se2k", + key_ops: ["sign", "verify"], + kty: "oct", + }; + + console.log("static key: " + JSON.stringify(jwk)); + + const importedKey = await crypto.subtle.importKey( + "jwk", + jwk, + { name: "HMAC", hash: { name: "SHA-256" } }, + true, + ["sign", "verify"] + ); + + console.log("imported: " + JSON.stringify(importedKey)); + + const exportedAgain = await crypto.subtle.exportKey("jwk", importedKey); + + console.log("exported again: " + JSON.stringify(exportedAgain)); + } catch (err) { + console.log(JSON.stringify(err)); + } +} diff --git a/examples/sign_verify/sign-verify-ecdsa.js b/examples/sign_verify/sign-verify-ecdsa.js index f29c108..fd4fce8 100644 --- a/examples/sign_verify/sign-verify-ecdsa.js +++ b/examples/sign_verify/sign-verify-ecdsa.js @@ -13,7 +13,7 @@ export default async function () { const data = string2ArrayBuffer("Hello World"); - const alg = { name: "ECDSA", hash: {name: "SHA-256" } }; + const alg = { name: "ECDSA", hash: { name: "SHA-256" } }; // makes a signature of the encoded data with the provided key const signature = await crypto.subtle.sign(alg, keyPair.privateKey, data); diff --git a/examples/sign_verify/sign-verify-hmac.js b/examples/sign_verify/sign-verify-hmac.js index ec985ab..b94a60e 100644 --- a/examples/sign_verify/sign-verify-hmac.js +++ b/examples/sign_verify/sign-verify-hmac.js @@ -1,40 +1,41 @@ import { crypto } from "k6/x/webcrypto"; - export default async function () { - try { - const generatedKey = await crypto.subtle.generateKey( - { - name: "HMAC", - hash: { name: "SHA-1" }, - }, - true, - [ - "sign", - "verify", - ] - ); +export default async function () { + try { + const generatedKey = await crypto.subtle.generateKey( + { + name: "HMAC", + hash: { name: "SHA-1" }, + }, + true, + ["sign", "verify"] + ); - const encoded = string2ArrayBuffer("Hello World"); - - // Signs the encoded data with the provided key using the HMAC algorithm - // the returned signature can be verified using the verify method. - const signature = await crypto.subtle.sign("HMAC", generatedKey, encoded); + const encoded = string2ArrayBuffer("Hello World"); - // Verifies the signature of the encoded data with the provided key using the HMAC algorithm - const verified = await crypto.subtle.verify("HMAC", generatedKey, signature, encoded); + // Signs the encoded data with the provided key using the HMAC algorithm + // the returned signature can be verified using the verify method. + const signature = await crypto.subtle.sign("HMAC", generatedKey, encoded); - console.log("verified: ", verified) - } catch (err) { - console.log(JSON.stringify(err)); - } + // Verifies the signature of the encoded data with the provided key using the HMAC algorithm + const verified = await crypto.subtle.verify( + "HMAC", + generatedKey, + signature, + encoded + ); + console.log("verified: ", verified); + } catch (err) { + console.log(JSON.stringify(err)); + } } function string2ArrayBuffer(str) { - var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char - var bufView = new Uint16Array(buf); - for (var i = 0, strLen = str.length; i < strLen; i++) { - bufView[i] = str.charCodeAt(i); - } - return buf; + var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + var bufView = new Uint16Array(buf); + for (var i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; }