Skip to main content

AES module

📖 Description​

The AES module contains AES-based encryption and decryption functionality.

Rationale

The functions in this module are primitive and probably widely available in many other open-source packages on npm. So why are they included in the Tripetto Runner library? That's because the Tripetto Runner library doesn't have any dependency on other npm packages. So all the code needed to run a Tripetto form is included and these functions are simply required. Since they are part of the package, they are exported so you can use them too. For example when building custom blocks.

▶️ Functions​


🔧 decrypt​

Decrypt Base64-encoded AES-encrypted data using AES in counter mode.

Signature​

decrypt(
data: string,
password: string,
bits: 128 | 192 | 256,
atob: (input: string) => string
): string

Parameters​

NameTypeOptionalDescription
datastringNoSpecifies the data to decrypt.
passwordstringNoSpecifies the password used to generate the decryption key.
bits128 | 192 | 256NoSpecifies the number of bits for the decryption key.
atob(input: string) => stringNoSpecifies the function used to decode the Base64-encoded input data (for example, atob).

Return value​

Returns the decrypted string data.

Example​

import { AES } from "@tripetto/runner";

// This example uses the standard `atob` function available in browsers (see https://developer.mozilla.org/en-US/docs/Web/API/atob)
const decryptedData = AES.decrypt("cgGot0H7OmKO2Wh741MKBbTnJg==", "secret password", 256, atob);

// Outputs `secret data` to the console
console.log(decryptedData);

🔧 encrypt​

Encrypt string data using AES encryption in counter mode and returns the enrypted data as a string in Base64-encoded ASCII format.

Signature​

encrypt(
data: string,
password: string,
bits: 128 | 192 | 256,
btoa: (input: string) => string
): string

Parameters​

NameTypeOptionalDescription
datastringNoSpecifies the data to encrypt.
passwordstringNoSpecifies the password used to generate the encryption key.
bits128 | 192 | 256NoSpecifies the number of bits for the encryption key.
btoa(input: string) => stringNoSpecifies the function used to generate the Base64-encoded ASCII string with the encrypted data (for example, btoa).

Return value​

Returns the encrypted data as a string in Base64-encoded ASCII format.

Example​

import { AES } from "@tripetto/runner";

// This example uses the standard `btoa` function available in browsers (see https://developer.mozilla.org/en-US/docs/Web/API/btoa)
const encryptedData = AES.encrypt("secret data", "secret password", 256, btoa);

// Outputs `cgGot0H7OmKO2Wh741MKBbTnJg==` to the console
console.log(encryptedData);