UNPKG

nehoid

Version:

Advanced unique ID generation utility with multi-layer encoding, collision detection, and context-aware features

148 lines 5.24 kB
/** * Checksum and hash utilities for NehoID. * * This module provides various checksum and hashing algorithms * for ID validation, integrity checking, and data fingerprinting. */ export type ChecksumAlgorithm = "djb2" | "crc32" | "adler32" | "fnv1a" | "murmur3"; export declare class ChecksumError extends Error { constructor(message: string); } /** * Generates checksums using various algorithms. * * Provides multiple checksum algorithms for different use cases: * - djb2: Fast, simple hash (default) * - crc32: Cyclic redundancy check (good for error detection) * - adler32: Adler-32 checksum (fast, good for small data) * - fnv1a: FNV-1a hash (good distribution) * - murmur3: MurmurHash3 (high quality, good for large data) */ export declare class Checksum { private static readonly MAX_INPUT_LENGTH; private static readonly MIN_CHECKSUM_LENGTH; private static readonly MAX_CHECKSUM_LENGTH; private static readonly VALID_ALGORITHMS; private static crcTableCache; /** * Generates a checksum using the specified algorithm. * * @param input - String to generate checksum for * @param algorithm - Checksum algorithm to use (default: 'djb2') * @param length - Desired length of checksum output (default: 4 for short checksums) * @returns Checksum string * @throws {ChecksumError} If input validation fails * * @example * ```typescript * const checksum = Checksum.generate('hello world'); * // Output: '2a3b' (djb2 hash, 4 chars) * * const crc32 = Checksum.generate('data', 'crc32', 8); * // Output: '8 chars CRC32' * ``` */ static generate(input: string, algorithm?: ChecksumAlgorithm, length?: number): string; /** * Validates input parameters for checksum generation. * @private */ private static validateInput; /** * DJB2 hash algorithm - fast and simple. * Good for basic integrity checks and short checksums. * * @param str - Input string * @returns 32-bit hash number */ private static djb2; /** * CRC32 (Cyclic Redundancy Check) - good for error detection. * Better at detecting errors than simple hashes. * * @param str - Input string * @returns 32-bit CRC32 checksum */ private static crc32; /** * Adler-32 checksum - fast and good for small data. * Used in zlib compression. * * @param str - Input string * @returns 32-bit Adler-32 checksum */ private static adler32; /** * FNV-1a hash - good distribution properties. * Fast and provides good hash distribution. * * @param str - Input string * @returns 32-bit FNV-1a hash */ private static fnv1a; /** * MurmurHash3 (simplified version) - high quality hash. * Good for larger data and cryptographic purposes. * * @param str - Input string * @param seed - Optional seed value for hash initialization * @returns 32-bit MurmurHash3 */ private static murmur3; /** * Gets or generates CRC32 lookup table (cached for performance). * @private */ private static getCRCTable; /** * Validates a checksum against input data. * * @param input - Original input string * @param checksum - Checksum to validate * @param algorithm - Algorithm used for checksum (default: 'djb2') * @param length - Length of checksum used (default: 4) * @returns True if checksum matches, false otherwise * @throws {ChecksumError} If validation parameters are invalid * * @example * ```typescript * const data = 'important-data'; * const checksum = Checksum.generate(data); * * // Later validation * const isValid = Checksum.validate(data, checksum); * console.log(isValid); // true * ``` */ static validate(input: string, checksum: string, algorithm?: ChecksumAlgorithm, length?: number): boolean; /** * Safely generates a checksum, returning null on error instead of throwing. * * @param input - String to generate checksum for * @param algorithm - Checksum algorithm to use (default: 'djb2') * @param length - Desired length of checksum output (default: 4) * @returns Checksum string or null on error * * @example * ```typescript * const checksum = Checksum.tryGenerate('data'); * if (checksum) { * console.log('Checksum:', checksum); * } else { * console.log('Failed to generate checksum'); * } * ``` */ static tryGenerate(input: string, algorithm?: ChecksumAlgorithm, length?: number): string | null; /** * Safely validates a checksum, returning false on error instead of throwing. * * @param input - Original input string * @param checksum - Checksum to validate * @param algorithm - Algorithm used for checksum (default: 'djb2') * @param length - Length of checksum used (default: 4) * @returns True if valid, false if invalid or on error */ static tryValidate(input: string, checksum: string, algorithm?: ChecksumAlgorithm, length?: number): boolean; } //# sourceMappingURL=checksum.d.ts.map