|
| 1 | +--- |
| 2 | +title: 'crypto' |
| 3 | +description: 'k6 WebCrypto API implementation' |
| 4 | +weight: 14 |
| 5 | +--- |
| 6 | + |
| 7 | +# crypto |
| 8 | + |
| 9 | +With this module, you can use the [WebCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in your k6 scripts. However, note that this API is not yet fully implemented and some algorithms and features might still be missing. |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +## API |
| 14 | + |
| 15 | +The module is a top-level `crypto` object with the following properties and methods: |
| 16 | + |
| 17 | +| Interface/Function | Description | |
| 18 | +| :------------------------------------------------------------------------------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 19 | +| [getRandomValues](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/crypto/getrandomvalues) | Fills the passed `TypedArray` with cryptographically sound random values. | |
| 20 | +| [randomUUID](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/crypto/randomuuid) | Returns a randomly generated, 36 character long v4 UUID. | |
| 21 | +| [subtle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/crypto/subtlecrypto) | The [SubtleCrypto](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/crypto/subtlecrypto) interface provides access to common cryptographic primitives, such as hashing, signing, encryption, or decryption. | |
| 22 | + |
| 23 | +## Example |
| 24 | + |
| 25 | +{{< code >}} |
| 26 | + |
| 27 | +```javascript |
| 28 | +export default async function () { |
| 29 | + const plaintext = stringToArrayBuffer('Hello, World!'); |
| 30 | + |
| 31 | + /** |
| 32 | + * Generate a symmetric key using the AES-CBC algorithm. |
| 33 | + */ |
| 34 | + const key = await crypto.subtle.generateKey( |
| 35 | + { |
| 36 | + name: 'AES-CBC', |
| 37 | + length: 256, |
| 38 | + }, |
| 39 | + true, |
| 40 | + ['encrypt', 'decrypt'] |
| 41 | + ); |
| 42 | + |
| 43 | + /** |
| 44 | + * Encrypt the plaintext using the AES-CBC key with |
| 45 | + * have generated. |
| 46 | + */ |
| 47 | + const iv = crypto.getRandomValues(new Uint8Array(16)); |
| 48 | + const ciphertext = await crypto.subtle.encrypt( |
| 49 | + { |
| 50 | + name: 'AES-CBC', |
| 51 | + iv: iv, |
| 52 | + }, |
| 53 | + key, |
| 54 | + plaintext |
| 55 | + ); |
| 56 | + |
| 57 | + /** |
| 58 | + * Decrypt the ciphertext using the same key to verify |
| 59 | + * that the resulting plaintext is the same as the original. |
| 60 | + */ |
| 61 | + const deciphered = await crypto.subtle.decrypt( |
| 62 | + { |
| 63 | + name: 'AES-CBC', |
| 64 | + iv: iv, |
| 65 | + }, |
| 66 | + key, |
| 67 | + ciphertext |
| 68 | + ); |
| 69 | + |
| 70 | + console.log( |
| 71 | + 'deciphered text == original plaintext: ', |
| 72 | + arrayBufferToHex(deciphered) === arrayBufferToHex(plaintext) |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +function arrayBufferToHex(buffer) { |
| 77 | + return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join(''); |
| 78 | +} |
| 79 | + |
| 80 | +function stringToArrayBuffer(str) { |
| 81 | + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char |
| 82 | + const bufView = new Uint16Array(buf); |
| 83 | + for (let i = 0, strLen = str.length; i < strLen; i++) { |
| 84 | + bufView[i] = str.charCodeAt(i); |
| 85 | + } |
| 86 | + return buf; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +{{< /code >}} |
0 commit comments