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 21, 2024
1 parent d9b36bb commit d8ebd3f
Show file tree
Hide file tree
Showing 2 changed files with 73 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));
}
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));
}

}

0 comments on commit d8ebd3f

Please sign in to comment.