Skip to content

Commit

Permalink
docs: format examples them according k6-docs ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Apr 23, 2024
1 parent b51964d commit 4513adf
Show file tree
Hide file tree
Showing 17 changed files with 277 additions and 289 deletions.
9 changes: 6 additions & 3 deletions examples/encrypt_decrypt/encrypt-decrypt-aes-cbc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions examples/encrypt_decrypt/encrypt-decrypt-aes-ctr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions examples/encrypt_decrypt/encrypt-decrypt-aes-gcm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 10 additions & 13 deletions examples/generateKey/generateKey-aes.js
Original file line number Diff line number Diff line change
@@ -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))
}
console.log(JSON.stringify(key));
}
23 changes: 10 additions & 13 deletions examples/generateKey/generateKey-ecdh.js
Original file line number Diff line number Diff line change
@@ -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))
}
console.log(JSON.stringify(key));
}
20 changes: 10 additions & 10 deletions examples/generateKey/generateKey-ecdsa.js
Original file line number Diff line number Diff line change
@@ -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))
}
console.log(JSON.stringify(key));
}
33 changes: 15 additions & 18 deletions examples/generateKey/generateKey-hmac.js
Original file line number Diff line number Diff line change
@@ -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))
}
}
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));
}
}
1 change: 0 additions & 1 deletion examples/import_export/export-ecdsa-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ const printArrayBuffer = (buffer) => {
let view = new Uint8Array(buffer);
return Array.from(view);
};

40 changes: 19 additions & 21 deletions examples/import_export/import-export-aes-key.js
Original file line number Diff line number Diff line change
@@ -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))
}
console.log(JSON.stringify(importedKey));
}
49 changes: 23 additions & 26 deletions examples/import_export/import-export-hmac-key.js
Original file line number Diff line number Diff line change
@@ -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));
}

}
console.log(JSON.stringify(importedKey));
} catch (err) {
console.log(JSON.stringify(err));
}
}
70 changes: 34 additions & 36 deletions examples/import_export/import-export-jwk-aes-key.js
Original file line number Diff line number Diff line change
@@ -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));
}
}
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));
}
}
Loading

0 comments on commit 4513adf

Please sign in to comment.