kritikov/IdCraft

GitHub: kritikov/IdCraft

一个零依赖的 JavaScript 标识符工具包,提供基于密码学安全的多种 ID 生成与深度检测功能。

Stars: 1 | Forks: 0

# 🛠️ IdCraft.js **IdCraft.js** 是一个专业级的 JavaScript 工具包,旨在生成和检测具有强密码学特性的标识符。它为 **NanoID**、**UUID (v1, v4, v7)**、**ULID** 和 **Random String** 提供了统一的接口,并内置了元数据分析功能。 [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) ![Size: Tiny](https://img.shields.io/badge/Size-Tiny-blue) ![Security: CSPRNG](https://img.shields.io/badge/Security-CSPRNG-green) ## ✨ 核心特性 * **ULID 生成**:通用唯一字典序可排序标识符(Universally Unique Lexicographically Sortable Identifiers)。使用 Crockford 的 Base32 字母表生成 26 个字符的、URL 安全的 ID。包含自动的 **Monotonicity Guard**(单调性保护),以确保在同一毫秒内的批量生成过程中具有严格的时间顺序。 * **NanoID 生成**:可定制的、URL 友好的 ID,内置防止取模偏差(modulo bias)的保护。 * **UUID 生成**: * **v4**:安全的随机标识符。在可用时使用原生的 `crypto.randomUUID()` 以实现最高性能。 * **v7**:现代的、按时间排序的 ID——针对数据库主键和顺序索引进行了优化。 * **Random String 生成**:生成随机字符串和密码。 * **深度检测 (v1, v4, v7)**:强大的分析工具,可“解码”现有的 UUID。从 v1 字符串中提取 **时间戳**、**ISO 日期**、**相对时间**(例如,“5 分钟前”),甚至 **Node (MAC)** 信息。 * **密码学安全**:利用 Web Crypto API (`crypto.getRandomValues`) 实现最大熵。 * **零依赖**:纯 JavaScript。轻量、快速且无依赖。 * **熵分析引擎**:测量任何字符串 payload 的精确数学强度。使用香农熵(Shannon Entropy)公式计算以位($H$)为单位的信息熵。 ## 🚀 安装 你可以通过导入的方式在你的项目中包含 IdCraft.js: ``` import IdCraft from './IdCraft.js'; ``` ## 📖 使用指南 ### 1. 生成 NanoID 创建简短、安全且可定制的 ID。 ``` const result = IdCraft.generateNanoIds({ count: 10, length: 12, lowercase: true, uppercase: true, numbers: true, symbols: true, extra: "", prefix: "user_", suffix: "" }); console.log(result.nanoIds); // ["user_A1b2C3d4E5f6"] ``` ### 2. 生成 UUID 生成标准的 v4(随机)或全新的 v7(按时间排序)UUID。 ``` // Generate 5 time-ordered UUIDs (v7) const batch = IdCraft.generateUUIDs({ count: 5, version: "v7", // v4, v7 format: "lowercase", // lowercase | uppercase withHyphens: true, braces: "none", // none, curly }); console.log(batch.uuids); ``` ### 3. 生成 ULID 生成由 Crockford 的 Base32 编码包装的通用唯一字典序可排序标识符(ULID)。 ``` // Generate 5 time-ordered ULIDs const batch = IdCraft.generateULIDs({ count: 5, format: "uppercase" // uppercase (standard) | lowercase }); console.log(batch.ulids); // Output: Array of 26-character sortable strings console.log(batch.monotonicUsed); // true if monotonicity guard was triggered within the same millisecond ``` ### 4. 生成 Random String 和密码 利用智能过滤规则和字符组分布保证,创建具有密码学安全性的随机字符串或强密码。 ``` const result = IdCraft.getRandomString({ length: 16, lowercase: true, uppercase: true, numbers: true, symbols: true, excludeSimilar: true, // Excludes confusing chars like O, 0, I, l, 1 excludeAmbiguous: true, // Excludes symbols like ' " ` \ to prevent escaping issues guaranteeAll: true, // Ensures at least one character from each active group extra: "" // Pass any additional custom characters here }); console.log(result.string); // Example: "pX9#mK4@fG2!vR7$" ``` ### 5. 检测 UUID IdCraft 的特性之一是能够“解构” UUID 字符串以查看其来源。 ``` // Example: Inspecting multiple IDs at once const items = [ "018f4a12-b7e1-7abc-8d2f-4a5b6c7d8e9f", // valid v7 "48507851-419b-4654-9337-1836f32e9206", // valid v4 "invalid-id-123" // invalid ]; const results = IdCraft.inspectUUIDs(items); results.forEach((inspection, index) => { if (inspection.valid) { console.log(`Item ${index} is a valid v${inspection.uuid.version}`); console.log(inspection.uuid.getInformation()); // Access relative time if it's a time-based UUID (v1 or v7) const timeInfo = inspection.uuid.v7?.relativeTime || inspection.uuid.v1?.relativeTime; if (timeInfo) console.log(`Generated: ${timeInfo}`); } else { console.error(`Item ${index} error: ${inspection.error}`); } }); ``` ### 6. 分析字符串熵与强度 IdCraft 包含一个熵评估引擎,旨在测量任何字符串 payload 的数学强度,计算其破解时间,并映射架构漏洞。 ``` // Example: Analyzing an API key in Exhaustive Mode const tokenResult = IdCraft.analyzeEntropy("4a5b6c7d8e9f2a1b", { searchMode: "exhaustive" }); if (tokenResult.valid) { console.log(`Entropy: ${tokenResult.entropyBits} bits`); console.log(`Character Pool Size: ${tokenResult.poolSize}`); console.log(`Estimated Crack Time: ${tokenResult.crackTimeFormatted}`); } // Example 2: Profiling a user password in Smart Mode (evaluating heuristics) const passwordResult = IdCraft.analyzeEntropy("Password12345!", { searchMode: "smart", guessesPerSecond: 1e12 // Simulating a high-end GPU cracking rig }); if (passwordResult.valid) { console.log(`String Length: ${passwordResult.stringLength}`); console.log(`Vulnerability Metrics: ${passwordResult.crackTimeFormatted}`); // Iterate through telemetry logs to inspect active structural alerts or penalties passwordResult.messages.forEach(msg => { console.log(`[${msg.severity.toUpperCase()}] ${msg.title}: ${msg.details}`); }); } ``` ## 🛡️ 安全与性能 ### 原生速度 对于 UUID v4,IdCraft 会自动检测当前环境是否支持原生的 crypto.randomUUID() 方法。 如果支持,它将使用浏览器或 runtime 的优化实现。如果不支持,它将回退到安全的 crypto.getRandomValues() buffer 实现。 ### 取模偏差保护 大多数简单的 ID 生成器使用 Math.random() % charset.length,这会产生一种细微的 统计偏差。IdCraft 通过计算 maxValid 阈值消除了这一问题,确保 你的字母表中的每个字符都有在数学上同等的机会被选中。 ## 🛡️ 在线查看 你可以在以下网址在线查看该库的用法: https://nkode.gr/GR/tools/uuid-generator image https://nkode.gr/EN/tools/uuid-inspector image https://nkode.gr/EN/tools/nano-id-generator image ## 📜 License 该项目基于 GNU GPL v3.0 或更高版本授权 - 详情请参阅 LICENSE 文件。
标签:CMS安全, ID生成器, JavaScript, NanoID, SOC Prime, UUID, 密码学, 开发工具, 手动系统调用, 数据可视化, 自定义脚本