bc-node-sdk
Version:
BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.
63 lines (62 loc) • 2.41 kB
TypeScript
/**
* Class {@link CipherUtil} encapsulates utility methods for encrypting and decrypting data.
* The encryption algorithm used is AES in CBC mode.
*
* @example
* const encrypted = CipherUtil.aesEncrypt("Hello World!", KEY, IV);
* const decrypted = CipherUtil.aesDecrypt(encrypted, KEY, IV);
* // decrypted is "Hello World!"
*/
export default abstract class CipherUtil {
/**
* XORs two buffers together, returning a new buffer of the same length.
* @param buf1 The first buffer to XOR.
* @param buf2 The second buffer to XOR.
* @returns A new buffer containing the result of the XOR operation.
*/
private static xorBuffers;
/**
* Adds padding to a buffer to make its length a multiple of the block size.
* This is necessary for certain encryption algorithms that require the input
* to be a multiple of the block size.
* @param buffer The buffer to add padding to.
* @param blockSize The block size to pad to.
* @returns A new buffer that is padded to the specified block size.
*/
private static addPadding;
/**
* Removes the PKCS#7 padding from an encrypted buffer.
* @param buffer The encrypted buffer to remove padding from.
* @returns The decrypted plaintext buffer.
*/
private static removePadding;
/**
* Encrypts a plaintext using AES in CBC mode.
* @param plainText The plaintext to encrypt.
* @param key The key to use for encryption.
* @param iv The initialization vector to use for encryption.
* @returns The encrypted ciphertext.
*/
private static aesEncrypt;
/**
* Decrypts a given buffer using AES in CBC mode.
* @param encrypted The buffer to decrypt.
* @param key The key to use for decryption.
* @param iv The initialization vector to use for decryption.
* @returns The decrypted buffer.
*/
private static aesDecrypt;
/**
* Encrypts the given text using AES encryption.
*
* @param text - The text to encrypt.
* @returns The encrypted text as a hexadecimal string.
*/
static encrypt(text: string): string;
/**
* Decrypts the given text using AES in CBC mode.
* @param {string} text The text to decrypt.
* @returns {string} The decrypted text.
*/
static decrypt(text: string): string;
}