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

hash-anything

v1.3.3

Published

Hash any Javascript primitive or object

Downloads

493

Readme

hash-anything

Hash (almost) any Javascript primitive or object.

sha1(anything)

var sha1 = require('hash-anything').sha1;

var hash1 = sha1({
    a: 1,
    b: 2
});
console.log(hash1); //a8fe01c7b160932d628ba94d8400b6615846e791

var hash2 = sha1(1234);
console.log(hash2); //9deba4ae0dab97a17b7e9c299af6e4d52d994f1a

var hash3 = sha1('some text');
console.log(hash3); //4d3dd7cdbb862bbe4b9d1470f74dc26c5262c016

var hash4 = sha1(56.78);
console.log(hash4); //ac8433e96f689a49f3d54a52bb07d6e697dfc2ab

md5(anything)

var md5 = require('hash-anything').md5;

var hash = md5({
    a: 1,
    b: 2
});

console.log(hash); //f8761944960a318b27a1ee4104351f8a

sha256(anything)

var sha256 = require('hash-anything').sha256;

var hash = sha256({
    a: 1,
    b: 2
});

console.log(hash); //3c6d145329ac03391eb3db110935dbbc9e006af8c9da674f0c01b2c0e04f1fa6

sha512(anything)

var sha512 = require('hash-anything').sha512;

var hash = sha512({
    a: 1,
    b: 2
});

console.log(hash); //4f54bacd20498e42ca5c0f1c275032eb47b740e1a81db5af835d6310fc6b92ff678d09a9cb15c12e3f8780886c91c8ea242bd28de60618af8dd9b70620746fb6

new Hash(algorithm) - Hash multiple things at once.

algorithm (string) = 'sha1', 'md5', 'sha256', or 'sha512'

  • hash(anything) - add anything to the hash calculation
  • getValue() - get the hash value of everything added to the Hash object
  • clear() - clears the contents of the Hash object, so it can be reused.
var Hash = require('hash-anything').Hash;

var hash = new Hash('sha1')
    .hash(1234)
    .hash('some text')
    .hash(new Date(2013, 1, 1))
    .hash(567.89)
    .hash(/regex/);

console.log(hash.getValue()); //2e3518275983ee7ad81cc9a68fdc634ea9777dfc

new Hash(function) - Use a custom hashing routine.

The routine should take a Buffer and return the hash.

This example uses the xxhash module. It implements xxHash. xxHash is a fast, non-cryptographic hash algorithm. Do not use it for security, but it's great for comparing, caching, indexing, etc.

var Hash = require('hash-anything').Hash;
var XXHash = require('xxhash');

var doHash = function(buf) {
    var hasher = new XXHash(0x6d4e9ec6); //random seed
    hasher.update(buf);
    return hasher.digest();
};

var hash = new Hash(doHash)
    .hash(1234)
    .hash('some text')
    .hash(new Date(2013, 1, 1))
    .hash(567.89)
    .hash(/regex/);

console.log(hash.getValue()); //3804156080

Supported Types

  • Primatives: Boolean, Number, String, BigInt, undefined, and Symbol
  • Built-In Objects: Date, RegExp, Null, Object, Function, TypedArrays(Uint16Array, etc.), Map, Set, GeneratorFunction, AsyncFunction
  • Global Properties: Nan, Infinity

Unsupported Types

  • WeakMap
  • WeakSet
  • Promise
  • DataView

Circular References

Currently, hash-anything does not support circular references. They will cause a stack overflow error.

Note on New Types

When a new built-in type is added to Javascript, hash-anything usually needs be updated to support it. Otherwise, the new type will be treated like any other user-defined objects. This may not produce ideal results..