XOR.mjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Utils from "../Utils";
  8. import { bitOp, xor } from "../lib/BitwiseOp";
  9. /**
  10. * XOR operation
  11. */
  12. class XOR extends Operation {
  13. /**
  14. * XOR constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "XOR";
  19. this.module = "Default";
  20. this.description = "XOR the input with the given key.<br>e.g. <code>fe023da5</code><br><br><strong>Options</strong><br><u>Null preserving:</u> If the current byte is 0x00 or the same as the key, skip it.<br><br><u>Scheme:</u><ul><li>Standard - key is unchanged after each round</li><li>Input differential - key is set to the value of the previous unprocessed byte</li><li>Output differential - key is set to the value of the previous processed byte</li></ul>";
  21. this.inputType = "byteArray";
  22. this.outputType = "byteArray";
  23. this.args = [
  24. {
  25. "name": "Key",
  26. "type": "toggleString",
  27. "value": "",
  28. "toggleValues": ["Hex", "Base64", "UTF8", "Latin1"]
  29. },
  30. {
  31. "name": "Scheme",
  32. "type": "option",
  33. "value": ["Standard", "Input differential", "Output differential"]
  34. },
  35. {
  36. "name": "Null preserving",
  37. "type": "boolean",
  38. "value": false
  39. }
  40. ];
  41. }
  42. /**
  43. * @param {byteArray} input
  44. * @param {Object[]} args
  45. * @returns {byteArray}
  46. */
  47. run(input, args) {
  48. const key = Utils.convertToByteArray(args[0].string || "", args[0].option),
  49. [, scheme, nullPreserving] = args;
  50. return bitOp(input, key, xor, nullPreserving, scheme);
  51. }
  52. /**
  53. * Highlight XOR
  54. *
  55. * @param {Object[]} pos
  56. * @param {number} pos[].start
  57. * @param {number} pos[].end
  58. * @param {Object[]} args
  59. * @returns {Object[]} pos
  60. */
  61. highlight(pos, args) {
  62. return pos;
  63. }
  64. /**
  65. * Highlight XOR in reverse
  66. *
  67. * @param {Object[]} pos
  68. * @param {number} pos[].start
  69. * @param {number} pos[].end
  70. * @param {Object[]} args
  71. * @returns {Object[]} pos
  72. */
  73. highlightReverse(pos, args) {
  74. return pos;
  75. }
  76. }
  77. export default XOR;