Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f658c5a
commit 3ff1284
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
31
examples/import_export/import-export-jwk-aes-static-key.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
32
examples/import_export/import-export-jwk-hmac-static-key.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |