index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. 'use strict';
  2. var assert = require('assert');
  3. var crypto = require('crypto');
  4. var hash = require('../index');
  5. var validSha1 = /^[0-9a-f]{40}$/i;
  6. describe('hash', function() {
  7. it('throws when nothing to hash', function () {
  8. assert.throws(hash, 'no arguments');
  9. assert.throws(function() {
  10. hash(undefined, {algorithm: 'md5'});
  11. }, 'undefined');
  12. });
  13. it('throws when passed an invalid options', function() {
  14. assert.throws(function() {
  15. hash({foo: 'bar'}, {algorithm: 'shalala'});
  16. }, 'bad algorithm');
  17. assert.throws(function() {
  18. hash({foo: 'bar'}, {encoding: 'base16'});
  19. }, 'bad encoding');
  20. });
  21. it('hashes a simple object', function() {
  22. assert.ok(validSha1.test(hash({foo: 'bar', bar: 'baz'})), 'hash object');
  23. });
  24. if (typeof Buffer !== 'undefined') {
  25. it('can return buffers', function() {
  26. assert.ok(Buffer.isBuffer(hash({foo: 'bar', bar: 'baz'}, {encoding: 'buffer'})), 'hash object');
  27. });
  28. }
  29. it('hashes identical objects with different key ordering', function() {
  30. var hash1 = hash({foo: 'bar', bar: 'baz'});
  31. var hash2 = hash({bar: 'baz', foo: 'bar'});
  32. var hash3 = hash({bar: 'foo', foo: 'baz'});
  33. assert.equal(hash1, hash2, 'hashes are equal');
  34. assert.notEqual(hash1, hash3, 'different objects not equal');
  35. });
  36. it('respects object key ordering when unorderedObjects = false', function() {
  37. var hash1 = hash({foo: 'bar', bar: 'baz'}, { unorderedObjects: false });
  38. var hash2 = hash({bar: 'baz', foo: 'bar'}, { unorderedObjects: false });
  39. assert.notEqual(hash1, hash2, 'hashes are not equal');
  40. });
  41. it('hashes only object keys when excludeValues option is set', function() {
  42. var hash1 = hash({foo: false, bar: 'OK'}, { excludeValues: true });
  43. var hash2 = hash({foo: true, bar: 'NO'}, { excludeValues: true });
  44. var hash3 = hash({foo: true, bar: 'OK', baz: false}, { excludeValues: true });
  45. assert.equal(hash1, hash2, 'values not in hash digest');
  46. assert.notEqual(hash1, hash3, 'different keys not equal');
  47. });
  48. it('array values are hashed', function() {
  49. var hash1 = hash({foo: ['bar', 'baz'], bax: true });
  50. var hash2 = hash({foo: ['baz', 'bar'], bax: true });
  51. assert.notEqual(hash1, hash2, 'different array orders are unique');
  52. });
  53. it('nested object values are hashed', function() {
  54. var hash1 = hash({foo: {bar: true, bax: 1}});
  55. var hash2 = hash({foo: {bar: true, bax: 1}});
  56. var hash3 = hash({foo: {bar: false, bax: 1}});
  57. assert.equal(hash1, hash2, 'hashes are equal');
  58. assert.notEqual(hash1, hash3, 'different objects not equal');
  59. });
  60. it('sugar methods should be equivalent', function() {
  61. var obj = {foo: 'bar', baz: true};
  62. assert.equal(hash.keys(obj), hash(obj, {excludeValues: true}), 'keys');
  63. assert.equal(hash.sha1(obj), hash(obj, {algorithm: 'sha1'}), 'sha1');
  64. assert.equal(hash.MD5(obj), hash(obj, {algorithm: 'md5'}), 'md5');
  65. assert.equal(hash.keysMD5(obj),
  66. hash(obj, {algorithm: 'md5', excludeValues: true}), 'keys md5');
  67. });
  68. it('array of nested object values are hashed', function() {
  69. var hash1 = hash({foo: [ {bar: true, bax: 1}, {bar: false, bax: 2} ] });
  70. var hash2 = hash({foo: [ {bar: true, bax: 1}, {bar: false, bax: 2} ] });
  71. var hash3 = hash({foo: [ {bar: false, bax: 2} ] });
  72. assert.equal(hash1, hash2, 'hashes are equal');
  73. assert.notEqual(hash1, hash3, 'different objects not equal');
  74. });
  75. it("recursive objects don't blow up stack", function() {
  76. var hash1 = {foo: 'bar'};
  77. hash1.recursive = hash1;
  78. assert.doesNotThrow(function() {hash(hash1);}, /Maximum call stack size exceeded/, 'Should not throw an stack size exceeded exception');
  79. });
  80. it("recursive arrays don't blow up stack", function() {
  81. var hash1 = ['foo', 'bar'];
  82. hash1.push(hash1);
  83. assert.doesNotThrow(function() {hash(hash1);}, /Maximum call stack size exceeded/, 'Should not throw an stack size exceeded exception');
  84. });
  85. it("recursive arrays don't blow up stack with unorderedArrays", function() {
  86. var hash1 = ['foo', 'bar'];
  87. hash1.push(hash1);
  88. assert.doesNotThrow(function() {hash(hash1, {unorderedArrays: true});}, /Maximum call stack size exceeded/, 'Should not throw an stack size exceeded exception');
  89. });
  90. it("recursive handling tracks identity", function() {
  91. var hash1 = {k1: {k: 'v'}, k2: {k: 'k2'}};
  92. hash1.k1.r1 = hash1.k1;
  93. hash1.k2.r2 = hash1.k2;
  94. var hash2 = {k1: {k: 'v'}, k2: {k: 'k2'}};
  95. hash2.k1.r1 = hash2.k2;
  96. hash2.k2.r2 = hash2.k1;
  97. assert.notEqual(hash(hash1), hash(hash2), "order of recursive objects should matter");
  98. });
  99. it("object types are hashed", function() {
  100. var hash1 = hash({foo: 'bar'});
  101. var hash2 = hash(['foo', 'bar']);
  102. assert.notEqual(hash1, hash2, "arrays and objects should not produce identical hashes");
  103. });
  104. it("utf8 strings are hashed correctly", function() {
  105. var hash1 = hash('\u03c3'); // cf 83 in utf8
  106. var hash2 = hash('\u01c3'); // c7 83 in utf8
  107. assert.notEqual(hash1, hash2, "different strings with similar utf8 encodings should produce different hashes");
  108. });
  109. it("strings passed as new String are hashed correctly", function() {
  110. var hash1 = hash(new String('foo'));
  111. assert.equal(hash1, '7cd3edacc4c9dd43908177508c112608a398bb9a');
  112. var hash2 = hash({foo: new String('bar')});
  113. assert.equal(hash2, 'a75c05bdca7d704bdfcd761913e5a4e4636e956b');
  114. });
  115. it("various hashes in crypto.getHashes() should be supported", function() {
  116. var hashes = ['sha1', 'md5'];
  117. if (crypto.getHashes) {
  118. // take all hashes from crypto.getHashes() starting with MD or SHA
  119. hashes = crypto.getHashes().filter(RegExp.prototype.test.bind(/^(md|sha)/i));
  120. }
  121. var obj = {randomText: 'bananas'};
  122. for (var i = 0; i < hashes.length; i++) {
  123. assert.ok(hash(obj, {algorithm: hashes[i]}), 'Algorithm ' + hashes[i] + ' should be supported');
  124. }
  125. });
  126. it("null and 'Null' string produce different hashes", function() {
  127. var hash1 = hash({foo: null});
  128. var hash2 = hash({foo: 'Null'});
  129. assert.notEqual(hash1, hash2, "null and 'Null' should not produce identical hashes");
  130. });
  131. it('Distinguish functions based on their properties', function() {
  132. var a, b, c, d;
  133. function Foo() {}
  134. a = hash(Foo);
  135. Foo.foo = 22;
  136. b = hash(Foo);
  137. Foo.bar = "42";
  138. c = hash(Foo);
  139. Foo.foo = "22";
  140. d = hash(Foo);
  141. assert.notEqual(a,b, 'adding a property changes the hash');
  142. assert.notEqual(b,c, 'adding another property changes the hash');
  143. assert.notEqual(c,d, 'changing a property changes the hash');
  144. });
  145. it('respectFunctionProperties = false', function() {
  146. var a, b;
  147. function Foo() {}
  148. a = hash(Foo, {respectFunctionProperties: false});
  149. Foo.foo = 22;
  150. b = hash(Foo, {respectFunctionProperties: false});
  151. assert.equal(a,b, 'function properties are ignored');
  152. });
  153. it('Distinguish functions based on prototype properties', function() {
  154. var a, b, c, d;
  155. function Foo() {}
  156. a = hash(Foo);
  157. Foo.prototype.foo = 22;
  158. b = hash(Foo);
  159. Foo.prototype.bar = "42";
  160. c = hash(Foo);
  161. Foo.prototype.foo = "22";
  162. d = hash(Foo);
  163. assert.notEqual(a,b, 'adding a property to the prototype changes the hash');
  164. assert.notEqual(b,c, 'adding another property to the prototype changes the hash');
  165. assert.notEqual(c,d, 'changing a property in the prototype changes the hash');
  166. });
  167. it('Distinguish objects based on their type', function() {
  168. function Foo() {}
  169. function Bar() {}
  170. var f = new Foo(), b = new Bar();
  171. assert.notEqual(hash(Foo), hash(Bar), 'Functions with different names should produce a different Hash.');
  172. assert.notEqual(hash(f), hash(b), 'Objects with different constructor should have a different Hash.');
  173. });
  174. it('respectType = false', function() {
  175. var opt = { respectType: false };
  176. function Foo() {}
  177. function Bar() {}
  178. var f = new Foo(), b = new Bar();
  179. assert.equal(hash(f, opt), hash(b, opt), 'Hashing should disregard the different constructor');
  180. var ha, hb;
  181. function F() {}
  182. ha = hash(F, opt);
  183. F.prototype.meaningOfLife = 42;
  184. hb = hash(F, opt);
  185. assert.equal(ha, hb, 'Hashing should disregard changes in the function\'s prototype');
  186. });
  187. it('unorderedArrays = false', function() {
  188. var ha, hb;
  189. ha = hash([1, 2, 3]);
  190. hb = hash([3, 2, 1]);
  191. assert.notEqual(ha, hb, 'Hashing should respect the order of array entries');
  192. });
  193. it('unorderedArrays = true', function() {
  194. var opt = { unorderedArrays: true };
  195. var ha, hb;
  196. ha = hash([1, 2, 3], opt);
  197. hb = hash([3, 2, 1], opt);
  198. assert.equal(ha, hb, 'Hashing should not respect the order of array entries');
  199. ha = hash([{a: 1}, {a: 2}], opt);
  200. hb = hash([{a: 2}, {a: 1}], opt);
  201. assert.equal(ha, hb, 'Hashing should not respect the order of array entries');
  202. });
  203. it('excludeKeys works', function() {
  204. var ha, hb;
  205. ha = hash({a: 1, b: 4}, { excludeKeys: function(key) { return key === 'b' } });
  206. hb = hash({a: 1});
  207. assert.equal(ha, hb, 'Hashing should ignore key `b`');
  208. });
  209. if (typeof Set !== 'undefined') {
  210. it('unorderedSets = false', function() {
  211. var opt = { unorderedSets: false };
  212. var ha, hb;
  213. ha = hash(new Set([1, 2, 3]), opt);
  214. hb = hash(new Set([3, 2, 1]), opt);
  215. assert.notEqual(ha, hb, 'Hashing should respect the order of Set entries');
  216. });
  217. it('unorderedSets = true', function() {
  218. var ha, hb;
  219. ha = hash(new Set([1, 2, 3]));
  220. hb = hash(new Set([3, 2, 1]));
  221. assert.equal(ha, hb, 'Hashing should not respect the order of Set entries');
  222. });
  223. }
  224. });