1 | 'use strict';
|
2 |
|
3 | var crypto = require('crypto');
|
4 | var stream = require('stream');
|
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.respectFunctionProperties = options.respectFunctionProperties === false ? false : true;
|
70 | options.unorderedArrays = options.unorderedArrays !== true ? false : true;
|
71 | options.unorderedSets = options.unorderedSets === false ? false : true;
|
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 |
|
80 |
|
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 |
|
102 | function 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 |
|
110 | function 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('');
|
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 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | exports.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 |
|
160 | function 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) {
|
181 | objType = 'unknown:[' + objString + ']';
|
182 | } else {
|
183 | objType = objType[1];
|
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 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
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;
|
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 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | var contextAdditions = [];
|
256 | var entries = arr.map(function(entry) {
|
257 | var strm = new stream.PassThrough();
|
258 | var localContext = context.slice();
|
259 | var hasher = typeHasher(options, strm, localContext);
|
260 | hasher.dispatch(entry);
|
261 |
|
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 |
|
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 | }
|