UNPKG

ggez-banking-sdk

Version:

A Node.js package to handle GGEZ Banking API endpoints, Simplify the process of managing CRUD operations with this efficient and easy-to-use package.

188 lines (187 loc) 6.35 kB
import { createCipheriv, createDecipheriv } from "crypto"; class CipherHelper { // #region "Properties" errorHandler; // #endregion // #region "Constructor" constructor(errorHandler) { this.errorHandler = errorHandler; } // #endregion // #region "Encrypt" encrypt = (plainText, key, iv, urlSafe = false) => { try { const encoding = urlSafe ? "base64url" : "base64"; const keyBuffer = Buffer.from(key, "utf8"); const ivBuffer = Buffer.from(iv, "utf8"); const cipher = createCipheriv("aes-256-cbc", keyBuffer, ivBuffer); let encrypted = cipher.update(plainText, "utf8", encoding); encrypted += cipher.final(encoding); return encrypted; } catch (error) { this.errorHandler(error); return ""; } }; encryptAsJson = (plainText, cookieKey, key, iv) => { try { const json = JSON.stringify({ [cookieKey]: plainText }); const encrypted = this.encrypt(json, key, iv); return encrypted; } catch (error) { this.errorHandler(error); return ""; } }; encryptByProgramID = (plainText, programId) => { try { const { key, iv } = this.generateByProgramID(programId); return this.encrypt(plainText, key, iv, true); } catch (error) { this.errorHandler(error); return ""; } }; // #endregion // #region "Decrypt" decrypt = (cipherText, key, iv, urlSafe = false) => { try { const encoding = urlSafe ? "base64url" : "base64"; const keyBuffer = Buffer.from(key, "utf8"); const ivBuffer = Buffer.from(iv, "utf8"); const decipher = createDecipheriv("aes-256-cbc", keyBuffer, ivBuffer); let decrypted = decipher.update(cipherText, encoding, "utf8"); decrypted += decipher.final("utf8"); return decrypted; } catch (error) { this.errorHandler(error); return ""; } }; decryptAsJson = (cipherText, key, iv) => { try { const decryptedJSON = this.decrypt(cipherText, key, iv); if (decryptedJSON == "{}") { return { [key]: "" }; } const decrypted = JSON.parse(decryptedJSON); if (!decrypted) { return { [key]: "" }; } return decrypted; } catch (error) { this.errorHandler(error); return { [key]: "" }; } }; decryptByProgramID = (cipherText, programId) => { try { const { key, iv } = this.generateByProgramID(programId); return this.decrypt(cipherText, key, iv, true); } catch (error) { this.errorHandler(error); return ""; } }; // #endregion // #region "Generate" generate = (code) => { try { if (!code) throw new Error("code is not defined"); const KEY = this.PaddingLeft(code.toString(), 32, "0"); const IV = this.PaddingLeft(code.toString(), 16, "0"); return { key: KEY, iv: IV }; } catch (error) { this.errorHandler(error); return { key: "", iv: "" }; } }; generateByUSRAndIID = (USR, IID) => { try { if (!USR || !USR.device_id || !USR.user_id) { throw new Error("USR is not defined"); } if (!IID) { throw new Error("IID is not defined"); } const DID_KEY = this.PaddingLeft(USR.device_id.toString(), 10, "0"); const UID_KEY = this.PaddingLeft(USR.user_id.toString(), 10, "0"); const IID_KEY = IID.slice(0, 10); const KEY = `${DID_KEY}.${UID_KEY}.${IID_KEY}`.toString(); const DID_IV = this.PaddingLeft(USR.device_id.toString(), 8, "0"); const UID_IV = this.PaddingLeft(USR.user_id.toString(), 8, "0"); const IV = `${DID_IV}${UID_IV}`; return { key: KEY, iv: IV }; } catch (error) { this.errorHandler(error); return { key: "", iv: "" }; } }; generateByProgramID = (programId) => { try { if (!programId) { throw new Error("Program ID is not defined"); } const PID_KEY = this.PaddingLeft(programId, 10, "0"); const F_KEY = this.PaddingLeft("0".toString(), 10, "0"); const FingerPrint_KEY = this.PaddingLeft("", 10, "0"); const KEY = `${PID_KEY}.${F_KEY}.${FingerPrint_KEY}`; const PID_IV = this.PaddingLeft(programId, 8, "0"); const F_IV = this.PaddingLeft("0".toString(), 8, "0"); const IV = `${PID_IV}${F_IV}`; return { key: KEY, iv: IV }; } catch (error) { this.errorHandler(error); return { key: "", iv: "" }; } }; generateByUserID = (user_id) => { try { const UID_KEY = this.PaddingLeft(user_id.toString(), 32, "0"); const UID_IV = this.PaddingLeft(user_id.toString(), 16, "0"); return { key: UID_KEY, iv: UID_IV }; } catch (error) { this.errorHandler(error); return { key: "", iv: "" }; } }; generateByInstallationID = (IID) => { try { if (!IID) { throw new Error("Installation ID is not defined"); } const IID_KEY = IID.slice(0, 32); const IID_IV = IID.slice(0, 16); return { key: IID_KEY, iv: IID_IV }; } catch (error) { this.errorHandler(error); return { key: "", iv: "" }; } }; // #endregion // #region "Utils" PaddingLeft = (value, length, paddingChar) => { try { if (value.length >= length) return value; return paddingChar.repeat(length - value.length) + value; } catch (error) { this.errorHandler(error); return ""; } }; } export { CipherHelper };