const xorCrypt = require("xor-crypt"); import { TObject } from "."; export namespace dcrypto { // Below 4 function is the basics of encoding uisng xor export function encode_xor(text: string, len: number = 10) { return xorCrypt(text, len); } export function decode_xor(text: string, len: number = 10) { return xorCrypt(text, len); } export function encodeObjXor(text: TObject, len: number = 10) { return xorCrypt(JSON.stringify(text), len); } export function decodeObjXor(text: TObject, len: number = 10) { return JSON.parse(xorCrypt(text, len)); } /** summary of the two TypeScript functions (encodeJsonToBase64 and decodeBase64ToJson) designed to obfuscate JSON data using Base64 encoding and decoding. */ export function encodeJsonToBase64(jsonData: object): string { const jsonString = JSON.stringify(jsonData); // Convert JSON to string // @ts-ignore if (typeof window !== "undefined" && window.btoa) { // Browser environment return btoa(jsonString); // @ts-ignore } else if (typeof Buffer !== "undefined") { // Node.js environment // @ts-ignore return Buffer.from(jsonString, "utf-8").toString("base64"); } else { throw new Error("Environment not supported for Base64 encoding."); } } /** summary of the two TypeScript functions (encodeJsonToBase64 and decodeBase64ToJson) designed to obfuscate JSON data using Base64 encoding and decoding. */ export function decodeBase64ToJson(base64String: string): object { let jsonString: string; // @ts-ignore if (typeof window !== "undefined" && window.atob) { // Browser environment jsonString = atob(base64String); // @ts-ignore } else if (typeof Buffer !== "undefined") { // Node.js environment // @ts-ignore jsonString = Buffer.from(base64String, "base64").toString("utf-8"); } else { throw new Error("Environment not supported for Base64 decoding."); } return JSON.parse(jsonString); // Convert string back to JSON } } /* broken react native const Cryptr = require("cryptr"); export namespace dcrypto { export function encrypt(str: string, key: string): string { dassert.verifyNotNullAndEmpty(key, "must pass a valid key"); return new Cryptr(key).encrypt(str); } export function decrypt(str: string, key: string): string { dassert.verifyNotNullAndEmpty(key, "must pass a valid key"); try { return new Cryptr(key).decrypt(str); } catch (e) { throw new Error("Invalid key - not able to decrypt"); } } }*/