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.

198 lines
6.6 KiB

2 years ago
  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', 'passthrough'. default: sha1
  24. * This supports the algorithms returned by `crypto.getHashes()`. Note that the default of SHA-1 is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
  25. * This also supports the `passthrough` algorith, which will return the information that would otherwise have been hashed.
  26. * `excludeValues` {true|false} hash object keys, values ignored. default: false
  27. * `encoding` hash encoding, supports 'buffer', 'hex', 'binary', 'base64'. default: hex
  28. * `ignoreUnknown` {true|*false} ignore unknown object types. default: false
  29. * `replacer` optional function that replaces values before hashing. default: accept all values
  30. * `respectFunctionProperties` {true|false} Whether properties on functions are considered when hashing. default: true
  31. * `respectFunctionNames` {true|false} consider `name` property of functions for hashing. default: true
  32. * `respectType` {true|false} Whether special type attributes (`.prototype`, `.__proto__`, `.constructor`)
  33. are hashed. default: true
  34. * `unorderedArrays` {true|false} Sort all arrays before hashing. Note that this affects *all* collections,
  35. i.e. including typed arrays, Sets, Maps, etc. default: false
  36. * `unorderedSets` {true|false} Sort `Set` and `Map` instances before hashing, i.e. make
  37. `hash(new Set([1, 2])) == hash(new Set([2, 1]))` return `true`. default: true
  38. * `unorderedObjects` {true|false} Sort objects before hashing, i.e. make `hash({ x: 1, y: 2 }) === hash({ y: 2, x: 1 })`. default: true
  39. * `excludeKeys` optional function for excluding specific key(s) from hashing, if true is returned then exclude from hash. default: include all keys
  40. ## hash.sha1(value)
  41. Hash using the sha1 algorithm.
  42. Note that SHA-1 is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
  43. *Sugar method, equivalent to* `hash(value, {algorithm: 'sha1'})`
  44. ## hash.keys(value)
  45. Hash object keys using the sha1 algorithm, values ignored.
  46. *Sugar method, equivalent to* `hash(value, {excludeValues: true})`
  47. ## hash.MD5(value)
  48. Hash using the md5 algorithm.
  49. Note that the MD5 algorithm is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
  50. *Sugar method, equivalent to* `hash(value, {algorithm: 'md5'})`
  51. ## hash.keysMD5(value)
  52. Hash object keys using the md5 algorithm, values ignored.
  53. Note that the MD5 algorithm is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
  54. *Sugar method, equivalent to* `hash(value, {algorithm: 'md5', excludeValues: true})`
  55. ## hash.writeToStream(value, [options,] stream)
  56. Write the information that would otherwise have been hashed to a stream, e.g.:
  57. ```js
  58. hash.writeToStream({foo: 'bar', a: 42}, {respectType: false}, process.stdout)
  59. // => e.g. 'object:a:number:42foo:string:bar'
  60. ```
  61. ## Installation
  62. node:
  63. ```js
  64. npm install object-hash
  65. ```
  66. browser: */dist/object_hash.js*
  67. ```html
  68. <script src="object_hash.js" type="text/javascript"></script>
  69. <script>
  70. var hash = objectHash.sha1({foo:'bar'});
  71. console.log(hash); // e003c89cdf35cdf46d8239b4692436364b7259f9
  72. </script>
  73. ```
  74. ## Example usage
  75. ```js
  76. var hash = require('object-hash');
  77. var peter = { name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };
  78. var michael = { name: 'Michael', stapler: false, friends: ['Peter', 'Samir'] };
  79. var bob = { name: 'Bob', stapler: true, friends: [] };
  80. /***
  81. * sha1 hex encoding (default)
  82. */
  83. hash(peter);
  84. // 14fa461bf4b98155e82adc86532938553b4d33a9
  85. hash(michael);
  86. // 4b2b30e27699979ce46714253bc2213010db039c
  87. hash(bob);
  88. // 38d96106bc8ef3d8bd369b99bb6972702c9826d5
  89. /***
  90. * hash object keys, values ignored
  91. */
  92. hash(peter, { excludeValues: true });
  93. // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
  94. hash(michael, { excludeValues: true });
  95. // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
  96. hash.keys(bob);
  97. // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
  98. /***
  99. * hash object, ignore specific key(s)
  100. */
  101. hash(peter, { excludeKeys: function(key) {
  102. if ( key === 'friends') {
  103. return true;
  104. }
  105. return false;
  106. }
  107. });
  108. // 66b7d7e64871aa9fda1bdc8e88a28df797648d80
  109. /***
  110. * md5 base64 encoding
  111. */
  112. hash(peter, { algorithm: 'md5', encoding: 'base64' });
  113. // 6rkWaaDiG3NynWw4svGH7g==
  114. hash(michael, { algorithm: 'md5', encoding: 'base64' });
  115. // djXaWpuWVJeOF8Sb6SFFNg==
  116. hash(bob, { algorithm: 'md5', encoding: 'base64' });
  117. // lFzkw/IJ8/12jZI0rQeS3w==
  118. ```
  119. ## Legacy Browser Support
  120. IE <= 8 and Opera <= 11 support dropped in version 0.3.0. If you require
  121. legacy browser support you must either use an ES5 shim or use version 0.2.5
  122. of this module.
  123. ## Development
  124. ```sh-session
  125. git clone https://github.com/puleos/object-hash
  126. ```
  127. ## Node Docker Wrapper
  128. If you want to stand this up in a docker container, you should take at look
  129. at the [![node-object-hash](https://github.com/bean5/node-object-hash)](https://github.com/bean5/node-object-hash) project.
  130. ### gulp tasks
  131. * `gulp watch` (default) watch files, test and lint on change/add
  132. * `gulp test` unit tests
  133. * `gulp karma` browser unit tests
  134. * `gulp lint` jshint
  135. * `gulp dist` create browser version in /dist
  136. ## License
  137. MIT
  138. ## Changelog
  139. ### v2.0.0
  140. Only Node.js versions `>= 6.0.0` are being tested in CI now.
  141. No other breaking changes were introduced.