index.js 3.6 KB

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