-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'remove/events' into fix/mem-leak
- Loading branch information
Showing
251 changed files
with
10,772 additions
and
4,294 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
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 |
---|---|---|
|
@@ -34,3 +34,6 @@ script.js | |
|
||
/packaging/.env | ||
/packaging/*.gpg | ||
|
||
# web platform tests | ||
js/modules/k6/webcrypto/tests/wpt/ |
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
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
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,11 @@ | ||
# k6 webcrypto examples | ||
|
||
In this directory, you will find examples of how to use the k6's `webcrypto` module in your k6 scripts. | ||
|
||
> [!IMPORTANT] | ||
> We do run the tests based on these examples, that's why we have a simple convention for each example: | ||
> | ||
> * It should do any `console.log`. Since we try to detect that output (log) contain `INFO` keyword. | ||
> * It should NOT `try/catch` exceptions. Since we try to detect if keywords like `"Uncaught"` and `"ERRO"` should not appear in the output (logs). | ||
See [`../../js/modules/k6/webcrypto/cmd_run_test.go`](../../js/modules/k6/webcrypto/cmd_run_test.go) for more details. |
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,54 @@ | ||
import { crypto } from "k6/experimental/webcrypto"; | ||
|
||
export default async function () { | ||
// Generate a key pair for Alice | ||
const aliceKeyPair = await crypto.subtle.generateKey( | ||
{ | ||
name: "ECDH", | ||
namedCurve: "P-256", | ||
}, | ||
true, | ||
["deriveKey", "deriveBits"] | ||
); | ||
|
||
// Generate a key pair for Bob | ||
const bobKeyPair = await crypto.subtle.generateKey( | ||
{ | ||
name: "ECDH", | ||
namedCurve: "P-256", | ||
}, | ||
true, | ||
["deriveKey", "deriveBits"] | ||
); | ||
|
||
// 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 | ||
); | ||
|
||
console.log("alice shared secret: " + printArrayBuffer(aliceSharedSecret)); | ||
console.log("bob shared secret: " + printArrayBuffer(bobSharedSecret)); | ||
} | ||
|
||
async function deriveSharedSecret(privateKey, publicKey) { | ||
return crypto.subtle.deriveBits( | ||
{ | ||
name: "ECDH", | ||
public: publicKey, // An ECDH public key from the other party | ||
}, | ||
privateKey, // Your ECDH private key | ||
256 // the number of bits to derive | ||
); | ||
} | ||
|
||
const printArrayBuffer = (buffer) => { | ||
let view = new Uint8Array(buffer); | ||
return Array.from(view); | ||
}; |
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,22 @@ | ||
import { crypto } from "k6/experimental/webcrypto"; | ||
|
||
export default function () { | ||
crypto.subtle.digest("SHA-256", stringToArrayBuffer("Hello, world!")).then( | ||
(hash) => { | ||
console.log(arrayBufferToHex(hash)); | ||
}, | ||
(err) => { | ||
throw err; | ||
} | ||
); | ||
} | ||
|
||
function arrayBufferToHex(buffer) { | ||
return [...new Uint8Array(buffer)] | ||
.map((x) => x.toString(16).padStart(2, "0")) | ||
.join(""); | ||
} | ||
|
||
function stringToArrayBuffer(s) { | ||
return Uint8Array.from(new String(s), (x) => x.charCodeAt(0)); | ||
} |
53 changes: 53 additions & 0 deletions
53
examples/webcrypto/encrypt_decrypt/encrypt-decrypt-aes-cbc.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,53 @@ | ||
import { crypto } from "k6/experimental/webcrypto"; | ||
|
||
export default async function () { | ||
const key = await crypto.subtle.generateKey( | ||
{ | ||
name: "AES-CBC", | ||
length: 256, | ||
}, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
const encoded = stringToArrayBuffer("Hello, World!"); | ||
const iv = crypto.getRandomValues(new Uint8Array(16)); | ||
|
||
const ciphertext = await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-CBC", | ||
iv: iv, | ||
}, | ||
key, | ||
encoded | ||
); | ||
|
||
const plaintext = await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-CBC", | ||
iv: iv, | ||
}, | ||
key, | ||
ciphertext | ||
); | ||
|
||
console.log( | ||
"deciphered text == original text: ", | ||
arrayBufferToHex(plaintext) === arrayBufferToHex(encoded) | ||
); | ||
} | ||
|
||
function arrayBufferToHex(buffer) { | ||
return [...new Uint8Array(buffer)] | ||
.map((x) => x.toString(16).padStart(2, "0")) | ||
.join(""); | ||
} | ||
|
||
function stringToArrayBuffer(str) { | ||
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char | ||
var bufView = new Uint16Array(buf); | ||
for (var i = 0, strLen = str.length; i < strLen; i++) { | ||
bufView[i] = str.charCodeAt(i); | ||
} | ||
return buf; | ||
} |
55 changes: 55 additions & 0 deletions
55
examples/webcrypto/encrypt_decrypt/encrypt-decrypt-aes-ctr.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,55 @@ | ||
import { crypto } from "k6/experimental/webcrypto"; | ||
|
||
export default async function () { | ||
const key = await crypto.subtle.generateKey( | ||
{ | ||
name: "AES-CTR", | ||
length: 256, | ||
}, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
const encoded = string2ArrayBuffer("Hello World"); | ||
const counter = crypto.getRandomValues(new Uint8Array(16)); | ||
|
||
const ciphertext = await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-CTR", | ||
counter, | ||
length: 64, | ||
}, | ||
key, | ||
encoded | ||
); | ||
|
||
const plaintext = await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-CTR", | ||
counter, | ||
length: 64, | ||
}, | ||
key, | ||
ciphertext | ||
); | ||
|
||
console.log( | ||
"deciphered text == original text: ", | ||
arrayBufferToHex(plaintext) === arrayBufferToHex(encoded) | ||
); | ||
} | ||
|
||
function arrayBufferToHex(buffer) { | ||
return [...new Uint8Array(buffer)] | ||
.map((x) => x.toString(16).padStart(2, "0")) | ||
.join(""); | ||
} | ||
|
||
function string2ArrayBuffer(str) { | ||
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char | ||
var bufView = new Uint16Array(buf); | ||
for (var i = 0, strLen = str.length; i < strLen; i++) { | ||
bufView[i] = str.charCodeAt(i); | ||
} | ||
return buf; | ||
} |
53 changes: 53 additions & 0 deletions
53
examples/webcrypto/encrypt_decrypt/encrypt-decrypt-aes-gcm.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,53 @@ | ||
import { crypto } from "k6/experimental/webcrypto"; | ||
|
||
export default async function () { | ||
const key = await crypto.subtle.generateKey( | ||
{ | ||
name: "AES-GCM", | ||
length: 256, | ||
}, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
const encoded = string2ArrayBuffer("Hello World"); | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
|
||
const ciphertext = await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv: iv, | ||
}, | ||
key, | ||
encoded | ||
); | ||
|
||
const plaintext = await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv: iv, | ||
}, | ||
key, | ||
ciphertext | ||
); | ||
|
||
console.log( | ||
"deciphered text == original text: ", | ||
arrayBufferToHex(plaintext) === arrayBufferToHex(encoded) | ||
); | ||
} | ||
|
||
function arrayBufferToHex(buffer) { | ||
return [...new Uint8Array(buffer)] | ||
.map((x) => x.toString(16).padStart(2, "0")) | ||
.join(""); | ||
} | ||
|
||
function string2ArrayBuffer(str) { | ||
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char | ||
var bufView = new Uint16Array(buf); | ||
for (var i = 0, strLen = str.length; i < strLen; i++) { | ||
bufView[i] = str.charCodeAt(i); | ||
} | ||
return buf; | ||
} |
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,59 @@ | ||
import { crypto } from "k6/experimental/webcrypto"; | ||
|
||
export default async function () { | ||
const keyPair = await crypto.subtle.generateKey( | ||
{ | ||
name: "RSA-OAEP", | ||
modulusLength: 2048, | ||
publicExponent: new Uint8Array([1, 0, 1]), | ||
hash: { name: "SHA-1" }, | ||
}, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
const encoded = stringToArrayBuffer("Hello, World!"); | ||
|
||
const cipherText = await crypto.subtle.encrypt( | ||
{ | ||
name: "RSA-OAEP", | ||
}, | ||
keyPair.publicKey, | ||
encoded | ||
); | ||
|
||
// ciphertext.byteLength * 8, vector.privateKey.algorithm.modulusLength | ||
console.log("cipherText's byteLength: ", cipherText.byteLength * 8); | ||
console.log( | ||
"algorithm's modulusLength: ", | ||
keyPair.privateKey.algorithm.modulusLength | ||
); | ||
|
||
const plaintext = await crypto.subtle.decrypt( | ||
{ | ||
name: "RSA-OAEP", | ||
}, | ||
keyPair.privateKey, | ||
cipherText | ||
); | ||
|
||
console.log( | ||
"deciphered text == original text: ", | ||
arrayBufferToHex(plaintext) === arrayBufferToHex(encoded) | ||
); | ||
} | ||
|
||
function arrayBufferToHex(buffer) { | ||
return [...new Uint8Array(buffer)] | ||
.map((x) => x.toString(16).padStart(2, "0")) | ||
.join(""); | ||
} | ||
|
||
function stringToArrayBuffer(str) { | ||
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char | ||
var bufView = new Uint16Array(buf); | ||
for (var i = 0, strLen = str.length; i < strLen; i++) { | ||
bufView[i] = str.charCodeAt(i); | ||
} | ||
return buf; | ||
} |
Oops, something went wrong.