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

43 lines (42 loc) 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tokenGenerator = exports.characterSets = void 0; exports.characterSets = { defaultSet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=~[]{}|;:,.<>?/', number: '0123456789', alpha: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', symbols: '!@#$%^&*()_+-=~[]{}|;:,.<>?/\'"\\\\`' }; /** * Generates a random token string of specified length using the given character set * * @param {number} length - The desired length of the generated token * @param {CharacterSetType} charSet - The character set to use for generation * @returns {string} A random token string * @throws {Error} If token generation fails * * @example * tokenGenerator(8, 'defaultSet') // Returns something like "Kj9mP2nX" * * @since 1.0.0 * @category Generator * @public */ const tokenGenerator = (length, charSet) => { let token = ''; if (charSet == undefined) charSet = exports.characterSets.defaultSet; const characterSet = exports.characterSets[charSet] || charSet; for (let i = 0; i < length; i++) { token += characterSet.charAt(Math.floor(Math.random() * characterSet.length)); } do { var _token_ = token.split(''); if (_token_[0] === '0') _token_[0] = characterSet.charAt(Math.floor(Math.random() * characterSet.length)); if (token.length < length) token += characterSet.charAt(Math.floor(Math.random() * characterSet.length)); } while (_token_[0] === '0' || token.length !== length); return _token_.join(''); }; exports.tokenGenerator = tokenGenerator;