npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spookyhash

v3.0.0

Published

Node bindings for SpookyHash V2

Downloads

1,406

Readme

spookyhash

npm Build Downloads Node Issues Known Vulnerabilities

Node bindings for SpookyHash V2.

Table of Contents

spookyhash

The spookyhash module provides wrappers for SpookyHash V2.

SpookyHash was devised by Bob Jenkins and is described as "a 128-bit noncryptographic hash".

Use require('spookyhash') to access this module.

const spookyhash = require('spookyhash');

const secret = Buffer.from('abdcdefg');

const hash = new spookyhash.Hash();
hash.update(secret);
console.log(hash.digest().toString('hex'));
// Prints:
//   7ea54bc662fb34e1e1057e271a0d3782

console.log(spookyhash.hash128(secret).toString('hex'));
// Prints:
//   7ea54bc662fb34e1e1057e271a0d3782

console.log(spookyhash.hash64(secret));
// Prints:
//   16227871758974952830n

console.log(spookyhash.hash32(secret));
// Prints:
//   1938270189

Similar projects

node-spookyhash-v2 also provides NodeJS bindings for SpookyHash V2. Unfortunately it's not been updated for some years and doesn't work with newer versions of Node. From 2021-04-30 it will not run on any supported version of Node.

I spent a little time attempting to upgrade that code, but decided it would be easier to build new bindings using N-API rather than learn the v8 API!

Class: Hash

The Hash class is a utility for assembling a 128-bit hash from separate parts (unlike hash128) which requires the entire message to be available).

const hash = new spookyhash.Hash();
hash.update(Buffer.from('one'));
console.log(hash.digest().toString('hex'));
// Prints:
//   d19880c1b51c5eab60a2da1901446f99

hash.update(Buffer.from('two'));
console.log(hash.digest().toString('hex'));
// Prints:
//   4042682e0a624f3d0d29a6f8a6c5db76

hash.update(Buffer.from('three'));
console.log(hash.digest().toString('hex'));
// Prints:
//   74782eb99a1d50c3ad109b7c9beaaeac

new Hash([seed1[, seed2]])

  • seed1, seed2 <BigInt> | <Buffer> Seeds for the hash calculation. If passed as a BigInt a seed must be an unsigned, 64-bit integer. If passed as a Buffer a seed must be 8 bytes in length. If not provided then a default of 0 will be used.
const hash1 = new spookyhash.Hash();
hash1.update(Buffer.from('Test Message'))
console.log(hash1.digest())
// Prints:
//   <Buffer 01 e7 5a b5 6a 0a 16 13 2b 9d 9b f4 d0 b3 5d f6>

const hash2 = new spookyhash.Hash(3141592653589793238n, 2718281828459045235n);
hash2.update(Buffer.from('Test Message'))
console.log(hash2.digest())
// Prints:
//   <Buffer 52 26 cf 42 95 35 93 66 3d ac 4a f4 5a 83 fc eb>

hash.digest()

Calculates the digest of all the data passed to be hashed (using the hash.update() method).

hash.update(message)

Updates the hash content with the given message.

This can be called many times with new data as it is streamed.

spookyhash module methods and properties

spookyhash.hash128(message[, seed1[, seed2]])

  • message <Buffer> The message to hash

  • seed1, seed2 <BigInt> | <Buffer> Seeds for the hash calculation. If passed as a BigInt a seed must be an unsigned, 64-bit integer. If passed as a Buffer a seed must be 8 bytes in length. If not provided then a default of 0 will be used.

  • Returns: <Buffer>

Calculates a 128-bit hash of the provided Buffer.

spookyhash.hash128(Buffer.from('Test Message'))
// Prints:
//   <Buffer 01 e7 5a b5 6a 0a 16 13 2b 9d 9b f4 d0 b3 5d f6>

spookyhash.hash128(Buffer.from('Test message'), 3141592653589793238n, 2718281828459045235n);
// Prints:
//   <Buffer ab 95 34 ca 56 03 a6 41 c3 69 ae 6e c1 b8 98 1d>

spookyhash.hash64(message[, seed])

  • message <Buffer> The message to hash

  • seed <BigInt> | <Buffer> Seed for the hash calculation. If passed as a BigInt a seed must be an unsigned, 64-bit integer. If passed as a Buffer a seed must be 8 bytes in length. If not provided then a default of 0 will be used.

  • Returns: <BigInt>.

Calculates a 64-bit hash of the provided Buffer.

spookyhash.hash64(Buffer.from('Test Message'));
// Prints:
//   9120740005544271225n

spookyhash.hash64(Buffer.from('Test message'), 3141592653589793238n);
// Prints:
//   9667288884877287795n

spookyhash.hash32(message[, seed])

  • message <Buffer> The message to hash

  • seed <integer> Seed for the hash calculation. If not provided then a default of 0 will be used.

  • Returns: <integer>

Calculates a 32-bit hash of the provided Buffer.

spookyhash.hash32(Buffer.from('Test Message'));
// Prints:
//   388570489

spookyhash.hash32(Buffer.from('Test Message'), 42);
// Prints:
//   4254323322