What is fnv-plus?
The fnv-plus npm package is a JavaScript implementation of the FNV (Fowler-Noll-Vo) hash function. It provides fast and efficient hashing capabilities, which are useful for various applications such as data integrity checks, hash tables, and more.
What are fnv-plus's main functionalities?
Hashing a String
This feature allows you to hash a string using the FNV hash function. The resulting hash can be used for data integrity checks or as a unique identifier.
const fnv = require('fnv-plus');
const hash = fnv.hash('example string');
console.log(hash.hex());
Hashing a Buffer
This feature allows you to hash a buffer, which is useful for binary data. The resulting hash can be used for various purposes, including data integrity and unique identification.
const fnv = require('fnv-plus');
const buffer = Buffer.from('example buffer');
const hash = fnv.hash(buffer);
console.log(hash.hex());
Hashing with Different Bit Lengths
This feature allows you to hash data with different bit lengths (32-bit or 64-bit). This can be useful depending on the specific requirements of your application.
const fnv = require('fnv-plus');
const hash32 = fnv.hash('example string', 32);
const hash64 = fnv.hash('example string', 64);
console.log(hash32.hex());
console.log(hash64.hex());
Other packages similar to fnv-plus
hash.js
hash.js is a library that provides various hash functions including SHA, RIPEMD, and more. It is more versatile compared to fnv-plus as it supports a wider range of hash functions.
crypto
The built-in Node.js 'crypto' module provides a wide range of cryptographic functionalities, including various hash functions like SHA-256, SHA-512, and more. It is more comprehensive compared to fnv-plus but may be overkill for simple FNV hashing needs.
murmurhash
murmurhash is a library that implements the MurmurHash algorithm, which is known for its speed and good distribution. It is similar to fnv-plus in terms of providing a fast hashing function but uses a different algorithm.
fnv-plus
Javascript FNV-1a Hashing Algorithm up to 1024 bits, with highly optimized 32bit and 52bit implementations.
0. Concept
The FNV-1a hash algorithm, often simply called "fnv", disperses hashes
throughout the n-bit hash space with very good dispersion and is very
fast.
Use this module to generate unique hash/checksum values for Javascript
strings or objects. Note: The FNV-1a algorithm is not even remotely suitable as
a cryptographic pseudo-random generator, and should not be used to secure any
thing for any reason. It is designed for uniqueness, not randomness.
Why fnv-plus?
- I call this module
fnv-plus
because it is the only npm module that
is capable of generating fnv hashes for keyspaces larger than 32 bits. fnv-plus
is well-tested. Many other fnv implementations offer no unit tests
to prove they work and are performant.fnv-plus
implements a 52bit version of FNV-1a which provides a larger
hash space while still making use of Javasript's 53-bit integer space.
New in 1.2.x
- You can easily define custom seeds. Most other fnv implementations hardcode
the fnv offset.
- the
hash()
function can now take arbitrary Javascript objects as input. - changed default bitlength to 52
1. Usage
var fnv = require('fnv-plus'),
astring = 'hello world',
ahash32 = fnv.hash(astring), // 32-bit hash by default
ahash64 = fnv.hash(astring, 64); // 64-bit hash specified
assert.equal(ahash32.hex(), '0xb23eba32');
assert.equal(ahash32.str(), '1dgfu42');
assert.equal(ahash32.dec(), '2990455346');
assert.equal(ahash64.hex(), '0xa8dd8fbdc2b13ffc');
assert.equal(ahash64.str(), '2kg3e4gji835o');
assert.equal(ahash64.dec(), '12168039813402935292');
fnv.seed('fobar testseed');
assert.notEqual(fnv.hash(astring), ahash64);
// ^^ because the default seed is not 'foobar testseed'
2. API
3. Contribute!