Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify, format, and run tests against examples #77

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# xk6-webcrypto Examples

In this directory, you will find examples of how to use the `xk6-webcrypto` module in your k6 scripts.

> [!IMPORTANT]
> We do run the tests based on these examples, that's why we have a simple convention for each example:
>
> * It should do any `console.log`. Since we try to detect that output (log) contain `INFO` keyword.
> * It should NOT `try/catch` exceptions. Since we try to detect if keywords like `"Uncaught"` and `"ERRO"` should not appear in the output (logs).

See [`webcrypto/tests/cmd_run_test.go`](../webcrypto/tests/cmd_run_test.go) for more details.
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) {
Expand Down
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));
}
30 changes: 12 additions & 18 deletions examples/generateKey/generateKey-hmac.js
Original file line number Diff line number Diff line change
@@ -1,21 +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));
}
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);
};

76 changes: 39 additions & 37 deletions examples/import_export/import-ecdh-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,11 @@ import { crypto } from "k6/x/webcrypto";

export default async function () {
const aliceKeyPair = await importKeys(
new Uint8Array([
4, 8, 249, 89, 225, 84, 28, 108, 246, 144, 7, 182, 109, 32, 155, 16, 102,
22, 66, 253, 148, 220, 48, 6, 106, 21, 123, 98, 229, 191, 20, 200, 35, 5,
208, 131, 136, 154, 125, 18, 20, 202, 231, 168, 184, 127, 53, 186, 6, 136,
114, 101, 127, 109, 179, 44, 96, 108, 193, 126, 217, 131, 163, 131, 135,
]),
new Uint8Array([
48, 129, 135, 2, 1, 0, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42,
134, 72, 206, 61, 3, 1, 7, 4, 109, 48, 107, 2, 1, 1, 4, 32, 194, 150, 86,
186, 233, 47, 132, 192, 213, 56, 60, 179, 112, 7, 89, 65, 116, 88, 8, 158,
228, 172, 190, 234, 143, 152, 33, 175, 47, 0, 39, 79, 161, 68, 3, 66, 0,
4, 8, 249, 89, 225, 84, 28, 108, 246, 144, 7, 182, 109, 32, 155, 16, 102,
22, 66, 253, 148, 220, 48, 6, 106, 21, 123, 98, 229, 191, 20, 200, 35, 5,
208, 131, 136, 154, 125, 18, 20, 202, 231, 168, 184, 127, 53, 186, 6, 136,
114, 101, 127, 109, 179, 44, 96, 108, 193, 126, 217, 131, 163, 131, 135,
])
alicePublicKeyData,
alicePrivateKeyData
);

const bobKeyPair = await importKeys(
new Uint8Array([
4, 218, 134, 37, 137, 90, 68, 101, 112, 234, 68, 87, 110, 182, 85, 178,
161, 106, 223, 50, 150, 9, 155, 68, 191, 51, 138, 185, 186, 226, 211, 25,
203, 96, 193, 213, 68, 7, 181, 238, 52, 154, 113, 56, 76, 86, 44, 245,
128, 194, 103, 14, 81, 229, 124, 189, 13, 252, 138, 98, 196, 218, 39, 34,
42,
]),
new Uint8Array([
48, 129, 135, 2, 1, 0, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42,
134, 72, 206, 61, 3, 1, 7, 4, 109, 48, 107, 2, 1, 1, 4, 32, 59, 168, 213,
160, 115, 123, 19, 203, 62, 86, 50, 152, 17, 210, 42, 35, 174, 230, 191,
11, 65, 239, 223, 130, 73, 53, 161, 46, 9, 210, 50, 4, 161, 68, 3, 66, 0,
4, 218, 134, 37, 137, 90, 68, 101, 112, 234, 68, 87, 110, 182, 85, 178,
161, 106, 223, 50, 150, 9, 155, 68, 191, 51, 138, 185, 186, 226, 211, 25,
203, 96, 193, 213, 68, 7, 181, 238, 52, 154, 113, 56, 76, 86, 44, 245,
128, 194, 103, 14, 81, 229, 124, 189, 13, 252, 138, 98, 196, 218, 39, 34,
42,
])
);
const bobKeyPair = await importKeys(bobPublicKeyData, bobPrivateKeyData);

console.log("alice: ", JSON.stringify(aliceKeyPair));
console.log("bob: ", JSON.stringify(bobKeyPair));
Expand Down Expand Up @@ -79,7 +46,6 @@ const importKeys = async (publicKeyData, privateKeyData) => {

return { publicKey: publicKey, privateKey: privateKey };
};

async function deriveSharedSecret(privateKey, publicKey) {
return crypto.subtle.deriveBits(
{
Expand All @@ -95,3 +61,39 @@ const printArrayBuffer = (buffer) => {
let view = new Uint8Array(buffer);
return Array.from(view);
};

const alicePublicKeyData = new Uint8Array([
4, 8, 249, 89, 225, 84, 28, 108, 246, 144, 7, 182, 109, 32, 155, 16, 102, 22,
66, 253, 148, 220, 48, 6, 106, 21, 123, 98, 229, 191, 20, 200, 35, 5, 208,
131, 136, 154, 125, 18, 20, 202, 231, 168, 184, 127, 53, 186, 6, 136, 114,
101, 127, 109, 179, 44, 96, 108, 193, 126, 217, 131, 163, 131, 135,
]);

const alicePrivateKeyData = new Uint8Array([
48, 129, 135, 2, 1, 0, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42,
134, 72, 206, 61, 3, 1, 7, 4, 109, 48, 107, 2, 1, 1, 4, 32, 194, 150, 86, 186,
233, 47, 132, 192, 213, 56, 60, 179, 112, 7, 89, 65, 116, 88, 8, 158, 228,
172, 190, 234, 143, 152, 33, 175, 47, 0, 39, 79, 161, 68, 3, 66, 0, 4, 8, 249,
89, 225, 84, 28, 108, 246, 144, 7, 182, 109, 32, 155, 16, 102, 22, 66, 253,
148, 220, 48, 6, 106, 21, 123, 98, 229, 191, 20, 200, 35, 5, 208, 131, 136,
154, 125, 18, 20, 202, 231, 168, 184, 127, 53, 186, 6, 136, 114, 101, 127,
109, 179, 44, 96, 108, 193, 126, 217, 131, 163, 131, 135,
]);

const bobPublicKeyData = new Uint8Array([
4, 218, 134, 37, 137, 90, 68, 101, 112, 234, 68, 87, 110, 182, 85, 178, 161,
106, 223, 50, 150, 9, 155, 68, 191, 51, 138, 185, 186, 226, 211, 25, 203, 96,
193, 213, 68, 7, 181, 238, 52, 154, 113, 56, 76, 86, 44, 245, 128, 194, 103,
14, 81, 229, 124, 189, 13, 252, 138, 98, 196, 218, 39, 34, 42,
]);

const bobPrivateKeyData = new Uint8Array([
48, 129, 135, 2, 1, 0, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42,
134, 72, 206, 61, 3, 1, 7, 4, 109, 48, 107, 2, 1, 1, 4, 32, 59, 168, 213, 160,
115, 123, 19, 203, 62, 86, 50, 152, 17, 210, 42, 35, 174, 230, 191, 11, 65,
239, 223, 130, 73, 53, 161, 46, 9, 210, 50, 4, 161, 68, 3, 66, 0, 4, 218, 134,
37, 137, 90, 68, 101, 112, 234, 68, 87, 110, 182, 85, 178, 161, 106, 223, 50,
150, 9, 155, 68, 191, 51, 138, 185, 186, 226, 211, 25, 203, 96, 193, 213, 68,
7, 181, 238, 52, 154, 113, 56, 76, 86, 44, 245, 128, 194, 103, 14, 81, 229,
124, 189, 13, 252, 138, 98, 196, 218, 39, 34, 42,
]);
Loading
Loading