be8a98f5625cff07c0786812a8c731ed1462f95304e6089bc53a1fb1135211ca823e33d5a33961397c90601a2c924e0370198b6bc6bc2f3367506768806231 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # object-hash
  2. Generate hashes from objects and values in node and the browser. Uses node.js
  3. crypto module for hashing. Supports SHA1 and many others (depending on the platform)
  4. as well as custom streams (e.g. CRC32).
  5. [![NPM](https://nodei.co/npm/object-hash.png?downloads=true&downloadRank=true)](https://www.npmjs.com/package/object-hash)
  6. [![Travis CI](https://secure.travis-ci.org/puleos/object-hash.png?branch=master)](https://secure.travis-ci.org/puleos/object-hash?branch=master)
  7. [![Coverage Status](https://coveralls.io/repos/puleos/object-hash/badge.svg?branch=master&service=github)](https://coveralls.io/github/puleos/object-hash?branch=master)
  8. * Hash values of any type.
  9. * Supports a keys only option for grouping similar objects with different values.
  10. ```js
  11. var hash = require('object-hash');
  12. hash({foo: 'bar'}) // => '67b69634f9880a282c14a0f0cb7ba20cf5d677e9'
  13. hash([1, 2, 2.718, 3.14159]) // => '136b9b88375971dff9f1af09d7356e3e04281951'
  14. ```
  15. ## Versioning Disclaimer
  16. Starting with version `1.1.8` (released April 2017), new versions will consider
  17. the exact returned hash part of the API contract, i.e. changes that will affect
  18. hash values will be considered `semver-major`. Previous versions may violate
  19. that expectation.
  20. For more information, see [this discussion](https://github.com/puleos/object-hash/issues/30).
  21. ## hash(value, options);
  22. Generate a hash from any object or type. Defaults to sha1 with hex encoding.
  23. * `algorithm` hash algo to be used: 'sha1', 'md5'. default: sha1
  24. * `excludeValues` {true|false} hash object keys, values ignored. default: false
  25. * `encoding` hash encoding, supports 'buffer', 'hex', 'binary', 'base64'. default: hex
  26. * `ignoreUnknown` {true|*false} ignore unknown object types. default: false
  27. * `replacer` optional function that replaces values before hashing. default: accept all values
  28. * `respectFunctionProperties` {true|false} Whether properties on functions are considered when hashing. default: true
  29. * `respectFunctionNames` {true|false} consider `name` property of functions for hashing. default: true
  30. * `respectType` {true|false} Whether special type attributes (`.prototype`, `.__proto__`, `.constructor`)
  31. are hashed. default: true
  32. * `unorderedArrays` {true|false} Sort all arrays using before hashing. Note that this affects *all* collections,
  33. i.e. including typed arrays, Sets, Maps, etc. default: false
  34. * `unorderedSets` {true|false} Sort `Set` and `Map` instances before hashing, i.e. make
  35. `hash(new Set([1, 2])) == hash(new Set([2, 1]))` return `true`. default: true
  36. * `unorderedObjects` {true|false} Sort objects before hashing, i.e. make `hash({ x: 1, y: 2 }) === hash({ y: 2, x: 1 })`. default: true
  37. * `excludeKeys` optional function for exclude specific key(s) from hashing, if returns true then exclude from hash. default: include all keys
  38. ## hash.sha1(value);
  39. Hash using the sha1 algorithm.
  40. *Sugar method, equivalent to hash(value, {algorithm: 'sha1'})*
  41. ## hash.keys(value);
  42. Hash object keys using the sha1 algorithm, values ignored.
  43. *Sugar method, equivalent to hash(value, {excludeValues: true})*
  44. ## hash.MD5(value);
  45. Hash using the md5 algorithm.
  46. *Sugar method, equivalent to hash(value, {algorithm: 'md5'})*
  47. ## hash.keysMD5(value);
  48. Hash object keys using the md5 algorithm, values ignored.
  49. *Sugar method, equivalent to hash(value, {algorithm: 'md5', excludeValues: true})*
  50. ## hash.writeToStream(value, [options,] stream):
  51. Write the information that would otherwise have been hashed to a stream, e.g.:
  52. ```js
  53. hash.writeToStream({foo: 'bar', a: 42}, {respectType: false}, process.stdout)
  54. // => e.g. 'object:a:number:42foo:string:bar'
  55. ```
  56. ## Installation
  57. node:
  58. ```js
  59. npm install object-hash
  60. ```
  61. browser: */dist/object_hash.js*
  62. ```
  63. <script src="object_hash.js" type="text/javascript"></script>
  64. <script>
  65. var hash = objectHash.sha1({foo:'bar'});
  66. console.log(hash); // e003c89cdf35cdf46d8239b4692436364b7259f9
  67. </script>
  68. ```
  69. ## Example usage
  70. ```js
  71. var hash = require('object-hash');
  72. var peter = {name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };
  73. var michael = {name: 'Michael', stapler: false, friends: ['Peter', 'Samir'] };
  74. var bob = {name: 'Bob', stapler: true, friends: [] };
  75. /***
  76. * sha1 hex encoding (default)
  77. */
  78. hash(peter);
  79. // 14fa461bf4b98155e82adc86532938553b4d33a9
  80. hash(michael);
  81. // 4b2b30e27699979ce46714253bc2213010db039c
  82. hash(bob);
  83. // 38d96106bc8ef3d8bd369b99bb6972702c9826d5
  84. /***
  85. * hash object keys, values ignored
  86. */
  87. hash(peter, { excludeValues: true });
  88. // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
  89. hash(michael, { excludeValues: true });
  90. // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
  91. hash.keys(bob);
  92. // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
  93. /***
  94. * hash object, ignore specific key(s)
  95. */
  96. hash(peter, { excludeKeys: function(key) {
  97. if ( key === 'friends') {
  98. return true;
  99. }
  100. return false;
  101. }
  102. });
  103. // 66b7d7e64871aa9fda1bdc8e88a28df797648d80
  104. /***
  105. * md5 base64 encoding
  106. */
  107. hash(peter, { algorithm: 'md5', encoding: 'base64' });
  108. // 6rkWaaDiG3NynWw4svGH7g==
  109. hash(michael, { algorithm: 'md5', encoding: 'base64' });
  110. // djXaWpuWVJeOF8Sb6SFFNg==
  111. hash(bob, { algorithm: 'md5', encoding: 'base64' });
  112. // lFzkw/IJ8/12jZI0rQeS3w==
  113. ```
  114. ## Legacy Browser Support
  115. IE <= 8 and Opera <= 11 support dropped in version 0.3.0. If you require
  116. legacy browser support you must either use an ES5 shim or use version 0.2.5
  117. of this module.
  118. ## Development
  119. ```
  120. git clone https://github.com/puleos/object-hash
  121. ```
  122. ## Node Docker Wrapper
  123. If you want to stand this up in a docker container, you should take at look
  124. at the [![node-object-hash](https://github.com/bean5/node-object-hash)](https://github.com/bean5/node-object-hash) project.
  125. ### gulp tasks
  126. * `gulp watch` (default) watch files, test and lint on change/add
  127. * `gulp test` unit tests
  128. * `gulp karma` browser unit tests
  129. * `gulp lint` jshint
  130. * `gulp dist` create browser version in /dist
  131. ## License
  132. MIT