BaconCipherDecode.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * BaconCipher operation.
  3. *
  4. * @author kassi [kassi@users.noreply.github.com]
  5. * @copyright Karsten Silkenbäumer 2019
  6. * @license Apache-2.0
  7. */
  8. import Operation from "../Operation";
  9. import {
  10. BACON_ALPHABETS,
  11. BACON_TRANSLATION_CASE, BACON_TRANSLATION_AMNZ, BACON_TRANSLATIONS, BACON_CLEARER_MAP, BACON_NORMALIZE_MAP,
  12. swapZeroAndOne
  13. } from "../lib/Bacon";
  14. /**
  15. * BaconCipherDecode operation
  16. */
  17. class BaconCipherDecode extends Operation {
  18. /**
  19. * BaconCipherDecode constructor
  20. */
  21. constructor() {
  22. super();
  23. this.name = "Bacon Cipher Decode";
  24. this.module = "Default";
  25. this.description = "Bacon's cipher or the Baconian cipher is a method of steganography(a method of hiding a secret message as opposed to just a cipher) devised by Francis Bacon in 1605.[1][2][3] A message is concealed in the presentation of text, rather than its content.";
  26. this.infoURL = "https://en.wikipedia.org/wiki/Bacon%27s_cipher";
  27. this.inputType = "string";
  28. this.outputType = "string";
  29. this.args = [
  30. {
  31. "name": "Alphabet",
  32. "type": "option",
  33. "value": Object.keys(BACON_ALPHABETS)
  34. },
  35. {
  36. "name": "Translation",
  37. "type": "option",
  38. "value": BACON_TRANSLATIONS
  39. },
  40. {
  41. "name": "Invert Translation",
  42. "type": "boolean",
  43. "value": false
  44. }
  45. ];
  46. }
  47. /**
  48. * @param {String} input
  49. * @param {Object[]} args
  50. * @returns {String}
  51. */
  52. run(input, args) {
  53. const [alphabet, translation, invert] = args;
  54. const alphabetObject = BACON_ALPHABETS[alphabet];
  55. // remove invalid characters
  56. input = input.replace(BACON_CLEARER_MAP[translation], "");
  57. // normalize to unique alphabet
  58. if (BACON_NORMALIZE_MAP[translation] !== undefined) {
  59. input = input.replace(/./g, function (c) {
  60. return BACON_NORMALIZE_MAP[translation][c];
  61. });
  62. } else if (translation === BACON_TRANSLATION_CASE) {
  63. const codeA = "A".charCodeAt(0);
  64. const codeZ = "Z".charCodeAt(0);
  65. input = input.replace(/./g, function (c) {
  66. const code = c.charCodeAt(0);
  67. if (code >= codeA && code <= codeZ) {
  68. return "1";
  69. } else {
  70. return "0";
  71. }
  72. });
  73. } else if (translation === BACON_TRANSLATION_AMNZ) {
  74. const words = input.split(" ");
  75. const letters = words.map(function (e) {
  76. const code = e[0].toUpperCase().charCodeAt(0);
  77. return code >= "N".charCodeAt(0) ? "1" : "0";
  78. });
  79. input = letters.join("");
  80. }
  81. if (invert) {
  82. input = swapZeroAndOne(input);
  83. }
  84. // group into 5
  85. const inputArray = input.match(/(.{5})/g) || [];
  86. let output = "";
  87. for (let index = 0; index < inputArray.length; index++) {
  88. const code = inputArray[index];
  89. const number = parseInt(code, 2);
  90. output += number < alphabetObject.alphabet.length ? alphabetObject.alphabet[number] : "?";
  91. }
  92. return output;
  93. }
  94. }
  95. export default BaconCipherDecode;