Skip to content

Commit

Permalink
JWK Import/Export examples
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Feb 23, 2024
1 parent 63e0969 commit a51233f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/import_export/import-export-jwk-aes-key.js
@@ -0,0 +1,34 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
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));
}
31 changes: 31 additions & 0 deletions examples/import_export/import-export-jwk-aes-static-key.js
@@ -0,0 +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",
};

console.log("static key: " + JSON.stringify(jwk));

const importedKey = await crypto.subtle.importKey(
"jwk",
jwk,
{ name: "AES-CBC", length: 256},
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));
}

}
39 changes: 39 additions & 0 deletions examples/import_export/import-export-jwk-hmac-key.js
@@ -0,0 +1,39 @@
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));
}

}
32 changes: 32 additions & 0 deletions examples/import_export/import-export-jwk-hmac-static-key.js
@@ -0,0 +1,32 @@
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));
}

}

0 comments on commit a51233f

Please sign in to comment.