UNPKG

xypriss-security

Version:

Advanced High-Performance Security Framework. Military-grade encryption, post-quantum resilience, and fortified data structures.

122 lines 4.22 kB
"use strict"; /*************************************************************************** * XyPriss Security Core - Random Class ****************************************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); exports.Random = void 0; const bridge_1 = require("./bridge"); const SecureBuffer_1 = require("./SecureBuffer"); const utils_1 = require("../utils"); const constants_1 = require("../utils/constants"); /** * ### Random Class * * Cryptographically secure random number and token generation. */ class Random { /** * Generates a readable secure random token with specified constraints. * * @param length - The desired length of the generated token. * @param options - Configuration for character sets and entropy levels. * @returns A secure random string token. */ static generateToken(length = 32, options = {}) { let charset = ""; // Build charset based on options if (options.includeUppercase !== false || options.includeLowercase !== false || options.includeNumbers !== false || options.includeSymbols !== false) { if (options.includeLowercase !== false) charset += constants_1.C.LOWERCASE; if (options.includeUppercase !== false) charset += constants_1.C.UPPERCASE; if (options.includeNumbers !== false) charset += constants_1.C.NUMBERS; if (options.includeSymbols) charset += constants_1.C.SYMBOLS; } // Similarity filter if (options.excludeSimilarCharacters) { charset = charset.replace(constants_1.C.RSIMILAR_CHARS, ""); } const token = bridge_1.Bridge.generatePassword(length, charset); return new SecureBuffer_1.SecureBuffer((0, utils_1.stringToBuffer)(token)); } /** * Generates a numeric OTP of specified length. */ static generateOTP(digits = 6) { const res = bridge_1.Bridge.generateOTP(digits); if (res.startsWith("error:")) throw new Error(res); return res; } /** * Generates a secure random integer in [0, max). */ static getRandomInt(max) { return bridge_1.Bridge.getRandomInt(max); } /** * Generates a buffer of cryptographically secure random bytes. * * @param length - The number of random bytes to generate. * @returns A SecureBuffer containing random bytes. */ static getRandomBytes(length) { const bytes = bridge_1.Bridge.getRandomBytes(length); return new SecureBuffer_1.SecureBuffer(bytes); } /** * Generates a secure random integer in [min, max). * If only one argument is provided, it's treated as the maximum. * * @param minOrMax - The minimum value (inclusive) or maximum if second arg missing. * @param max - The maximum value (exclusive). */ static Int(minOrMax, max) { if (max === undefined) { return bridge_1.Bridge.getRandomInt(minOrMax); } const min = minOrMax; const range = max - min; if (range <= 0) return min; return min + bridge_1.Bridge.getRandomInt(range); } /** * Alias for getRandomBytes. */ static Bytes(...args) { return bridge_1.Bridge.getRandomBytes(...args); } /** * Alias for generateOTP. */ static OTP(...args) { return bridge_1.Bridge.generateOTP(...args); } /** * Randomly picks an item from an array using cryptographically secure random numbers. * * @param arr - The array to pick from. * @returns A random item from the array. */ static pick(arr) { if (!arr || arr.length === 0) { throw new Error("Cannot pick from an empty array"); } const index = bridge_1.Bridge.getRandomInt(arr.length); return arr[index]; } /** * Alias for generateToken. */ static generateSecureToken(...args) { return Random.generateToken(...args); } } exports.Random = Random; //# sourceMappingURL=Random.js.map