BaconCipherEncode.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE,
  11. BACON_TRANSLATIONS_FOR_ENCODING, BACON_TRANSLATION_AB,
  12. swapZeroAndOne
  13. } from "../lib/Bacon";
  14. import { BACON_CODES_REDUCED } from "../lib/Bacon.mjs";
  15. /**
  16. * BaconCipherEncode operation
  17. */
  18. class BaconCipherEncode extends Operation {
  19. /**
  20. * BaconCipherEncode constructor
  21. */
  22. constructor() {
  23. super();
  24. this.name = "Bacon Cipher Encode";
  25. this.module = "Default";
  26. 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.";
  27. this.infoURL = "https://en.wikipedia.org/wiki/Bacon%27s_cipher";
  28. this.inputType = "string";
  29. this.outputType = "string";
  30. this.args = [
  31. {
  32. "name": "Alphabet",
  33. "type": "option",
  34. "value": [BACON_ALPHABET_REDUCED, BACON_ALPHABET_COMPLETE]
  35. },
  36. {
  37. "name": "Translation",
  38. "type": "option",
  39. "value": BACON_TRANSLATIONS_FOR_ENCODING
  40. },
  41. {
  42. "name": "Keep extra characters",
  43. "type": "boolean",
  44. "value": false
  45. },
  46. {
  47. "name": "Invert Translation",
  48. "type": "boolean",
  49. "value": false
  50. }
  51. ];
  52. }
  53. /**
  54. * @param {String} input
  55. * @param {Object[]} args
  56. * @returns {String}
  57. */
  58. run(input, args) {
  59. const [alphabet, translation, keep, invert] = args;
  60. const charCodeA = "A".charCodeAt(0);
  61. const charCodeZ = "Z".charCodeAt(0);
  62. let output = input.replace(/./g, function (c) {
  63. const charCode = c.toUpperCase().charCodeAt(0);
  64. if (charCode >= charCodeA && charCode <= charCodeZ) {
  65. let code = charCode - charCodeA;
  66. if (alphabet === BACON_ALPHABET_REDUCED) {
  67. code = BACON_CODES_REDUCED[code];
  68. }
  69. const bacon = ("00000" + code.toString(2)).substr(-5, 5);
  70. return bacon;
  71. } else {
  72. return c;
  73. }
  74. });
  75. if (invert) {
  76. output = swapZeroAndOne(output);
  77. }
  78. if (!keep) {
  79. output = output.replace(/[^01]/g, "");
  80. const outputArray = output.match(/(.{5})/g) || [];
  81. output = outputArray.join(" ");
  82. }
  83. if (translation === BACON_TRANSLATION_AB) {
  84. output = output.replace(/[01]/g, function (c) {
  85. return {
  86. "0": "A",
  87. "1": "B"
  88. }[c];
  89. });
  90. }
  91. return output;
  92. }
  93. }
  94. export default BaconCipherEncode;