1 | # object-hash
|
2 |
|
3 | Generate hashes from objects and values in node and the browser. Uses node.js
|
4 | crypto module for hashing. Supports SHA1 and many others (depending on the platform)
|
5 | as well as custom streams (e.g. CRC32).
|
6 |
|
7 | [![NPM](https://nodei.co/npm/object-hash.png?downloads=true&downloadRank=true)](https://www.npmjs.com/package/object-hash)
|
8 |
|
9 | [![Travis CI](https://secure.travis-ci.org/puleos/object-hash.png?branch=master)](https://secure.travis-ci.org/puleos/object-hash?branch=master)
|
10 | [![Coverage Status](https://coveralls.io/repos/puleos/object-hash/badge.svg?branch=master&service=github)](https://coveralls.io/github/puleos/object-hash?branch=master)
|
11 |
|
12 | * Hash values of any type.
|
13 | * Supports a keys only option for grouping similar objects with different values.
|
14 |
|
15 | ```js
|
16 | var hash = require('object-hash');
|
17 |
|
18 | hash({foo: 'bar'}) // => '67b69634f9880a282c14a0f0cb7ba20cf5d677e9'
|
19 | hash([1, 2, 2.718, 3.14159]) // => '136b9b88375971dff9f1af09d7356e3e04281951'
|
20 | ```
|
21 |
|
22 | ## Versioning Disclaimer
|
23 |
|
24 | Starting with version `1.1.8` (released April 2017), new versions will consider
|
25 | the exact returned hash part of the API contract, i.e. changes that will affect
|
26 | hash values will be considered `semver-major`. Previous versions may violate
|
27 | that expectation.
|
28 |
|
29 | For more information, see [this discussion](https://github.com/puleos/object-hash/issues/30).
|
30 |
|
31 | ## hash(value, options)
|
32 |
|
33 | Generate a hash from any object or type. Defaults to sha1 with hex encoding.
|
34 |
|
35 | * `algorithm` hash algo to be used: 'sha1', 'md5', 'passthrough'. default: sha1
|
36 | * This supports the algorithms returned by `crypto.getHashes()`. Note that the default of SHA-1 is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
|
37 | * This also supports the `passthrough` algorith, which will return the information that would otherwise have been hashed.
|
38 | * `excludeValues` {true|false} hash object keys, values ignored. default: false
|
39 | * `encoding` hash encoding, supports 'buffer', 'hex', 'binary', 'base64'. default: hex
|
40 | * `ignoreUnknown` {true|*false} ignore unknown object types. default: false
|
41 | * `replacer` optional function that replaces values before hashing. default: accept all values
|
42 | * `respectFunctionProperties` {true|false} Whether properties on functions are considered when hashing. default: true
|
43 | * `respectFunctionNames` {true|false} consider `name` property of functions for hashing. default: true
|
44 | * `respectType` {true|false} Whether special type attributes (`.prototype`, `.__proto__`, `.constructor`)
|
45 | are hashed. default: true
|
46 | * `unorderedArrays` {true|false} Sort all arrays before hashing. Note that this affects *all* collections,
|
47 | i.e. including typed arrays, Sets, Maps, etc. default: false
|
48 | * `unorderedSets` {true|false} Sort `Set` and `Map` instances before hashing, i.e. make
|
49 | `hash(new Set([1, 2])) == hash(new Set([2, 1]))` return `true`. default: true
|
50 | * `unorderedObjects` {true|false} Sort objects before hashing, i.e. make `hash({ x: 1, y: 2 }) === hash({ y: 2, x: 1 })`. default: true
|
51 | * `excludeKeys` optional function for excluding specific key(s) from hashing, if true is returned then exclude from hash. default: include all keys
|
52 |
|
53 | ## hash.sha1(value)
|
54 |
|
55 | Hash using the sha1 algorithm.
|
56 |
|
57 | Note that SHA-1 is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
|
58 |
|
59 | *Sugar method, equivalent to* `hash(value, {algorithm: 'sha1'})`
|
60 |
|
61 | ## hash.keys(value)
|
62 |
|
63 | Hash object keys using the sha1 algorithm, values ignored.
|
64 |
|
65 | *Sugar method, equivalent to* `hash(value, {excludeValues: true})`
|
66 |
|
67 | ## hash.MD5(value)
|
68 |
|
69 | Hash using the md5 algorithm.
|
70 |
|
71 | Note that the MD5 algorithm is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
|
72 |
|
73 | *Sugar method, equivalent to* `hash(value, {algorithm: 'md5'})`
|
74 |
|
75 | ## hash.keysMD5(value)
|
76 |
|
77 | Hash object keys using the md5 algorithm, values ignored.
|
78 |
|
79 | Note that the MD5 algorithm is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
|
80 |
|
81 | *Sugar method, equivalent to* `hash(value, {algorithm: 'md5', excludeValues: true})`
|
82 |
|
83 | ## hash.writeToStream(value, [options,] stream)
|
84 |
|
85 | Write the information that would otherwise have been hashed to a stream, e.g.:
|
86 |
|
87 | ```js
|
88 | hash.writeToStream({foo: 'bar', a: 42}, {respectType: false}, process.stdout)
|
89 | // => e.g. 'object:a:number:42foo:string:bar'
|
90 | ```
|
91 |
|
92 | ## Installation
|
93 |
|
94 | node:
|
95 |
|
96 | ```js
|
97 | npm install object-hash
|
98 | ```
|
99 |
|
100 | browser: */dist/object_hash.js*
|
101 |
|
102 | ```html
|
103 | <script src="object_hash.js" type="text/javascript"></script>
|
104 |
|
105 | <script>
|
106 | var hash = objectHash.sha1({foo:'bar'});
|
107 |
|
108 | console.log(hash); // e003c89cdf35cdf46d8239b4692436364b7259f9
|
109 | </script>
|
110 | ```
|
111 |
|
112 | ## Example usage
|
113 |
|
114 | ```js
|
115 | var hash = require('object-hash');
|
116 |
|
117 | var peter = { name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };
|
118 | var michael = { name: 'Michael', stapler: false, friends: ['Peter', 'Samir'] };
|
119 | var bob = { name: 'Bob', stapler: true, friends: [] };
|
120 |
|
121 | /***
|
122 | * sha1 hex encoding (default)
|
123 | */
|
124 | hash(peter);
|
125 | // 14fa461bf4b98155e82adc86532938553b4d33a9
|
126 | hash(michael);
|
127 | // 4b2b30e27699979ce46714253bc2213010db039c
|
128 | hash(bob);
|
129 | // 38d96106bc8ef3d8bd369b99bb6972702c9826d5
|
130 |
|
131 | /***
|
132 | * hash object keys, values ignored
|
133 | */
|
134 | hash(peter, { excludeValues: true });
|
135 | // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
|
136 | hash(michael, { excludeValues: true });
|
137 | // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
|
138 | hash.keys(bob);
|
139 | // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
|
140 |
|
141 | /***
|
142 | * hash object, ignore specific key(s)
|
143 | */
|
144 | hash(peter, { excludeKeys: function(key) {
|
145 | if ( key === 'friends') {
|
146 | return true;
|
147 | }
|
148 | return false;
|
149 | }
|
150 | });
|
151 | // 66b7d7e64871aa9fda1bdc8e88a28df797648d80
|
152 |
|
153 | /***
|
154 | * md5 base64 encoding
|
155 | */
|
156 | hash(peter, { algorithm: 'md5', encoding: 'base64' });
|
157 | // 6rkWaaDiG3NynWw4svGH7g==
|
158 | hash(michael, { algorithm: 'md5', encoding: 'base64' });
|
159 | // djXaWpuWVJeOF8Sb6SFFNg==
|
160 | hash(bob, { algorithm: 'md5', encoding: 'base64' });
|
161 | // lFzkw/IJ8/12jZI0rQeS3w==
|
162 | ```
|
163 |
|
164 | ## Legacy Browser Support
|
165 |
|
166 | IE <= 8 and Opera <= 11 support dropped in version 0.3.0. If you require
|
167 | legacy browser support you must either use an ES5 shim or use version 0.2.5
|
168 | of this module.
|
169 |
|
170 | ## Development
|
171 |
|
172 | ```sh-session
|
173 | git clone https://github.com/puleos/object-hash
|
174 | ```
|
175 |
|
176 | ## Node Docker Wrapper
|
177 |
|
178 | If you want to stand this up in a docker container, you should take at look
|
179 | at the [![node-object-hash](https://github.com/bean5/node-object-hash)](https://github.com/bean5/node-object-hash) project.
|
180 |
|
181 | ### gulp tasks
|
182 |
|
183 | * `gulp watch` (default) watch files, test and lint on change/add
|
184 | * `gulp test` unit tests
|
185 | * `gulp karma` browser unit tests
|
186 | * `gulp lint` jshint
|
187 | * `gulp dist` create browser version in /dist
|
188 |
|
189 | ## License
|
190 |
|
191 | MIT
|
192 |
|
193 | ## Changelog
|
194 |
|
195 | ### v2.0.0
|
196 |
|
197 | Only Node.js versions `>= 6.0.0` are being tested in CI now.
|
198 | No other breaking changes were introduced.
|