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