UNPKG

zexson_toolkit

Version:

Zexson Toolkit is a powerful encryption and tokenization library developed by Zexson Team. It offers proprietary encryption algorithms, high-security random token generation, and advanced object comparison features. It includes many advanced security func

162 lines (161 loc) 6.65 kB
import type { base64DecodeType, cryptObject, DecryptBodyFunction, decryptType, EncryptBodyFunction, encryptType, isEqualType } from './types'; import { base64EncodeType } from './types'; /** * Encrypts a string using custom logic with random padding and Base64 encoding. * * @param {string} data - The string to encrypt. * @param {Object} [options] - Encryption options. * @param {CharacterSetType} [options.firstRandomCharSet] - Character set for the first random padding. * @param {CharacterSetType} [options.secondRandomCharSet] - Character set for the second random padding. * @param {string} [options.key] - Encryption key for additional security. * @returns {Promise<string | undefined>} The encrypted string or `undefined` if encryption fails. * @throws {Error} Throws if the encryption process encounters an issue. * * @example * const encrypted = await encrypt("hello") * const customEncrypted = await encrypt("hello", { key: "myCustomKey" }) * * @since 1.0.0 * @category Encryption */ export declare const encrypt: encryptType; /** * Decrypts a string that was encrypted using the `encrypt` function. * * @param {string} data - The encrypted string to decrypt. * @param {Object} [options] - Decryption options. * @param {string} [options.key] - Encryption key used during encryption. * @returns {Promise<string>} The decrypted original string. * @throws {Error} Throws if the decryption process encounters an issue. * * @example * const decrypted = await decrypt(encryptedString) * const customDecrypted = await decrypt(encryptedString, { key: "myCustomKey" }) * * @since 1.0.0 * @category Decryption */ export declare const decrypt: decryptType; /** * Encodes a string using a custom Base64 algorithm with added character shifting. * * @param {string} data - The string to encode. * @param {string} [key='qwertyuioplkjhgfdsazxcvbnm'] - Key for influencing the encoding process. * @returns {string} The encoded string. * @throws {Error} Throws if encoding fails. * * @example * const encoded = base64Encode("Hello, World!") * const customEncoded = base64Encode("Hello, World!", "customKey123") * * @since 1.0.5 * @category Encoding */ export declare const base64Encode: base64EncodeType; /** * Decodes a string encoded by `base64Encode` back to its original form. * * @param {string} encryptedData - The string to decode. * @param {string} [key='qwertyuioplkjhgfdsazxcvbnm'] - The key used during encoding. * @returns {string} The decoded string. * @throws {Error} Throws if decoding fails. * * @example * const decoded = base64Decode(encodedString) * const customDecoded = base64Decode(encodedString, "customKey123") * * @since 1.0.5 * @category Decoding */ export declare const base64Decode: base64DecodeType; /** * Compares a plain text string with an encrypted string for equality. * * @param {string} text - The plain text string. * @param {string} encrypted - The encrypted string to compare. * @param {IsEqualOptions} [options] - Comparison options (e.g., case sensitivity). * @returns {Promise<{ isEqual: boolean; method?: string }>} Indicates if the strings match and the method used. * * @example * const match = await isEqual("hello", encryptedString) * const caseInsensitiveMatch = await isEqual("hello", encryptedString, { caseSensitive: false }) * * @since 1.0.0 * @category Comparison */ export declare const isEqual: isEqualType; /** * Encrypts all values in an object. * * @template T - An object type where keys are strings, and values are strings. * @param {T} data - The object containing values to be encrypted. * @param {string} [key='qwertyuioplkjhgfdsazxcvbnm'] - The encryption key to use. * @returns {T} A new object with encrypted values. * @throws {Error} If encryption fails for any value. * * @example * const data = { username: 'JohnDoe', password: '12345' } * const encryptedData = encryptObject(data, 'my-secret-key') * * @since 1.0.7 * @category Encryption * @public */ export declare const encryptObject: cryptObject; /** * Decrypts all values in an object. * * @template T - An object type where keys are strings, and values are encrypted strings. * @param {T} data - The object containing values to be decrypted. * @param {string} [key='qwertyuioplkjhgfdsazxcvbnm'] - The decryption key to use. * @returns {T} A new object with decrypted values. * @throws {Error} If decryption fails for any value. * * @example * const encryptedData = { username: 'abc123', password: 'def456' } * const decryptedData = decryptObject(encryptedData, 'my-secret-key') * * @since 1.0.7 * @category Decryption * @public */ export declare const decryptObject: cryptObject; /** * Encrypts all keys and retains their values in an object. * * @template T - An object type where keys are strings and values can be of any type. * @param {T} object - The object whose keys will be encrypted and values retained. * @param {string} [secretKey='qwertyuioplkjhgfdsazxcvbnm'] - The encryption key to use for encrypting the keys. * @returns {EncryptableObject} A new object with encrypted keys and original values. * @throws {Error} If encryption fails for any value or key. * * @example * const data = { username: 'user123', password: 'pass456' } * const encryptedData = encryptBody(data) // encrypts the keys and values * * @since 1.1.1 * @category Encryption * @public */ export declare const encryptBody: EncryptBodyFunction; /** * Decrypts all keys and retains their values in an object. * * @template T - An object type where keys are strings, and values are encrypted strings. * @param {T} object - The object containing values to be decrypted, with keys being encrypted. * @param {string} [secretKey='qwertyuioplkjhgfdsazxcvbnm'] - The decryption key to use. * @returns {EncryptableObject} A new object with decrypted keys and original values. * @throws {Error} If decryption fails for any value. * * @example * const encryptedData = { * 'YlWzR3exYqt4eIFDcG4PWIfz]kOr\\4tZf6FUR4oRS1Loe0V|ZpE:RTA=': 'YUjOV1qXVqR4d59DcJQPVofyekOueJp[X58xSXo7PU2@', * 'Yn7Rh1etWrV4d3N1VFkPVYfy]kOv\\4tif5=2R4kRS1noQmR7]ZV5ejA=': 'ZH3tU2evQ4NoT5lK[VL9cofyWlq5e~NEcGJGNIM~S1nog0uQg682WYZsVmUYMl\\UX22QR\\dxXHRsZ2wGTH3n^mWYeK5jUJg9' * } * const decryptedData = decryptBody(encryptedData) // { username: 'user123', password: 'pass456' } * * @since 1.1.1 * @category Decryption * @public */ export declare const decryptBody: DecryptBodyFunction;