simple-str-hash
v2.4.0
Published
Lightweight string hashing utilities — FNV-1a and DJB2 implementations
Readme
simple-str-hash
Lightweight, dependency-free string hashing utilities for Node.js.
Implements several classic non-cryptographic hash algorithms (FNV, DJB2,
SDBM, CRC32, Java hashCode) plus small helpers for IDs, slugs and colors.
Install
npm install simple-str-hashUsage
const hash = require('simple-str-hash');
console.log(hash.fnv1a('hello world')); // 3582672807
console.log(hash.djb2('hello world')); // 894552257
console.log(hash.crc32('hello world')); // 222957957
console.log(hash.hash('hello world')); // default: FNV-1a
console.log(hash.toHex('hello world')); // 'd58b3fa7'
console.log(hash.toColor('hello world')); // '#...' deterministic color
console.log(hash.shortId('hello world')); // short URL-safe idAPI
Hashes
hash(str)— FNV-1a hash (default)fnv1a(str)— FNV-1a 32-bit hashfnv1(str)— FNV-1 32-bit hashdjb2(str)— DJB2 32-bit hashsdbm(str)— SDBM 32-bit hashcrc32(str)— CRC32 (IEEE) checksumjavaHashCode(str)— Java-styleString.hashCode()
Helpers
toHex(str, [fn])— hash as zero-padded hex stringtoColor(str)— deterministic#RRGGBBcolorshortId(str, [len], [alphabet])— short id from a hashrandomString(len, [prefix], [alphabet])— random stringslugify(str)— URL-friendly slugtruncate(str, [max], [ellipsis])— truncate with ellipsishashPassword(pw, [salt])/verifyPassword(pw, hash, salt)
License
MIT
