Join our biggest community event of the year—get a first look at Grafana 12, plus a science fair and sessions on Prometheus, OpenTelemetry, and more. Save 20% with 3+ or 10% when you bring a friend.
The experimental module k6/experimental/webcrypto has graduated, and its functionality is now available globally through the
crypto object. The k6/experimental/webcrypto is deprecated and will be removed in the near future.
To migrate your scripts, remove the k6/experimental/webcrypto imports and use the crypto object instead.
The AesCbcParams object represents the object that should be passed as the algorithm parameter into the
encrypt and
decrypt operation when using the AES-CBC algorithm.
The initialization vector. Must be 16 bytes, unpredictable and cryptographically random. Yet, it doesn’t need to be secret and can be transmitted along with the ciphertext.
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 ciphertext =await crypto.subtle.encrypt({name:'AES-CBC',iv: crypto.getRandomValues(newUint8Array(16)),},
key,
plaintext
);}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;}