UNPKG

3.65 kBJavaScriptView Raw
1'use strict';
2
3var crypto = require('crypto');
4var objectSorter = require('./objectSorter');
5
6/**
7 * Node object hash API object
8 * @typedef {Object} API
9 * @memberOf module:node-object-hash
10 * @inner
11 * @property {Function} hash Returns object hash string (see {@link module:node-object-hash#hash})
12 * @property {Function} sort Returns sorted object string (see {@link module:node-object-hash#sort})
13 */
14
15
16/**
17 * Generates node-object-hash API object
18 * @param {Object} [options] Library options
19 * @param {boolean} [options.coerce=true] Performs type coercion
20 * @param {boolean} [options.sort=true] Performs array, object, etc. sorting
21 * @param {string} [options.alg=sha256] Default crypto algorithm to use (can be overridden)
22 * @param {string} [options.enc=hex] Hash string encoding (can be overridden)
23 * @returns {module:node-object-hash~API} Node object hash API instance
24 * @memberOf module:node-object-hash
25 * @inner
26 * @example
27 * var apiConstructor = require('node-object-hash');
28 * var hashSortCoerce = apiConstructor({sort:true, coerce:true});
29 * // or
30 * var hashSort = apiConstructor({sort:true, coerce:false});
31 * // or
32 * var hashCoerce = apiConstructor({sort:false, coerce:true});
33 *
34 * var objects = {
35 * a: {
36 * a: [{c: 2, a: 1, b: {a: 3, c: 2, b: 0}}],
37 * b: [1, 'a', {}, null],
38 * },
39 * b: {
40 * b: ['a', 1, {}, undefined],
41 * a: [{c: '2', b: {b: false, c: 2, a: '3'}, a: true}]
42 * },
43 * c: ['4', true, 0, 2, 3]
44 * };
45 * hashSortCoerce.hash(objects.a) === hashSortCoerce.hash(objects.b);
46 * // returns true
47 *
48 * hashSortCoerce.sort(object.c);
49 * // returns '[0,1,2,3,4]'
50 */
51function apiConstructor(options) {
52 var defaults = options || {},
53 _sortObject;
54
55 defaults.alg = defaults.alg || 'sha256';
56 defaults.enc = defaults.enc || 'hex';
57
58 _sortObject = objectSorter(options);
59
60 /**
61 * Creates sorted string from given object
62 * @param {*} obj JS object to be sorted
63 * @returns {string} Sorted object string
64 * @see {@link module:node-object-hash/objectSorter~makeObjectSorter~objectToString}
65 * @memberOf module:node-object-hash
66 * @instance
67 * @public
68 * @alias sort
69 * @example
70 * var apiConstructor = require('node-object-hash');
71 * var sorter = apiConstructor({sort:true, coerce:true}).sort;
72 *
73 * sort({b: {b: 1, d: 'x'}, c: 2, a: [3, 5, 1]});
74 * // "{a:[1,3,5],b:{b:1,d:x},c:2}"
75 */
76 function sortObject(obj) {
77 return _sortObject(obj);
78 }
79
80 /**
81 * Creates hash from given object
82 * @param {*} obj JS object to hash
83 * @param {Object} [opts] Options
84 * @param {string} [opts.alg=sha256] Crypto algorithm to use
85 * @param {string} [opts.enc=hex] Hash string encoding
86 * @returns {string} Object hash value
87 * @memberOf module:node-object-hash
88 * @instance
89 * @public
90 * @alias hash
91 * @example
92 * var apiConstructor = require('node-object-hash');
93 * var hasher = apiConstructor({sort:true, coerce:true}).hash;
94 *
95 * hash({b: {b: 1, d: 'x'}, c: 2, a: [3, 5, 1]});
96 * // "4c18ce0dcb1696b329c8568d94a9830da810437d8c9e6cecf5d969780335a26b"
97 */
98 function hashObject(obj, opts) {
99 opts = opts || {};
100 var alg = opts.alg || defaults.alg,
101 enc = opts.enc || defaults.enc,
102 sorted = sortObject(obj);
103
104 return crypto.createHash(alg)
105 .update(sorted)
106 .digest(enc);
107 }
108
109 return {
110 hash: hashObject,
111 sortObject: sortObject,
112 sort: sortObject
113 };
114}
115
116/**
117 * Node object hash module.
118 * It provides a methods that return object hash or sorted object string.
119 * @module node-object-hash
120 * @type {module:node-object-hash~apiConstructor}
121 */
122module.exports = apiConstructor;