diff --git a/examples/import_export/import-export-jwk-aes-key.js b/examples/import_export/import-export-jwk-aes-key.js new file mode 100644 index 0000000..a0f8d3c --- /dev/null +++ b/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)); +} \ No newline at end of file diff --git a/examples/import_export/import-export-jwk-aes-static-key.js b/examples/import_export/import-export-jwk-aes-static-key.js new file mode 100644 index 0000000..8a87c01 --- /dev/null +++ b/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)); + } + +} \ No newline at end of file diff --git a/examples/import_export/import-export-jwk-hmac-key.js b/examples/import_export/import-export-jwk-hmac-key.js new file mode 100644 index 0000000..d5a5aa8 --- /dev/null +++ b/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)); + } + +} \ No newline at end of file diff --git a/examples/import_export/import-export-jwk-hmac-static-key.js b/examples/import_export/import-export-jwk-hmac-static-key.js new file mode 100644 index 0000000..a9d875c --- /dev/null +++ b/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)); + } + +} \ No newline at end of file