CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
readme.markdown199 linesDownload Raw Back to object-hash
1# object-hash2 3Generate hashes from objects and values in node and the browser.  Uses node.js4crypto module for hashing.  Supports SHA1 and many others (depending on the platform)5as well as custom streams (e.g. CRC32).6 7[![NPM](https://nodei.co/npm/object-hash.png?downloads=true&downloadRank=true)](https://www.npmjs.com/package/object-hash)8 9[![Travis CI](https://secure.travis-ci.org/puleos/object-hash.png?branch=master)](https://secure.travis-ci.org/puleos/object-hash?branch=master)10[![Coverage Status](https://coveralls.io/repos/puleos/object-hash/badge.svg?branch=master&service=github)](https://coveralls.io/github/puleos/object-hash?branch=master)11 12* Hash values of any type.13* Supports a keys only option for grouping similar objects with different values.14 15```js16var hash = require('object-hash');17 18hash({foo: 'bar'}) // => '67b69634f9880a282c14a0f0cb7ba20cf5d677e9'19hash([1, 2, 2.718, 3.14159]) // => '136b9b88375971dff9f1af09d7356e3e04281951'20```21 22## Versioning Disclaimer23 24Starting with version `1.1.8` (released April 2017), new versions will consider25the exact returned hash part of the API contract, i.e. changes that will affect26hash values will be considered `semver-major`. Previous versions may violate27that expectation.28 29For more information, see [this discussion](https://github.com/puleos/object-hash/issues/30).30 31## hash(value, options)32 33Generate a hash from any object or type.  Defaults to sha1 with hex encoding.34 35* `algorithm` hash algo to be used: 'sha1', 'md5', 'passthrough'. default: sha136  * 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.37  * This also supports the `passthrough` algorith, which will return the information that would otherwise have been hashed.38* `excludeValues` {true|false} hash object keys, values ignored. default: false39* `encoding` hash encoding, supports 'buffer', 'hex', 'binary', 'base64'. default: hex40* `ignoreUnknown` {true|*false} ignore unknown object types. default: false41* `replacer` optional function that replaces values before hashing. default: accept all values42* `respectFunctionProperties` {true|false} Whether properties on functions are considered when hashing. default: true43* `respectFunctionNames` {true|false} consider `name` property of functions for hashing. default: true44* `respectType` {true|false} Whether special type attributes (`.prototype`, `.__proto__`, `.constructor`)45   are hashed. default: true46* `unorderedArrays` {true|false} Sort all arrays before hashing. Note that this affects *all* collections,47   i.e. including typed arrays, Sets, Maps, etc. default: false48* `unorderedSets` {true|false} Sort `Set` and `Map` instances before hashing, i.e. make49  `hash(new Set([1, 2])) == hash(new Set([2, 1]))` return `true`. default: true50* `unorderedObjects` {true|false} Sort objects before hashing, i.e. make `hash({ x: 1, y: 2 }) === hash({ y: 2, x: 1 })`. default: true51* `excludeKeys` optional function for excluding specific key(s) from hashing, if true is returned then exclude from hash. default: include all keys52 53## hash.sha1(value)54 55Hash using the sha1 algorithm.56 57Note that SHA-1 is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.58 59*Sugar method, equivalent to* `hash(value, {algorithm: 'sha1'})`60 61## hash.keys(value)62 63Hash object keys using the sha1 algorithm, values ignored.64 65*Sugar method, equivalent to* `hash(value, {excludeValues: true})`66 67## hash.MD5(value)68 69Hash using the md5 algorithm.70 71Note that the MD5 algorithm is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.72 73*Sugar method, equivalent to* `hash(value, {algorithm: 'md5'})`74 75## hash.keysMD5(value)76 77Hash object keys using the md5 algorithm, values ignored.78 79Note that the MD5 algorithm is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.80 81*Sugar method, equivalent to* `hash(value, {algorithm: 'md5', excludeValues: true})`82 83## hash.writeToStream(value, [options,] stream)84 85Write the information that would otherwise have been hashed to a stream, e.g.:86 87```js88hash.writeToStream({foo: 'bar', a: 42}, {respectType: false}, process.stdout)89// => e.g. 'object:a:number:42foo:string:bar'90```91 92## Installation93 94node:95 96```js97npm install object-hash98```99 100browser: */dist/object_hash.js*101 102```html103<script src="object_hash.js" type="text/javascript"></script>104 105<script>106  var hash = objectHash.sha1({foo:'bar'});107 108  console.log(hash); // e003c89cdf35cdf46d8239b4692436364b7259f9109</script>110```111 112## Example usage113 114```js115var hash = require('object-hash');116 117var peter = { name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };118var michael = { name: 'Michael', stapler: false, friends: ['Peter', 'Samir'] };119var bob = { name: 'Bob', stapler: true, friends: [] };120 121/***122 * sha1 hex encoding (default)123 */124hash(peter);125// 14fa461bf4b98155e82adc86532938553b4d33a9126hash(michael);127// 4b2b30e27699979ce46714253bc2213010db039c128hash(bob);129// 38d96106bc8ef3d8bd369b99bb6972702c9826d5130 131/***132 * hash object keys, values ignored133 */134hash(peter, { excludeValues: true });135// 48f370a772c7496f6c9d2e6d92e920c87dd00a5c136hash(michael, { excludeValues: true });137// 48f370a772c7496f6c9d2e6d92e920c87dd00a5c138hash.keys(bob);139// 48f370a772c7496f6c9d2e6d92e920c87dd00a5c140 141/***142 * hash object, ignore specific key(s)143 */144hash(peter, { excludeKeys: function(key) {145    if ( key === 'friends') {146      return true;147    }148    return false;149  }150});151// 66b7d7e64871aa9fda1bdc8e88a28df797648d80152 153/***154 * md5 base64 encoding155 */156hash(peter, { algorithm: 'md5', encoding: 'base64' });157// 6rkWaaDiG3NynWw4svGH7g==158hash(michael, { algorithm: 'md5', encoding: 'base64' });159// djXaWpuWVJeOF8Sb6SFFNg==160hash(bob, { algorithm: 'md5', encoding: 'base64' });161// lFzkw/IJ8/12jZI0rQeS3w==162```163 164## Legacy Browser Support165 166IE <= 8 and Opera <= 11 support dropped in version 0.3.0.  If you require167legacy browser support you must either use an ES5 shim or use version 0.2.5168of this module.169 170## Development171 172```sh-session173git clone https://github.com/puleos/object-hash174```175 176## Node Docker Wrapper177 178If you want to stand this up in a docker container, you should take at look179at the [![node-object-hash](https://github.com/bean5/node-object-hash)](https://github.com/bean5/node-object-hash) project.180 181### gulp tasks182 183* `gulp watch` (default) watch files, test and lint on change/add184* `gulp test` unit tests185* `gulp karma` browser unit tests186* `gulp lint` jshint187* `gulp dist` create browser version in /dist188 189## License190 191MIT192 193## Changelog194 195### v2.0.0196 197Only Node.js versions `>= 6.0.0` are being tested in CI now.198No other breaking changes were introduced.199