While we intend to keep experimental modules as stable as possible, we may need to introduce breaking changes. This could happen at future k6 releases until the module becomes fully stable and graduates as a k6 core module. For more information, refer to the extension graduation process.
Experimental modules maintain a high level of stability and follow regular maintenance and security measures. Feel free to open an issue if you have any feedback or suggestions.
With this experimental module, you can use the WebCrypto API in your k6 scripts. However, note that this API is not yet fully implemented and some algorithms and features might still be missing.
The Web Crypto API is a JavaScript API for performing cryptographic operations such as encryption, decryption, digital signature generation and verification, and key generation and management. It provides a standard interface to access cryptographic functionality, which can help ensure that cryptographic operations are performed correctly and securely.
API
The module exports a top-level crypto object with the following properties and methods:
The SubtleCrypto interface provides access to common cryptographic primitives, such as hashing, signing, encryption, or decryption.
Example
JavaScript
import{ crypto }from'k6/experimental/webcrypto';exportdefaultasyncfunction(){const plaintext =stringToArrayBuffer('Hello, World!');/**
* Generate a symmetric key using the AES-CBC algorithm.
*/const key =await crypto.subtle.generateKey({name:'AES-CBC',length:256,},true,['encrypt','decrypt']);/**
* Encrypt the plaintext using the AES-CBC key with
* have generated.
*/const iv = crypto.getRandomValues(newUint8Array(16));const ciphertext =await crypto.subtle.encrypt({name:'AES-CBC',iv: iv,},
key,
plaintext
);/**
* Decrypt the ciphertext using the same key to verify
* that the resulting plaintext is the same as the original.
*/const deciphered =await crypto.subtle.decrypt({name:'AES-CBC',iv: iv,},
key,
ciphertext
);
console.log('deciphered text == original plaintext: ',arrayBufferToHex(deciphered)===arrayBufferToHex(plaintext));}functionarrayBufferToHex(buffer){return[...newUint8Array(buffer)].map((x)=> x.toString(16).padStart(2,'0')).join('');}functionstringToArrayBuffer(str){const buf =newArrayBuffer(str.length *2);// 2 bytes for each charconst bufView =newUint16Array(buf);for(let i =0, strLen = str.length; i < strLen; i++){
bufView[i]= str.charCodeAt(i);}return buf;}