UNPKG

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