UNPKG

13.7 kBJavaScriptView Raw
1'use strict';
2
3var crypto = require('crypto');
4var stream = require('stream');
5
6/**
7 * Exported function
8 *
9 * Options:
10 *
11 * - `algorithm` hash algo to be used by this instance: *'sha1', 'md5'
12 * - `excludeValues` {true|*false} hash object keys, values ignored
13 * - `encoding` hash encoding, supports 'buffer', '*hex', 'binary', 'base64'
14 * - `ignoreUnknown` {true|*false} ignore unknown object types
15 * - `replacer` optional function that replaces values before hashing
16 * - `respectFunctionProperties` {*true|false} consider function properties when hashing
17 * - `respectType` {*true|false} Respect special properties (prototype, constructor)
18 * when hashing to distinguish between types
19 * - `unorderedArrays` {true|*false} Sort all arrays before hashing
20 * - `unorderedSets` {*true|false} Sort `Set` and `Map` instances before hashing
21 * * = default
22 *
23 * @param {object} object value to hash
24 * @param {object} options hashing options
25 * @return {string} hash value
26 * @api public
27 */
28exports = module.exports = objectHash;
29
30function objectHash(object, options){
31 options = applyDefaults(object, options);
32
33 return hash(object, options);
34}
35
36/**
37 * Exported sugar methods
38 *
39 * @param {object} object value to hash
40 * @return {string} hash value
41 * @api public
42 */
43exports.sha1 = function(object){
44 return objectHash(object);
45};
46exports.keys = function(object){
47 return objectHash(object, {excludeValues: true, algorithm: 'sha1', encoding: 'hex'});
48};
49exports.MD5 = function(object){
50 return objectHash(object, {algorithm: 'md5', encoding: 'hex'});
51};
52exports.keysMD5 = function(object){
53 return objectHash(object, {algorithm: 'md5', encoding: 'hex', excludeValues: true});
54};
55
56// Internals
57function applyDefaults(object, options){
58 var hashes = crypto.getHashes ? crypto.getHashes() : ['sha1', 'md5'];
59 var encodings = ['buffer', 'hex', 'binary', 'base64'];
60
61 options = options || {};
62 options.algorithm = options.algorithm || 'sha1';
63 options.encoding = options.encoding || 'hex';
64 options.excludeValues = options.excludeValues ? true : false;
65 options.algorithm = options.algorithm.toLowerCase();
66 options.encoding = options.encoding.toLowerCase();
67 options.ignoreUnknown = options.ignoreUnknown !== true ? false : true; // default to false
68 options.respectType = options.respectType === false ? false : true; // default to true
69 options.respectFunctionProperties = options.respectFunctionProperties === false ? false : true;
70 options.unorderedArrays = options.unorderedArrays !== true ? false : true; // default to false
71 options.unorderedSets = options.unorderedSets === false ? false : true; // default to false
72 options.replacer = options.replacer || undefined;
73
74 if(typeof object === 'undefined') {
75 throw new Error('Object argument required.');
76 }
77
78 hashes.push('passthrough');
79 // if there is a case-insensitive match in the hashes list, accept it
80 // (i.e. SHA256 for sha256)
81 for (var i = 0; i < hashes.length; ++i) {
82 if (hashes[i].toLowerCase() === options.algorithm.toLowerCase()) {
83 options.algorithm = hashes[i];
84 }
85 }
86
87 if(hashes.indexOf(options.algorithm) === -1){
88 throw new Error('Algorithm "' + options.algorithm + '" not supported. ' +
89 'supported values: ' + hashes.join(', '));
90 }
91
92 if(encodings.indexOf(options.encoding) === -1 &&
93 options.algorithm !== 'passthrough'){
94 throw new Error('Encoding "' + options.encoding + '" not supported. ' +
95 'supported values: ' + encodings.join(', '));
96 }
97
98 return options;
99}
100
101/** Check if the given function is a native function */
102function isNativeFunction(f) {
103 if ((typeof f) !== 'function') {
104 return false;
105 }
106 var exp = /^function\s+\w*\s*\(\s*\)\s*{\s+\[native code\]\s+}$/i;
107 return exp.exec(Function.prototype.toString.call(f)) != null;
108}
109
110function hash(object, options) {
111 var hashingStream;
112
113 if (options.algorithm !== 'passthrough') {
114 hashingStream = crypto.createHash(options.algorithm);
115 } else {
116 hashingStream = new stream.PassThrough();
117 }
118
119 if (typeof hashingStream.write === 'undefined') {
120 hashingStream.write = hashingStream.update;
121 hashingStream.end = hashingStream.update;
122 }
123
124 var hasher = typeHasher(options, hashingStream);
125 hasher.dispatch(object);
126 hashingStream.end(''); // write empty string since .update() requires a string arg
127
128 if (typeof hashingStream.read === 'undefined' &&
129 typeof hashingStream.digest === 'function') {
130 return hashingStream.digest(options.encoding === 'buffer' ? undefined : options.encoding);
131 }
132
133 var buf = hashingStream.read();
134 if (options.encoding === 'buffer') {
135 return buf;
136 }
137
138 return buf.toString(options.encoding);
139}
140
141/**
142 * Expose streaming API
143 *
144 * @param {object} object Value to serialize
145 * @param {object} options Options, as for hash()
146 * @param {object} stream A stream to write the serializiation to
147 * @api public
148 */
149exports.writeToStream = function(object, options, stream) {
150 if (typeof stream === 'undefined') {
151 stream = options;
152 options = {};
153 }
154
155 options = applyDefaults(object, options);
156
157 return typeHasher(options, stream).dispatch(object);
158};
159
160function typeHasher(options, writeTo, context){
161 context = context || [];
162
163 return {
164 dispatch: function(value){
165 if (options.replacer) {
166 value = options.replacer(value);
167 }
168
169 var type = typeof value;
170 if (value === null) {
171 type = 'null';
172 }
173
174 return this['_' + type](value);
175 },
176 _object: function(object) {
177 var pattern = (/\[object (.*)\]/i);
178 var objString = Object.prototype.toString.call(object);
179 var objType = pattern.exec(objString);
180 if (!objType) { // object type did not match [object ...]
181 objType = 'unknown:[' + objString + ']';
182 } else {
183 objType = objType[1]; // take only the class name
184 }
185
186 objType = objType.toLowerCase();
187
188 var objectNumber = null;
189
190 if ((objectNumber = context.indexOf(object)) >= 0) {
191 return this.dispatch('[CIRCULAR:' + objectNumber + ']');
192 } else {
193 context.push(object);
194 }
195
196 if (typeof Buffer !== 'undefined' && Buffer.isBuffer && Buffer.isBuffer(object)) {
197 writeTo.write('buffer:');
198 return writeTo.write(object);
199 }
200
201 if(objType !== 'object' && objType !== 'function') {
202 if(this['_' + objType]) {
203 this['_' + objType](object);
204 } else if (options.ignoreUnknown) {
205 return writeTo.write('[' + objType + ']');
206 } else {
207 throw new Error('Unknown object type "' + objType + '"');
208 }
209 }else{
210 var keys = Object.keys(object).sort();
211 // Make sure to incorporate special properties, so
212 // Types with different prototypes will produce
213 // a different hash and objects derived from
214 // different functions (`new Foo`, `new Bar`) will
215 // produce different hashes.
216 // We never do this for native functions since some
217 // seem to break because of that.
218 if (options.respectType !== false && !isNativeFunction(object)) {
219 keys.splice(0, 0, 'prototype', '__proto__', 'constructor');
220 }
221
222 writeTo.write('object:' + keys.length + ':');
223 var self = this;
224 return keys.forEach(function(key){
225 self.dispatch(key);
226 writeTo.write(':');
227 if(!options.excludeValues) {
228 self.dispatch(object[key]);
229 }
230 writeTo.write(',');
231 });
232 }
233 },
234 _array: function(arr, unordered){
235 unordered = typeof unordered !== 'undefined' ? unordered :
236 options.unorderedArrays !== false; // default to options.unorderedArrays
237
238 var self = this;
239 writeTo.write('array:' + arr.length + ':');
240 if (!unordered) {
241 return arr.forEach(function(entry) {
242 return self.dispatch(entry);
243 });
244 }
245
246 // the unordered case is a little more complicated:
247 // since there is no canonical ordering on objects,
248 // i.e. {a:1} < {a:2} and {a:1} > {a:2} are both false,
249 // we first serialize each entry using a PassThrough stream
250 // before sorting.
251 // also: we can’t use the same context array for all entries
252 // since the order of hashing should *not* matter. instead,
253 // we keep track of the additions to a copy of the context array
254 // and add all of them to the global context array when we’re done
255 var contextAdditions = [];
256 var entries = arr.map(function(entry) {
257 var strm = new stream.PassThrough();
258 var localContext = context.slice(); // make copy
259 var hasher = typeHasher(options, strm, localContext);
260 hasher.dispatch(entry);
261 // take only what was added to localContext and append it to contextAdditions
262 contextAdditions = contextAdditions.concat(localContext.slice(context.length));
263 return strm.read().toString();
264 });
265 context = context.concat(contextAdditions);
266 entries.sort();
267 return this._array(entries, false);
268 },
269 _date: function(date){
270 return writeTo.write('date:' + date.toJSON());
271 },
272 _symbol: function(sym){
273 return writeTo.write('symbol:' + sym.toString(), 'utf8');
274 },
275 _error: function(err){
276 return writeTo.write('error:' + err.toString(), 'utf8');
277 },
278 _boolean: function(bool){
279 return writeTo.write('bool:' + bool.toString());
280 },
281 _string: function(string){
282 writeTo.write('string:' + string.length + ':');
283 writeTo.write(string, 'utf8');
284 },
285 _function: function(fn){
286 writeTo.write('fn:');
287 if (isNativeFunction(fn)) {
288 this.dispatch('[native]');
289 } else {
290 this.dispatch(fn.toString());
291 }
292
293 if (options.respectFunctionProperties) {
294 this._object(fn);
295 }
296 },
297 _number: function(number){
298 return writeTo.write('number:' + number.toString());
299 },
300 _xml: function(xml){
301 return writeTo.write('xml:' + xml.toString(), 'utf8');
302 },
303 _null: function() {
304 return writeTo.write('Null');
305 },
306 _undefined: function() {
307 return writeTo.write('Undefined');
308 },
309 _regexp: function(regex){
310 return writeTo.write('regex:' + regex.toString(), 'utf8');
311 },
312 _uint8array: function(arr){
313 writeTo.write('uint8array:');
314 return this.dispatch(Array.prototype.slice.call(arr));
315 },
316 _uint8clampedarray: function(arr){
317 writeTo.write('uint8clampedarray:');
318 return this.dispatch(Array.prototype.slice.call(arr));
319 },
320 _int8array: function(arr){
321 writeTo.write('uint8array:');
322 return this.dispatch(Array.prototype.slice.call(arr));
323 },
324 _uint16array: function(arr){
325 writeTo.write('uint16array:');
326 return this.dispatch(Array.prototype.slice.call(arr));
327 },
328 _int16array: function(arr){
329 writeTo.write('uint16array:');
330 return this.dispatch(Array.prototype.slice.call(arr));
331 },
332 _uint32array: function(arr){
333 writeTo.write('uint32array:');
334 return this.dispatch(Array.prototype.slice.call(arr));
335 },
336 _int32array: function(arr){
337 writeTo.write('uint32array:');
338 return this.dispatch(Array.prototype.slice.call(arr));
339 },
340 _float32array: function(arr){
341 writeTo.write('float32array:');
342 return this.dispatch(Array.prototype.slice.call(arr));
343 },
344 _float64array: function(arr){
345 writeTo.write('float64array:');
346 return this.dispatch(Array.prototype.slice.call(arr));
347 },
348 _arraybuffer: function(arr){
349 writeTo.write('arraybuffer:');
350 return this.dispatch(new Uint8Array(arr));
351 },
352 _url: function(url) {
353 return writeTo.write('url:' + url.toString(), 'utf8');
354 },
355 _map: function(map) {
356 writeTo.write('map:');
357 var arr = Array.from(map);
358 return this._array(arr, options.unorderedSets !== false);
359 },
360 _set: function(set) {
361 writeTo.write('set:');
362 var arr = Array.from(set);
363 return this._array(arr, options.unorderedSets !== false);
364 },
365 _blob: function() {
366 if (options.ignoreUnknown) {
367 return writeTo.write('[blob]');
368 }
369
370 throw Error('Hashing Blob objects is currently not supported\n' +
371 '(see https://github.com/puleos/object-hash/issues/26)\n' +
372 'Use "options.replacer" or "options.ignoreUnknown"\n');
373 },
374 _domwindow: function() { return writeTo.write('domwindow'); },
375 /* Node.js standard native objects */
376 _process: function() { return writeTo.write('process'); },
377 _timer: function() { return writeTo.write('timer'); },
378 _pipe: function() { return writeTo.write('pipe'); },
379 _tcp: function() { return writeTo.write('tcp'); },
380 _udp: function() { return writeTo.write('udp'); },
381 _tty: function() { return writeTo.write('tty'); },
382 _statwatcher: function() { return writeTo.write('statwatcher'); },
383 _securecontext: function() { return writeTo.write('securecontext'); },
384 _connection: function() { return writeTo.write('connection'); },
385 _zlib: function() { return writeTo.write('zlib'); },
386 _context: function() { return writeTo.write('context'); },
387 _nodescript: function() { return writeTo.write('nodescript'); },
388 _httpparser: function() { return writeTo.write('httpparser'); },
389 _dataview: function() { return writeTo.write('dataview'); },
390 _signal: function() { return writeTo.write('signal'); },
391 _fsevent: function() { return writeTo.write('fsevent'); },
392 _tlswrap: function() { return writeTo.write('tlswrap'); }
393 };
394}