1 | 'use strict';
|
2 |
|
3 | var crypto = require('crypto');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | exports = module.exports = objectHash;
|
29 |
|
30 | function objectHash(object, options){
|
31 | options = applyDefaults(object, options);
|
32 |
|
33 | return hash(object, options);
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | exports.sha1 = function(object){
|
44 | return objectHash(object);
|
45 | };
|
46 | exports.keys = function(object){
|
47 | return objectHash(object, {excludeValues: true, algorithm: 'sha1', encoding: 'hex'});
|
48 | };
|
49 | exports.MD5 = function(object){
|
50 | return objectHash(object, {algorithm: 'md5', encoding: 'hex'});
|
51 | };
|
52 | exports.keysMD5 = function(object){
|
53 | return objectHash(object, {algorithm: 'md5', encoding: 'hex', excludeValues: true});
|
54 | };
|
55 |
|
56 |
|
57 | function 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;
|
68 | options.respectType = options.respectType === false ? false : 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;
|
72 | options.unorderedSets = options.unorderedSets === false ? false : true;
|
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 |
|
81 |
|
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 |
|
103 | function 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 |
|
111 | function 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('');
|
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 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | exports.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 |
|
161 | function 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 |
|
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) {
|
184 | objType = 'unknown:[' + objString + ']';
|
185 | } else {
|
186 | objType = objType[1];
|
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 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
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;
|
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 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | var contextAdditions = [];
|
259 | var entries = arr.map(function(entry) {
|
260 | var strm = new PassThrough();
|
261 | var localContext = context.slice();
|
262 | var hasher = typeHasher(options, strm, localContext);
|
263 | hasher.dispatch(entry);
|
264 |
|
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 |
|
298 |
|
299 |
|
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 |
|
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 |
|
407 |
|
408 |
|
409 |
|
410 | function 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 | }
|