Skip to content

Commit

Permalink
docs: unifying of import/export examples
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Apr 18, 2024
1 parent 1602444 commit c2a732e
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 253 deletions.
57 changes: 38 additions & 19 deletions examples/import_export/export-ecdh-keys.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
const generatedKeyPair = await crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
);
crypto.subtle
.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
)
.then(
(generatedKeyPair) => {
crypto.subtle.exportKey("pkcs8", generatedKeyPair.privateKey).then(
(exportedPrivateKey) => {
console.log(
"operation successful, exported private key: " +
printArrayBuffer(exportedPrivateKey)
);
},
(err) => {
console.error("export private key, got unexpected error: " + err);
}
);

const exportedPrivateKey = await crypto.subtle.exportKey(
"pkcs8",
generatedKeyPair.privateKey
);
console.log("exported private key: " + printArrayBuffer(exportedPrivateKey));

const exportedPublicKey = await crypto.subtle.exportKey(
"raw",
generatedKeyPair.publicKey
);
console.log("exported public key: " + printArrayBuffer(exportedPublicKey));
crypto.subtle.exportKey("raw", generatedKeyPair.publicKey).then(
(exportedPublicKey) => {
console.log(
"operation successful, exported public key: " +
printArrayBuffer(exportedPublicKey)
);
},
(err) => {
console.error("export public key, got unexpected error: " + err);
}
);
},
(err) => {
console.error("key generation, got unexpected error: " + err);
}
);
}

const printArrayBuffer = (buffer) => {
Expand Down
57 changes: 38 additions & 19 deletions examples/import_export/export-ecdsa-keys.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
const generatedKeyPair = await crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-256",
},
true,
["sign", "verify"]
);
crypto.subtle
.generateKey(
{
name: "ECDSA",
namedCurve: "P-256",
},
true,
["sign", "verify"]
)
.then(
(generatedKeyPair) => {
crypto.subtle.exportKey("pkcs8", generatedKeyPair.privateKey).then(
(exportedPrivateKey) => {
console.log(
"operation successful, exported private key: " +
printArrayBuffer(exportedPrivateKey)
);
},
(err) => {
console.error("export private key, got unexpected error: " + err);
}
);

const exportedPrivateKey = await crypto.subtle.exportKey(
"pkcs8",
generatedKeyPair.privateKey
);
console.log("exported private key: " + printArrayBuffer(exportedPrivateKey));

const exportedPublicKey = await crypto.subtle.exportKey(
"raw",
generatedKeyPair.publicKey
);
console.log("exported public key: " + printArrayBuffer(exportedPublicKey));
crypto.subtle.exportKey("raw", generatedKeyPair.publicKey).then(
(exportedPublicKey) => {
console.log(
"operation successful, exported public key: " +
printArrayBuffer(exportedPublicKey)
);
},
(err) => {
console.error("export public key, got unexpected error: " + err);
}
);
},
(err) => {
console.error("got unexpected error: " + err);
}
);
}

const printArrayBuffer = (buffer) => {
Expand Down
112 changes: 70 additions & 42 deletions examples/import_export/import-ecdh-key.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
const aliceKeyPair = await importKeys(
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,
Expand All @@ -18,46 +18,74 @@ export default async function () {
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,
])
);
).then(
(aliceKeyPair) => {
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,
])
).then(
(bobKeyPair) => {
// Derive shared secret for Alice
deriveSharedSecret(
aliceKeyPair.privateKey,
bobKeyPair.publicKey
).then(
(aliceSharedSecret) => {
console.log(
"operation successful, alice shared secret: " +
printArrayBuffer(aliceSharedSecret)
);
},
(err) => {
console.error(
"deriving alice shared secret, got unexpected error: " + err
);
}
);

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

console.log("alice: ", JSON.stringify(aliceKeyPair));
console.log("bob: ", JSON.stringify(bobKeyPair));

// 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
deriveSharedSecret(
bobKeyPair.privateKey,
aliceKeyPair.publicKey
).then(
(bobSharedSecret) => {
console.log(
"operation successful, bob shared secret: " +
printArrayBuffer(bobSharedSecret)
);
},
(err) => {
console.error(
"deriving bob shared secret, got unexpected error: " + err
);
}
);
},
(err) => {
console.error("importing bob keys, got unexpected error: " + err);
}
);
},
(err) => {
console.error("importing alice keys, got unexpected error: " + err);
}
);

console.log("alice shared secret: " + printArrayBuffer(aliceSharedSecret));
console.log("bob shared secret: " + printArrayBuffer(bobSharedSecret));
}

const importKeys = async (publicKeyData, privateKeyData) => {
Expand All @@ -84,10 +112,10 @@ async function deriveSharedSecret(privateKey, publicKey) {
return crypto.subtle.deriveBits(
{
name: "ECDH",
public: publicKey, // An ECDH public key from the other party
public: publicKey,
},
privateKey, // Your ECDH private key
256 // the number of bits to derive
privateKey,
256
);
}

Expand Down
13 changes: 10 additions & 3 deletions examples/import_export/import-ecdsa-keys.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
const aliceKeyPair = await importKeys(
importKeys(
new Uint8Array([
4, 106, 149, 34, 76, 184, 103, 101, 35, 234, 57, 76, 231, 21, 188, 244,
15, 179, 101, 113, 24, 6, 17, 21, 195, 60, 181, 73, 154, 170, 206, 21,
Expand All @@ -20,9 +20,16 @@ export default async function () {
233, 76, 31, 135, 131, 215, 123, 149, 171, 153, 231, 152, 197, 87, 176,
32, 39, 137,
])
).then(
(keyPair) => {
console.log(
"operation successful, imported keys" + JSON.stringify(keyPair)
);
},
(err) => {
console.error("importing keys, got unexpected error: " + err);
}
);

console.log("alice: ", JSON.stringify(aliceKeyPair));
}

const importKeys = async (publicKeyData, privateKeyData) => {
Expand Down
61 changes: 41 additions & 20 deletions examples/import_export/import-export-aes-key.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
import { crypto } from "k6/x/webcrypto";

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 importedKey = await crypto.subtle.importKey(
"raw",
exportedKey,
"AES-CBC",
true,
["encrypt", "decrypt"]
);

console.log(JSON.stringify(importedKey));
crypto.subtle
.generateKey(
{
name: "AES-CBC",
length: "256",
},
true,
["encrypt", "decrypt"]
)
.then(
(key) => {
// export key
crypto.subtle.exportKey("raw", key).then(
(exportedKey) => {
// try to import the key again
crypto.subtle
.importKey("raw", exportedKey, "AES-CBC", true, [
"encrypt",
"decrypt",
])
.then(
(importedKey) => {
console.log(
"operation successful, imported: " +
JSON.stringify(importedKey)
);
},
(err) => {
console.error("key import, got unexpected error: " + err);
}
);
},
(err) => {
console.error("key export, got unexpected error: " + err);
}
);
},
(err) => {
console.error("key generation, got unexpected error: " + err);
}
);
}
Loading

0 comments on commit c2a732e

Please sign in to comment.