BaconCipherDecode.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @author Karsten Silkenbäumer [github.com/kassi]
  3. * @copyright Karsten Silkenbäumer 2019
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import {
  8. BACON_ALPHABETS,
  9. BACON_TRANSLATION_CASE, BACON_TRANSLATION_AMNZ, BACON_TRANSLATIONS, BACON_CLEARER_MAP, BACON_NORMALIZE_MAP,
  10. swapZeroAndOne
  11. } from "../lib/Bacon.mjs";
  12. /**
  13. * Bacon Cipher Decode operation
  14. */
  15. class BaconCipherDecode extends Operation {
  16. /**
  17. * BaconCipherDecode constructor
  18. */
  19. constructor() {
  20. super();
  21. this.name = "Bacon Cipher Decode";
  22. this.module = "Default";
  23. this.description = "Bacon's cipher or the Baconian cipher is a method of steganography devised by Francis Bacon in 1605. A message is concealed in the presentation of text, rather than its content.";
  24. this.infoURL = "https://wikipedia.org/wiki/Bacon%27s_cipher";
  25. this.inputType = "string";
  26. this.outputType = "string";
  27. this.args = [
  28. {
  29. "name": "Alphabet",
  30. "type": "option",
  31. "value": Object.keys(BACON_ALPHABETS)
  32. },
  33. {
  34. "name": "Translation",
  35. "type": "option",
  36. "value": BACON_TRANSLATIONS
  37. },
  38. {
  39. "name": "Invert Translation",
  40. "type": "boolean",
  41. "value": false
  42. }
  43. ];
  44. this.checks = [
  45. {
  46. pattern: "^\\s*([01]{5}\\s?)+$",
  47. flags: "",
  48. args: ["Standard (I=J and U=V)", "0/1", false]
  49. },
  50. {
  51. pattern: "^\\s*([01]{5}\\s?)+$",
  52. flags: "",
  53. args: ["Standard (I=J and U=V)", "0/1", true]
  54. },
  55. {
  56. pattern: "^\\s*([AB]{5}\\s?)+$",
  57. flags: "",
  58. args: ["Standard (I=J and U=V)", "A/B", false]
  59. },
  60. {
  61. pattern: "^\\s*([AB]{5}\\s?)+$",
  62. flags: "",
  63. args: ["Standard (I=J and U=V)", "A/B", true]
  64. },
  65. {
  66. pattern: "^\\s*([01]{5}\\s?)+$",
  67. flags: "",
  68. args: ["Complete", "0/1", false]
  69. },
  70. {
  71. pattern: "^\\s*([01]{5}\\s?)+$",
  72. flags: "",
  73. args: ["Complete", "0/1", true]
  74. },
  75. {
  76. pattern: "^\\s*([AB]{5}\\s?)+$",
  77. flags: "",
  78. args: ["Complete", "A/B", false]
  79. },
  80. {
  81. pattern: "^\\s*([AB]{5}\\s?)+$",
  82. flags: "",
  83. args: ["Complete", "A/B", true]
  84. }
  85. ];
  86. }
  87. /**
  88. * @param {string} input
  89. * @param {Object[]} args
  90. * @returns {string}
  91. */
  92. run(input, args) {
  93. const [alphabet, translation, invert] = args;
  94. const alphabetObject = BACON_ALPHABETS[alphabet];
  95. // remove invalid characters
  96. input = input.replace(BACON_CLEARER_MAP[translation], "");
  97. // normalize to unique alphabet
  98. if (BACON_NORMALIZE_MAP[translation] !== undefined) {
  99. input = input.replace(/./g, function (c) {
  100. return BACON_NORMALIZE_MAP[translation][c];
  101. });
  102. } else if (translation === BACON_TRANSLATION_CASE) {
  103. const codeA = "A".charCodeAt(0);
  104. const codeZ = "Z".charCodeAt(0);
  105. input = input.replace(/./g, function (c) {
  106. const code = c.charCodeAt(0);
  107. if (code >= codeA && code <= codeZ) {
  108. return "1";
  109. } else {
  110. return "0";
  111. }
  112. });
  113. } else if (translation === BACON_TRANSLATION_AMNZ) {
  114. const words = input.split(/\s+/);
  115. const letters = words.map(function (e) {
  116. if (e) {
  117. const code = e[0].toUpperCase().charCodeAt(0);
  118. return code >= "N".charCodeAt(0) ? "1" : "0";
  119. } else {
  120. return "";
  121. }
  122. });
  123. input = letters.join("");
  124. }
  125. if (invert) {
  126. input = swapZeroAndOne(input);
  127. }
  128. // group into 5
  129. const inputArray = input.match(/(.{5})/g) || [];
  130. let output = "";
  131. for (let i = 0; i < inputArray.length; i++) {
  132. const code = inputArray[i];
  133. const number = parseInt(code, 2);
  134. output += number < alphabetObject.alphabet.length ? alphabetObject.alphabet[number] : "?";
  135. }
  136. return output;
  137. }
  138. }
  139. export default BaconCipherDecode;