sha1.js 870 B

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var crypto_1 = require("crypto");
  4. exports.cache = Object.create(null);
  5. function sha1() {
  6. var data = [];
  7. for (var _i = 0; _i < arguments.length; _i++) {
  8. data[_i] = arguments[_i];
  9. }
  10. var canCache = data.length === 1 && typeof data[0] === 'string';
  11. var cacheKey;
  12. if (canCache) {
  13. cacheKey = data[0];
  14. if (cacheKey in exports.cache) {
  15. return exports.cache[cacheKey];
  16. }
  17. }
  18. var hash = crypto_1.createHash('sha1');
  19. data.forEach(function (item) {
  20. if (typeof item === 'string')
  21. hash.update(item, 'utf8');
  22. else
  23. hash.update(item);
  24. });
  25. var res = hash.digest('hex').toString();
  26. if (canCache) {
  27. exports.cache[cacheKey] = res;
  28. }
  29. return res;
  30. }
  31. exports.sha1 = sha1;