# es-hash

> Small library to create hashes from objects

Latest version **1.0.4** (published 2013-05-03) · 0 weekly downloads

## Install

```sh
npm install es-hash
pnpm add es-hash
yarn add es-hash
bun add es-hash
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2013-05-03 |
| First published | 2013-01-22 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Till Ehrengruber |
| Maintainers | tehrengruber |
| Keywords | hash, object hash, object array key |

## Links

- npm: https://www.npmjs.com/package/es-hash
- Repository: https://bitbucket.org/tehrengruber/es-js-hash
- npm.io page: https://npm.io/package/es-hash

## Dependencies (1)

- [node_hash](https://npm.io/package/node_hash.md) >=0.2.0

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2013-05-03
- 1.0.3 — 2013-01-22
- 1.0.2 — 2013-01-22
- 1.0.1 — 2013-01-22
- 1.0.0 — 2013-01-22

## README

This is a small module to create hashes from simple objects. The ECMA Standart defines Javascript Objects as unordered,
so you can't just iterate over the object and save a hash for each key value pair, you have to sort it. This module
does that for you so you can easily compare objects that have identical content but are ordered differently.

# Supported Hashing algorithms
djb2, md5, sha1, sha256, sha512, ripemd160

Default: djb2

# Installation
Node-JS:
```
$ npm install es-hash
```

Browser:
You can easily adapt this package for usage in the browser (e.q. RequireJS Optimizer, Cajon, ...). Keep in mind
that all hashing algorithms except of djb2 depend on node_hash which itself uses nodes crypto library, so you can
only use djb2 in the browser or implement other algorithms.

# Example

``` javascript
var hash = require('es-hash');

// Create two objects with identical content in a different order
var obj1 = { bla: 'blub', blub: 'bla' },
    obj2 = { blub: 'bla', bla: 'blub' };

// Output hashes
console.log('Obj1 - ' + hash(obj1));
console.log('Obj2 - ' + hash(obj2));

// Output hashes as md5
console.log('Obj1 - ' + hash(obj1, 'md5'));
console.log('Obj2 - ' + hash(obj2, 'md5'));
```

# Using Objects as Array keys
You may face the problem that array keys in javascript are always strings. So if you use an object as key, the resulting
key will be [object Object]. Thats not what we intended to do, as you can see in the following example.

``` javascript
var foo = {};

foo[{bar: 'foo'}] = 'foo';

/*
 * Expected output:
 *  foo
 *  undefined
 *
 * Actual output:
 *  foo
 *  foo
 */
console.log(foo[{bar: 'foo'}]);
console.log(foo[{}]);
```

You can still use the same syntax if you just override Object.toString and return a hash for the current object.

``` javascript
var hash = require('es-hash');

// Save data in an object with an object as a key
Object.prototype.toString = function () {
    return '[object Object #'+hash(this)+']';
}

var foo = {};

foo[{bar: 'foo'}] = 'foo';

/*
 * Output:
 *  foo
 *  undefined
 */
console.log(foo[{bar: 'foo'}]);
console.log(foo[{}]);
```

# Unit Tests
```
$ nodeunit test.js
```

Expected output:
```
test.js
✔ testHash
✔ testMd5Hash

OK: 2 assertions (18ms)
```

# License
(The MIT License)

Copyright (C) 2012 Till Ehrengruber

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

---
_Source: https://npm.io/package/es-hash · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
