UNPKG

forgeid

Version:

Scalable and secure unique ID generator with HMAC verification and time-based growth.

151 lines (102 loc) โ€ข 2.92 kB
# ๐Ÿ” ForgeID A scalable, secure, and verifiable unique ID generator for Node.js. Designed to evolve with time and prevent collisions for decades โ€” even millennia. [![NPM Version](https://img.shields.io/npm/v/forgeid.svg)](https://www.npmjs.com/package/forgeid) [![License](https://img.shields.io/npm/l/forgeid)](LICENSE) [![Test](https://img.shields.io/badge/tests-passing-brightgreen)](#) [![Build](https://img.shields.io/badge/build-manual-blue)](webpack.config.js) --- ## โœจ Features - โœ… Cryptographically signed (HMAC SHA-256) - ๐Ÿง† Unique and verifiable - ๐Ÿ•ฐ๏ธ ID length grows over time - ๐Ÿ” Includes timestamp + device fingerprint - ๐Ÿงช Built-in validation and stress testing - ๐Ÿฉธ Zero dependency (only uses `crypto` and `os`) - ๐ŸŽจ Supports prefix and formatting (`dash`, `space`) --- ## ๐Ÿš€ Installation ```bash npm install forgeid ``` --- ## ๐Ÿ”ง Basic Usage ```js const ForgeID = require('forgeid') const forge = new ForgeID('your-secret') const id = forge.generate() console.log('ID:', id) const isValid = forge.verify(id) console.log('Verified:', isValid) ``` --- ## ๐ŸŽจ With Prefix & Format ```js forge.generate('TRX') // TRX-abc123xyz... forge.generate('ORD', 'dash') // ORD-abc123-def456... forge.generate('REF', 'space') // REF abc123 def456... ``` --- ## ๐Ÿ“€ ID Structure Each ID includes: - Random entropy from `crypto.randomBytes` - Host fingerprint (`hostname + MAC`) - Base36 timestamp (`Date.now()`) - Signature from HMAC (last 10 characters) ID format is: ``` [prefix-]baseContent + signature ``` Length increases over time: ``` length = baseLength + floor((currentYear - startYear) / intervalYears) ``` --- ## ๐Ÿงช Stress Test ```js const forge = new ForgeID('your-secret') forge.stressTest(1e6, 1e5) ``` Tests 1 million IDs for: - Duplicate collision - HMAC signature validity --- ## ๐Ÿ—’๏ธ API ```ts new ForgeID(secret?: string, startYear?: number, baseLength?: number, intervalYears?: number) forge.generate(prefix?: string, format?: 'dash' | 'space' | ''): string forge.verify(id: string): boolean forge.format(id: string, style?: 'dash' | 'space' | ''): string forge.stressTest(total?: number, step?: number): void ``` --- ## โœ… Unit Testing ```bash npm install --save-dev mocha chai npm test ``` Test cases cover: - Raw ID generation & verification - Formatted and prefixed IDs - Tampered/invalid inputs - Collision-free generation (10K+) --- ## ๐Ÿง  TypeScript Support ```ts import ForgeID from 'forgeid' const forge = new ForgeID() const id: string = forge.generate('TRX', 'dash') const isValid: boolean = forge.verify(id) ``` Types are defined in `forgeid.d.ts`. --- ## ๐Ÿ“† Build ```bash npm run build ``` Produces: - `dist/forgeid.min.js` โ†’ minified bundle - `dist/forgeid.d.ts` โ†’ TypeScript definitions --- ## ๐Ÿ“„ License MIT ยฉ [NeaByteLab](https://github.com/NeaByteLab)