BcryptCompare.mjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import bcrypt from "bcryptjs";
  8. /**
  9. * Bcrypt compare operation
  10. */
  11. class BcryptCompare extends Operation {
  12. /**
  13. * BcryptCompare constructor
  14. */
  15. constructor() {
  16. super();
  17. this.name = "Bcrypt compare";
  18. this.module = "Crypto";
  19. this.description = "Tests whether the input matches the given bcrypt hash. To test multiple possible passwords, use the 'Fork' operation.";
  20. this.infoURL = "https://wikipedia.org/wiki/Bcrypt";
  21. this.inputType = "string";
  22. this.outputType = "string";
  23. this.args = [
  24. {
  25. "name": "Hash",
  26. "type": "string",
  27. "value": ""
  28. }
  29. ];
  30. }
  31. /**
  32. * @param {string} input
  33. * @param {Object[]} args
  34. * @returns {string}
  35. */
  36. async run(input, args) {
  37. const hash = args[0];
  38. const match = await bcrypt.compare(input, hash, null, p => {
  39. // Progress callback
  40. if (ENVIRONMENT_IS_WORKER())
  41. self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`);
  42. });
  43. return match ? "Match: " + input : "No match";
  44. }
  45. }
  46. export default BcryptCompare;