UNPKG

bc-node-sdk

Version:

BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.

138 lines (137 loc) 5.87 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../domain/constants"); const app_util_1 = __importDefault(require("./app-util")); const IV_LENGTH = 16; // 16 bytes key const KEY = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); // Buffer.from(AppConfig.CIPHER_ENCRYPTION_KEY!) // 16 bytes IV const IV = Buffer.from(process.env.CIPHER_ENCRYPTION_KEY); //new Uint8Array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]); /** * 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!" */ 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. */ static xorBuffers(buf1, buf2) { const length = Math.min(buf1.length, buf2.length); const result = new Uint8Array(length); for (let i = 0; i < length; i++) { result[i] = buf1[i] ^ buf2[i]; } return result; } /** * 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. */ static addPadding(buffer, blockSize) { const paddingLength = blockSize - (buffer.length % blockSize); const padding = new Uint8Array(paddingLength).fill(paddingLength); const paddedBuffer = new Uint8Array(buffer.length + paddingLength); paddedBuffer.set(buffer); paddedBuffer.set(padding, buffer.length); return paddedBuffer; } /** * Removes the PKCS#7 padding from an encrypted buffer. * @param buffer The encrypted buffer to remove padding from. * @returns The decrypted plaintext buffer. */ static removePadding(buffer) { const paddingLength = buffer[buffer.length - 1]; return buffer.slice(0, -paddingLength); } /** * 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. */ static aesEncrypt(plainText, key, iv) { const blockSize = IV_LENGTH; // block size in bytes const paddedPlaintext = CipherUtil.addPadding(plainText, blockSize); let encrypted = new Uint8Array(paddedPlaintext.length); let previousBlock = iv; for (let i = 0; i < paddedPlaintext.length; i += blockSize) { const block = paddedPlaintext.slice(i, i + blockSize); const xorBlock = CipherUtil.xorBuffers(block, previousBlock); encrypted.set(xorBlock, i); previousBlock = xorBlock; // In a real AES, this would be the encrypted block } return encrypted; } /** * 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. */ static aesDecrypt(encrypted, key, iv) { const blockSize = IV_LENGTH; // block size in bytes let decrypted = new Uint8Array(encrypted.length); let previousBlock = iv; for (let i = 0; i < encrypted.length; i += blockSize) { const encryptedBlock = encrypted.slice(i, i + blockSize); const decryptedBlock = CipherUtil.xorBuffers(encryptedBlock, previousBlock); decrypted.set(decryptedBlock, i); previousBlock = encryptedBlock; // In a real AES, this would be the encrypted block } return CipherUtil.removePadding(decrypted); } /** * Encrypts the given text using AES encryption. * * @param text - The text to encrypt. * @returns The encrypted text as a hexadecimal string. */ static encrypt(text) { try { const plainText = new TextEncoder().encode(text); const encrypted = CipherUtil.aesEncrypt(plainText, KEY, IV); const encryptedText = Buffer.from(encrypted).toString('hex'); return encryptedText; } catch (error) { app_util_1.default.logError(error); return constants_1.Defaults.String.Value; } } /** * Decrypts the given text using AES in CBC mode. * @param {string} text The text to decrypt. * @returns {string} The decrypted text. */ static decrypt(text) { try { const encrypted = Uint8Array.from(Buffer.from(text, 'hex')); const decrypted = CipherUtil.aesDecrypt(encrypted, KEY, IV); const decryptedText = Buffer.from(decrypted).toString(); return decryptedText; } catch (error) { app_util_1.default.logError(error); return constants_1.Defaults.String.Value; } } } exports.default = CipherUtil;