UNPKG

dev-utils-plus

Version:

Type-safe utility functions for JavaScript/TypeScript: string, array, object, date, validation, crypto, format, math

169 lines 4.66 kB
"use strict"; /** * Crypto utility functions for common cryptographic operations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.randomString = randomString; exports.randomNumber = randomNumber; exports.generateUUID = generateUUID; exports.simpleHash = simpleHash; exports.toBase64 = toBase64; exports.fromBase64 = fromBase64; exports.toBase64Url = toBase64Url; exports.fromBase64Url = fromBase64Url; exports.checksum = checksum; exports.randomHex = randomHex; exports.randomAlphanumeric = randomAlphanumeric; exports.randomNumeric = randomNumeric; exports.generatePassword = generatePassword; /** * Generates a random string of specified length */ function randomString(length, charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') { let result = ''; for (let i = 0; i < length; i++) { result += charset.charAt(Math.floor(Math.random() * charset.length)); } return result; } /** * Generates a random number between min and max (inclusive) */ function randomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } /** * Generates a random UUID v4 */ function generateUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = Math.random() * 16 | 0; const v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } /** * Creates a simple hash from a string */ function simpleHash(str) { let hash = 0; if (str.length === 0) return hash; for (let i = 0; i < str.length; i++) { const char = str.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; // Convert to 32-bit integer } return Math.abs(hash); } /** * Encodes a string to base64 */ function toBase64(str) { if (typeof btoa !== 'undefined') { return btoa(str); } // Fallback for Node.js return Buffer.from(str).toString('base64'); } /** * Decodes a base64 string */ function fromBase64(str) { if (typeof atob !== 'undefined') { return atob(str); } // Fallback for Node.js return Buffer.from(str, 'base64').toString(); } /** * Encodes a string to URL-safe base64 */ function toBase64Url(str) { return toBase64(str) .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=/g, ''); } /** * Decodes a URL-safe base64 string */ function fromBase64Url(str) { // Add padding back while (str.length % 4) { str += '='; } str = str.replace(/-/g, '+').replace(/_/g, '/'); return fromBase64(str); } /** * Creates a checksum hash from a string */ function checksum(str) { let hash = 0; if (str.length === 0) return hash.toString(); for (let i = 0; i < str.length; i++) { const char = str.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; } return hash.toString(16); } /** * Generates a random hex string */ function randomHex(length) { const chars = '0123456789abcdef'; let result = ''; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } /** * Generates a random alphanumeric string */ function randomAlphanumeric(length) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let result = ''; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } /** * Generates a random numeric string */ function randomNumeric(length) { const chars = '0123456789'; let result = ''; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } /** * Generates a random password with specified requirements */ function generatePassword(options = {}) { const { length = 12, includeUppercase = true, includeLowercase = true, includeNumbers = true, includeSymbols = true } = options; let chars = ''; if (includeUppercase) chars += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; if (includeLowercase) chars += 'abcdefghijklmnopqrstuvwxyz'; if (includeNumbers) chars += '0123456789'; if (includeSymbols) chars += '!@#$%^&*()_+-=[]{}|;:,.<>?'; if (chars.length === 0) { throw new Error('At least one character type must be included'); } let result = ''; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } //# sourceMappingURL=index.js.map