crypto.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 'use strict';
  2. var crypto = require('crypto');
  3. /**
  4. * hash
  5. *
  6. * @param {String} method hash method, e.g.: 'md5', 'sha1'
  7. * @param {String|Buffer|Object} s
  8. * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
  9. * @return {String} md5 hash string
  10. * @public
  11. */
  12. exports.hash = function hash(method, s, format) {
  13. var sum = crypto.createHash(method);
  14. var isBuffer = Buffer.isBuffer(s);
  15. if (!isBuffer && typeof s === 'object') {
  16. s = JSON.stringify(sortObject(s));
  17. }
  18. sum.update(s, isBuffer ? 'binary' : 'utf8');
  19. return sum.digest(format || 'hex');
  20. };
  21. /**
  22. * md5 hash
  23. *
  24. * @param {String|Buffer|Object} s
  25. * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
  26. * @return {String} md5 hash string
  27. * @public
  28. */
  29. exports.md5 = function md5(s, format) {
  30. return exports.hash('md5', s, format);
  31. };
  32. /**
  33. * sha1 hash
  34. *
  35. * @param {String|Buffer|Object} s
  36. * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
  37. * @return {String} sha1 hash string
  38. * @public
  39. */
  40. exports.sha1 = function sha1(s, format) {
  41. return exports.hash('sha1', s, format);
  42. };
  43. /**
  44. * sha256 hash
  45. *
  46. * @param {String|Buffer|Object} s
  47. * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'.
  48. * @return {String} sha256 hash string
  49. * @public
  50. */
  51. exports.sha256 = function sha256(s, format) {
  52. return exports.hash('sha256', s, format);
  53. };
  54. /**
  55. * HMAC algorithm.
  56. *
  57. * Equal bash:
  58. *
  59. * ```bash
  60. * $ echo -n "$data" | openssl dgst -binary -$algorithm -hmac "$key" | openssl $encoding
  61. * ```
  62. *
  63. * @param {String} algorithm, dependent on the available algorithms supported by the version of OpenSSL on the platform.
  64. * Examples are 'sha1', 'md5', 'sha256', 'sha512', etc.
  65. * On recent releases, `openssl list-message-digest-algorithms` will display the available digest algorithms.
  66. * @param {String} key, the hmac key to be used.
  67. * @param {String|Buffer} data, content string.
  68. * @param {String} [encoding='base64']
  69. * @return {String} digest string.
  70. */
  71. exports.hmac = function hmac(algorithm, key, data, encoding) {
  72. encoding = encoding || 'base64';
  73. var hmac = crypto.createHmac(algorithm, key);
  74. hmac.update(data, Buffer.isBuffer(data) ? 'binary' : 'utf8');
  75. return hmac.digest(encoding);
  76. };
  77. /**
  78. * Base64 encode string.
  79. *
  80. * @param {String|Buffer} s
  81. * @param {Boolean} [urlsafe=false] Encode string s using a URL-safe alphabet,
  82. * which substitutes - instead of + and _ instead of / in the standard Base64 alphabet.
  83. * @return {String} base64 encode format string.
  84. */
  85. exports.base64encode = function base64encode(s, urlsafe) {
  86. if (!Buffer.isBuffer(s)) {
  87. s = typeof Buffer.from === 'function' ? Buffer.from(s) : new Buffer(s);
  88. }
  89. var encode = s.toString('base64');
  90. if (urlsafe) {
  91. encode = encode.replace(/\+/g, '-').replace(/\//g, '_');
  92. }
  93. return encode;
  94. };
  95. /**
  96. * Base64 string decode.
  97. *
  98. * @param {String} encode, base64 encoding string.
  99. * @param {Boolean} [urlsafe=false] Decode string s using a URL-safe alphabet,
  100. * which substitutes - instead of + and _ instead of / in the standard Base64 alphabet.
  101. * @param {encoding} [encoding=utf8] if encoding = buffer, will return Buffer instance
  102. * @return {String|Buffer} plain text.
  103. */
  104. exports.base64decode = function base64decode(encodeStr, urlsafe, encoding) {
  105. if (urlsafe) {
  106. encodeStr = encodeStr.replace(/\-/g, '+').replace(/_/g, '/');
  107. }
  108. var buf = typeof Buffer.from === 'function' ? Buffer.from(encodeStr, 'base64') : new Buffer(encodeStr, 'base64');
  109. if (encoding === 'buffer') {
  110. return buf;
  111. }
  112. return buf.toString(encoding || 'utf8');
  113. };
  114. function sortObject(o) {
  115. if (!o || Array.isArray(o) || typeof o !== 'object') {
  116. return o;
  117. }
  118. var keys = Object.keys(o);
  119. keys.sort();
  120. var values = [];
  121. for (var i = 0; i < keys.length; i++) {
  122. var k = keys[i];
  123. values.push([k, sortObject(o[k])]);
  124. }
  125. return values;
  126. }