Socket
Socket
Sign inDemoInstall

object-hash

Package Overview
Dependencies
1
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    object-hash

Generate hashes from javascript objects in node and the browser.


Version published
Weekly downloads
18M
increased by7.2%
Maintainers
1
Install size
568 kB
Created
Weekly downloads
 

Package description

What is object-hash?

The object-hash npm package provides a function to generate a hash from a JavaScript object using various algorithms. It is useful for creating unique identifiers for objects, comparing objects by their hash, and caching objects based on their content.

What are object-hash's main functionalities?

Generate hash from an object

This feature allows you to generate a hash string from a JavaScript object. The hash is determined by the object's content.

const hash = require('object-hash');
const myObject = { name: 'Alice', age: 25 };
const objectHash = hash(myObject);

Generate hash with different algorithms

This feature allows you to specify different hashing algorithms, such as 'md5', 'sha1', 'sha256', etc., to generate the hash.

const hash = require('object-hash');
const options = { algorithm: 'sha256' };
const objectHash = hash({ name: 'Alice', age: 25 }, options);

Exclude properties from hashing

This feature allows you to exclude the values of the properties from the hash, effectively hashing the structure (keys) of the object only.

const hash = require('object-hash');
const options = { excludeValues: true };
const objectHash = hash({ name: 'Alice', age: 25 }, options);

Respect object types

This feature allows you to ignore the type of the object when generating the hash, treating arrays and objects with the same content as equal.

const hash = require('object-hash');
const options = { respectType: false };
const objectHash = hash([1, 2, 3], options);

Other packages similar to object-hash

Readme

Source

status

Object-Hash

Generate hashes from objects and values in node and the browser. Uses node.js crypo module for hashing. Supports sha1, md5 and many others (depending on the host os).

var objectHash = require('object-hash');

objectHash(value, options);

Generate a hash from any object or type. Defaults to sha1 with hex encoding.

Installation

node:

npm install object-hash

browser: /dist/object_hash.js

<script src="object_hash.min.js" type="text/javascript"></script>

Example usage

var objectHash = require('object-hash');

var peter = {name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };
var michael = {name: 'Michael', stapler: false, friends: ['Peter', 'Samir'] };
var bob = {name: 'Bob', stapler: true, friends: [] };

/***
 * sha1 hex encoding (default)
 */
console.log(objectHash(peter));
// 14fa461bf4b98155e82adc86532938553b4d33a9
console.log(objectHash(michael));
// 4b2b30e27699979ce46714253bc2213010db039c
console.log(objectHash(bob));
// 38d96106bc8ef3d8bd369b99bb6972702c9826d5

/***
 * hash object keys, values ignored
 */
console.log(objectHash(peter, { excludeValues: true }));
// 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
console.log(objectHash(michael, { excludeValues: true }));
// 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
console.log(objectHash(bob, { excludeValues: true }));
// 48f370a772c7496f6c9d2e6d92e920c87dd00a5c

/***
 * md5 binary encoding
 */
console.log(objectHash(peter, { algorithm: 'md5', encoding: 'base64' }));
// 6rkWaaDiG3NynWw4svGH7g==
console.log(objectHash(michael, { algorithm: 'md5', encoding: 'base64' }));
// djXaWpuWVJeOF8Sb6SFFNg==
console.log(objectHash(bob, { algorithm: 'md5', encoding: 'base64' }));
// lFzkw/IJ8/12jZI0rQeS3w==

Options

{
	algorithm: '<sha1(default)|sha|md5|...>',
	encoding: '<hex(default)|buffer|binary|base64>',
	excludeValues: <false(default)|true>
}

Development

git clone https://github.com/puleos/object-hash

gulp tasks

  • gulp watch (default) watch files, test and lint on change/add
  • gulp test unit tests
  • gulp lint jshint
  • gulp dist create browser version in /dest

License

MIT

Keywords

FAQs

Last updated on 07 Mar 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc