Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: remove try everywhere
Browse files Browse the repository at this point in the history
olegbespalov committed Apr 19, 2024

Verified

This commit was signed with the committer’s verified signature.
olegbespalov Oleg Bespalov
1 parent 8c9ad7a commit 5dad531
Showing 11 changed files with 205 additions and 248 deletions.
64 changes: 30 additions & 34 deletions examples/derive_bits/derive-bits-ecdh.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
try {
// Generate a key pair for Alice
const aliceKeyPair = await crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
);
// Generate a key pair for Alice
const aliceKeyPair = await crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
);

// Generate a key pair for Bob
const bobKeyPair = await crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
);
// Generate a key pair for Bob
const bobKeyPair = await crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
);

// Derive shared secret for Alice
const aliceSharedSecret = await deriveSharedSecret(
aliceKeyPair.privateKey,
bobKeyPair.publicKey
);
// Derive shared secret for Alice
const aliceSharedSecret = await deriveSharedSecret(
aliceKeyPair.privateKey,
bobKeyPair.publicKey
);

// Derive shared secret for Bob
const bobSharedSecret = await deriveSharedSecret(
bobKeyPair.privateKey,
aliceKeyPair.publicKey
);
// Derive shared secret for Bob
const bobSharedSecret = await deriveSharedSecret(
bobKeyPair.privateKey,
aliceKeyPair.publicKey
);

console.log("alice shared secret: " + printArrayBuffer(aliceSharedSecret));
console.log("bob shared secret: " + printArrayBuffer(bobSharedSecret));
} catch (err) {
console.log("Error: " + JSON.stringify(err));
}
console.log("alice shared secret: " + printArrayBuffer(aliceSharedSecret));
console.log("bob shared secret: " + printArrayBuffer(bobSharedSecret));
}

async function deriveSharedSecret(privateKey, publicKey) {
25 changes: 11 additions & 14 deletions examples/generateKey/generateKey-hmac.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
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));
}
const key = await crypto.subtle.generateKey(
{
name: "HMAC",
hash: { name: "SHA-512" },
length: 256,
},
true,
["sign", "verify"]
);

console.log(JSON.stringify(key));
}
38 changes: 17 additions & 21 deletions examples/import_export/import-export-hmac-key.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
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"]
);
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));
}
60 changes: 28 additions & 32 deletions examples/import_export/import-export-jwk-aes-key.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
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));
}
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));
}
40 changes: 18 additions & 22 deletions examples/import_export/import-export-jwk-aes-static-key.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
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",
};
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));
}
console.log("exported again: " + JSON.stringify(exportedAgain));
}
60 changes: 28 additions & 32 deletions examples/import_export/import-export-jwk-hmac-key.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
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));
}
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));
}
40 changes: 18 additions & 22 deletions examples/import_export/import-export-jwk-hmac-static-key.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
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",
};
const jwk = {
alg: "HS256",
ext: true,
k: "H6gLp3lw7w27NrPUn00WpcKU-IJojJdNzhL_8F6se2k",
key_ops: ["sign", "verify"],
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: "HMAC", hash: { name: "SHA-256" } },
true,
["sign", "verify"]
);
const importedKey = await crypto.subtle.importKey(
"jwk",
jwk,
{ name: "HMAC", hash: { name: "SHA-256" } },
true,
["sign", "verify"]
);

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));
}
console.log("exported again: " + JSON.stringify(exportedAgain));
}
12 changes: 4 additions & 8 deletions examples/import_export/import-jwk-base64-decrypt.js
Original file line number Diff line number Diff line change
@@ -18,15 +18,11 @@ export default async function () {
k: "9Id_8iG6FkGOWmc1S203vGVnTExtpDGxdQN7v7OV9Uc",
};

try {
const result = await decrypt(jwkKeyData, transmittedData);
const result = await decrypt(jwkKeyData, transmittedData);

// should output decrypted message
// INFO[0000] result: 'my secret message' source=console
console.log("result: '" + result + "'");
} catch (e) {
console.log("Error: " + JSON.stringify(e));
}
// should output decrypted message
// INFO[0000] result: 'my secret message' source=console
console.log("result: '" + result + "'");
}

const decrypt = async (keyData, transmittedData) => {
12 changes: 4 additions & 8 deletions examples/import_export/import-raw-base64-decrypt.js
Original file line number Diff line number Diff line change
@@ -15,15 +15,11 @@ export default async function () {
69, 215, 55, 197, 43, 122, 160, 178, 228, 104, 4, 115, 138, 159, 119, 49,
]);

try {
const result = await decrypt(keyData, transmittedData);
const result = await decrypt(keyData, transmittedData);

// should output decrypted message
// INFO[0000] result: 'my secret message' source=console
console.log("result: '" + result + "'");
} catch (e) {
console.log("Error: " + JSON.stringify(e));
}
// should output decrypted message
// INFO[0000] result: 'my secret message' source=console
console.log("result: '" + result + "'");
}

const decrypt = async (keyData, transmittedData) => {
58 changes: 27 additions & 31 deletions examples/sign_verify/sign-verify-ecdsa.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
try {
const keyPair = await crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-256",
},
true,
["sign", "verify"]
);

const data = string2ArrayBuffer("Hello World");

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);

console.log("signature: ", printArrayBuffer(signature));

//Verifies the signature of the encoded data with the provided key
const verified = await crypto.subtle.verify(
alg,
keyPair.publicKey,
signature,
data
);

console.log("verified: ", verified);
} catch (err) {
console.log("err: " + JSON.stringify(err));
}
const keyPair = await crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-256",
},
true,
["sign", "verify"]
);

const data = string2ArrayBuffer("Hello World");

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);

console.log("signature: ", printArrayBuffer(signature));

//Verifies the signature of the encoded data with the provided key
const verified = await crypto.subtle.verify(
alg,
keyPair.publicKey,
signature,
data
);

console.log("verified: ", verified);
}

const string2ArrayBuffer = (str) => {
44 changes: 20 additions & 24 deletions examples/sign_verify/sign-verify-hmac.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
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"]
);
const generatedKey = await crypto.subtle.generateKey(
{
name: "HMAC",
hash: { name: "SHA-1" },
},
true,
["sign", "verify"]
);

const encoded = string2ArrayBuffer("Hello World");
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);
// 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);

// 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
);
// 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));
}
console.log("verified: ", verified);
}

function string2ArrayBuffer(str) {

0 comments on commit 5dad531

Please sign in to comment.