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 SubtleCrypto interface provides a set of low-level cryptographic primitives such as encryption, decryption, digital signature generation and verification, and key generation and management. It is useful for using secure and efficient cryptographic operations within k6 scripts.
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;}