You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

453 lines
15 KiB

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