index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. var Matching = require('./Matching.js');
  3. var index = require('./scoring/index.js');
  4. var TimeEstimates = require('./TimeEstimates.js');
  5. var Feedback = require('./Feedback.js');
  6. var Options = require('./Options.js');
  7. var debounce = require('./debounce.js');
  8. const time = () => new Date().getTime();
  9. const createReturnValue = (resolvedMatches, password, start) => {
  10. const feedback = new Feedback();
  11. const timeEstimates = new TimeEstimates();
  12. const matchSequence = index.mostGuessableMatchSequence(password, resolvedMatches);
  13. const calcTime = time() - start;
  14. const attackTimes = timeEstimates.estimateAttackTimes(matchSequence.guesses);
  15. return {
  16. calcTime,
  17. ...matchSequence,
  18. ...attackTimes,
  19. feedback: feedback.getFeedback(attackTimes.score, matchSequence.sequence)
  20. };
  21. };
  22. const main = (password, userInputs) => {
  23. if (userInputs) {
  24. Options.default.extendUserInputsDictionary(userInputs);
  25. }
  26. const matching = new Matching();
  27. return matching.match(password);
  28. };
  29. const zxcvbn = (password, userInputs) => {
  30. const start = time();
  31. const matches = main(password, userInputs);
  32. if (matches instanceof Promise) {
  33. throw new Error('You are using a Promised matcher, please use `zxcvbnAsync` for it.');
  34. }
  35. return createReturnValue(matches, password, start);
  36. };
  37. const zxcvbnAsync = async (password, userInputs) => {
  38. const usedPassword = password.substring(0, Options.default.maxLength);
  39. const start = time();
  40. const matches = await main(usedPassword, userInputs);
  41. return createReturnValue(matches, usedPassword, start);
  42. };
  43. exports.Options = Options.Options;
  44. exports.zxcvbnOptions = Options.default;
  45. exports.debounce = debounce;
  46. exports.zxcvbn = zxcvbn;
  47. exports.zxcvbnAsync = zxcvbnAsync;
  48. //# sourceMappingURL=index.js.map