Give us feedback
encryptdecryptjs
Reactive icon

EncryptDecryptJS

Stable version 1.0.0 (Compatible with OutSystems 11)
Uploaded on 23 Jan by 
5.0
 (4 ratings)
encryptdecryptjs

EncryptDecryptJS

Documentation
1.0.0

Encryption Functions

Utility Functions

str2ab(str)

Converts a string to an ArrayBuffer.

Parameters:

  • str (String): The string to be converted.

Returns:

  • ArrayBuffer: The binary buffer equivalent of the input string.

ab2str(buf)

Converts an ArrayBuffer to a string.

Parameters:

  • buf (ArrayBuffer): The buffer to be converted.

Returns:

  • String: The string representation of the input buffer.

Encryption Function

encryptData(data, passphrase)

Asynchronously encrypts data using AES-GCM with a derived key from the provided passphrase.

Parameters:

  • data (String): The plaintext data to be encrypted.

  • passphrase (String): The passphrase used for key derivation.

Returns:

  • Promise<String>: A promise that resolves to the base64 encoded encrypted data.

Example Usage Function

handleEncryption(value, passphrase)

Manages the encryption process, sets the output parameters, and signals the completion of the asynchronous operation.

Parameters:

  • value (String): The plaintext value to be encrypted.

  • passphrase (String): The passphrase to be used for encryption.

OutSystems Parameters:

  • $parameters.OutEncrpyptedValue (String): The output parameter to store the encrypted value.

  • $parameters.IsSuccess (Boolean): The output parameter to indicate the success of the encryption operation.

  • $parameters.OutError (String): The output parameter to store any error message if encryption fails.

OutSystems Functions:

  • $resolve(): Signals the completion of the asynchronous operation to the OutSystems platform.

Usage

To use this module in your OutSystems application, follow these steps:

  1. Define the JavaScript functions in a JavaScript code node within your OutSystems flow.

  2. Set the input parameters ($parameters.Value and $parameters.passphrase) with the data you wish to encrypt and the passphrase.

  3. Call the handleEncryption function to perform the encryption.

  4. The handleEncryption function will set the output parameters and use $resolve() to signal the completion of the encryption process.

Notes

  • The encryption function uses the Web Crypto API, which is only available in secure contexts (HTTPS).

  • The encryptData function generates a random initialization vector (IV) for each encryption operation and prepends it to the encrypted data before encoding.

  • The module is designed to handle errors gracefully and will set the $parameters.IsSuccess flag accordingly.

  • Ensure that the $resolve function is defined in your OutSystems JavaScript node properties to properly signal the completion of the asynchronous operation.

Example Call

javascript

const passphrase = $parameters.passphrase;

handleEncryption($parameters.Value, passphrase);


This module provides a robust encryption mechanism for OutSystems applications, ensuring data is securely encrypted using modern cryptographic standards.


Decryption Functions

Utility Functions

str2ab(str)

Converts a string to an ArrayBuffer, facilitating binary data manipulation.

Parameters:

  • str (String): The string to convert.

Returns:

  • ArrayBuffer: The binary representation of the string.

ab2str(buf)

Converts an ArrayBuffer back to a string, typically used after decryption.

Parameters:

  • buf (ArrayBuffer): The buffer to convert.

Returns:

  • String: The string representation of the ArrayBuffer.

Decryption Function

decryptData(encryptedData, passphrase)

Asynchronously decrypts data using AES-GCM with a key derived from the provided passphrase.

Parameters:

  • encryptedData (String): Base64 encoded string containing the encrypted data.

  • passphrase (String): The passphrase used for key derivation, must match the one used during encryption.

Returns:

  • Promise<String>: A promise that resolves to the decrypted plaintext data.

Example Usage Function

handleDecryption(value, passphrase)

Handles the decryption process, sets the output parameters, and signals the completion of the asynchronous operation within the OutSystems environment.

Parameters:

  • value (String): The base64 encoded encrypted text to decrypt.

  • passphrase (String): The passphrase used for decryption.

OutSystems Parameters:

  • $parameters.DecryptedText (String): The output parameter to store the decrypted text.

  • $parameters.IsSuccess (Boolean): The output parameter to indicate the success of the decryption operation.

  • $parameters.OutError (String): The output parameter to store any error message if decryption fails.

OutSystems Functions:

  • $resolve(): Used to signal the completion of the asynchronous operation to the OutSystems platform.

Usage

To utilize this module in an OutSystems application:

  1. Implement the JavaScript functions within a JavaScript code node in your OutSystems flow.

  2. Assign the input parameters ($parameters.value and $parameters.passphrase) with the encrypted data and the passphrase.

  3. Invoke the handleDecryption function to perform the decryption.

  4. The handleDecryption function will set the output parameters and call $resolve() to indicate the completion of the decryption process.

Notes

  • The Web Crypto API is only accessible in secure contexts (HTTPS).

  • The decryptData function extracts the initialization vector (IV) and the encrypted data from the input, which must be formatted correctly as output by the corresponding encryption function.

  • Error handling is built into the module, setting the $parameters.IsSuccess flag and $parameters.OutError message appropriately.

  • Ensure that the $resolve function is properly defined in the JavaScript node properties in OutSystems to manage the asynchronous execution flow.

Example Call

javascript

const encryptedText = $parameters.value; // The encrypted text to be decrypted

const passphraseForDecryption = $parameters.passphrase; // Must match the passphrase used for encryption

handleDecryption(encryptedText, passphraseForDecryption);


This documentation outlines the integration of a secure decryption mechanism into OutSystems applications, complementing the encryption module to provide full encryption/decryption capabilities using modern cryptographic standards.