Base64.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import Utils from "../Utils.js";
  2. /**
  3. * Base64 operations.
  4. *
  5. * @author n1474335 [n1474335@gmail.com]
  6. * @copyright Crown Copyright 2016
  7. * @license Apache-2.0
  8. *
  9. * @namespace
  10. */
  11. const Base64 = {
  12. /**
  13. * @constant
  14. * @default
  15. */
  16. ALPHABET: "A-Za-z0-9+/=",
  17. /**
  18. * @constant
  19. * @default
  20. */
  21. ALPHABET_OPTIONS: [
  22. {name: "Standard: A-Za-z0-9+/=", value: "A-Za-z0-9+/="},
  23. {name: "URL safe: A-Za-z0-9-_", value: "A-Za-z0-9-_"},
  24. {name: "Filename safe: A-Za-z0-9+-=", value: "A-Za-z0-9+\\-="},
  25. {name: "itoa64: ./0-9A-Za-z=", value: "./0-9A-Za-z="},
  26. {name: "XML: A-Za-z0-9_.", value: "A-Za-z0-9_."},
  27. {name: "y64: A-Za-z0-9._-", value: "A-Za-z0-9._-"},
  28. {name: "z64: 0-9a-zA-Z+/=", value: "0-9a-zA-Z+/="},
  29. {name: "Radix-64: 0-9A-Za-z+/=", value: "0-9A-Za-z+/="},
  30. {name: "Uuencoding: [space]-_", value: " -_"},
  31. {name: "Xxencoding: +-0-9A-Za-z", value: "+\\-0-9A-Za-z"},
  32. {name: "BinHex: !-,-0-689@A-NP-VX-Z[`a-fh-mp-r", value: "!-,-0-689@A-NP-VX-Z[`a-fh-mp-r"},
  33. {name: "ROT13: N-ZA-Mn-za-m0-9+/=", value: "N-ZA-Mn-za-m0-9+/="},
  34. {name: "UNIX crypt: ./0-9A-Za-z", value: "./0-9A-Za-z"},
  35. ],
  36. /**
  37. * To Base64 operation.
  38. *
  39. * @param {ArrayBuffer} input
  40. * @param {Object[]} args
  41. * @returns {string}
  42. */
  43. runTo: function(input, args) {
  44. const alphabet = args[0] || Base64.ALPHABET;
  45. return Utils.toBase64(new Uint8Array(input), alphabet);
  46. },
  47. /**
  48. * @constant
  49. * @default
  50. */
  51. REMOVE_NON_ALPH_CHARS: true,
  52. /**
  53. * From Base64 operation.
  54. *
  55. * @param {string} input
  56. * @param {Object[]} args
  57. * @returns {byteArray}
  58. */
  59. runFrom: function(input, args) {
  60. let alphabet = args[0] || Base64.ALPHABET,
  61. removeNonAlphChars = args[1];
  62. return Utils.fromBase64(input, alphabet, "byteArray", removeNonAlphChars);
  63. },
  64. /**
  65. * @constant
  66. * @default
  67. */
  68. BASE32_ALPHABET: "A-Z2-7=",
  69. /**
  70. * To Base32 operation.
  71. *
  72. * @param {byteArray} input
  73. * @param {Object[]} args
  74. * @returns {string}
  75. */
  76. runTo32: function(input, args) {
  77. if (!input) return "";
  78. let alphabet = args[0] ?
  79. Utils.expandAlphRange(args[0]).join("") : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",
  80. output = "",
  81. chr1, chr2, chr3, chr4, chr5,
  82. enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8,
  83. i = 0;
  84. while (i < input.length) {
  85. chr1 = input[i++];
  86. chr2 = input[i++];
  87. chr3 = input[i++];
  88. chr4 = input[i++];
  89. chr5 = input[i++];
  90. enc1 = chr1 >> 3;
  91. enc2 = ((chr1 & 7) << 2) | (chr2 >> 6);
  92. enc3 = (chr2 >> 1) & 31;
  93. enc4 = ((chr2 & 1) << 4) | (chr3 >> 4);
  94. enc5 = ((chr3 & 15) << 1) | (chr4 >> 7);
  95. enc6 = (chr4 >> 2) & 31;
  96. enc7 = ((chr4 & 3) << 3) | (chr5 >> 5);
  97. enc8 = chr5 & 31;
  98. if (isNaN(chr2)) {
  99. enc3 = enc4 = enc5 = enc6 = enc7 = enc8 = 32;
  100. } else if (isNaN(chr3)) {
  101. enc5 = enc6 = enc7 = enc8 = 32;
  102. } else if (isNaN(chr4)) {
  103. enc6 = enc7 = enc8 = 32;
  104. } else if (isNaN(chr5)) {
  105. enc8 = 32;
  106. }
  107. output += alphabet.charAt(enc1) + alphabet.charAt(enc2) + alphabet.charAt(enc3) +
  108. alphabet.charAt(enc4) + alphabet.charAt(enc5) + alphabet.charAt(enc6) +
  109. alphabet.charAt(enc7) + alphabet.charAt(enc8);
  110. }
  111. return output;
  112. },
  113. /**
  114. * From Base32 operation.
  115. *
  116. * @param {string} input
  117. * @param {Object[]} args
  118. * @returns {byteArray}
  119. */
  120. runFrom32: function(input, args) {
  121. if (!input) return [];
  122. let alphabet = args[0] ?
  123. Utils.expandAlphRange(args[0]).join("") : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",
  124. removeNonAlphChars = args[0];
  125. let output = [],
  126. chr1, chr2, chr3, chr4, chr5,
  127. enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8,
  128. i = 0;
  129. if (removeNonAlphChars) {
  130. const re = new RegExp("[^" + alphabet.replace(/[\]\\\-^]/g, "\\$&") + "]", "g");
  131. input = input.replace(re, "");
  132. }
  133. while (i < input.length) {
  134. enc1 = alphabet.indexOf(input.charAt(i++));
  135. enc2 = alphabet.indexOf(input.charAt(i++) || "=");
  136. enc3 = alphabet.indexOf(input.charAt(i++) || "=");
  137. enc4 = alphabet.indexOf(input.charAt(i++) || "=");
  138. enc5 = alphabet.indexOf(input.charAt(i++) || "=");
  139. enc6 = alphabet.indexOf(input.charAt(i++) || "=");
  140. enc7 = alphabet.indexOf(input.charAt(i++) || "=");
  141. enc8 = alphabet.indexOf(input.charAt(i++) || "=");
  142. chr1 = (enc1 << 3) | (enc2 >> 2);
  143. chr2 = ((enc2 & 3) << 6) | (enc3 << 1) | (enc4 >> 4);
  144. chr3 = ((enc4 & 15) << 4) | (enc5 >> 1);
  145. chr4 = ((enc5 & 1) << 7) | (enc6 << 2) | (enc7 >> 3);
  146. chr5 = ((enc7 & 7) << 5) | enc8;
  147. output.push(chr1);
  148. if (enc2 & 3 !== 0 || enc3 !== 32) output.push(chr2);
  149. if (enc4 & 15 !== 0 || enc5 !== 32) output.push(chr3);
  150. if (enc5 & 1 !== 0 || enc6 !== 32) output.push(chr4);
  151. if (enc7 & 7 !== 0 || enc8 !== 32) output.push(chr5);
  152. }
  153. return output;
  154. },
  155. /**
  156. * @constant
  157. * @default
  158. */
  159. SHOW_IN_BINARY: false,
  160. /**
  161. * @constant
  162. * @default
  163. */
  164. OFFSETS_SHOW_VARIABLE: true,
  165. /**
  166. * Show Base64 offsets operation.
  167. *
  168. * @param {byteArray} input
  169. * @param {Object[]} args
  170. * @returns {html}
  171. */
  172. runOffsets: function(input, args) {
  173. let alphabet = args[0] || Base64.ALPHABET,
  174. showVariable = args[1],
  175. offset0 = Utils.toBase64(input, alphabet),
  176. offset1 = Utils.toBase64([0].concat(input), alphabet),
  177. offset2 = Utils.toBase64([0, 0].concat(input), alphabet),
  178. len0 = offset0.indexOf("="),
  179. len1 = offset1.indexOf("="),
  180. len2 = offset2.indexOf("="),
  181. script = "<script type='application/javascript'>$('[data-toggle=\"tooltip\"]').tooltip()</script>",
  182. staticSection = "",
  183. padding = "";
  184. if (input.length < 1) {
  185. return "Please enter a string.";
  186. }
  187. // Highlight offset 0
  188. if (len0 % 4 === 2) {
  189. staticSection = offset0.slice(0, -3);
  190. offset0 = "<span data-toggle='tooltip' data-placement='top' title='" +
  191. Utils.escapeHtml(Utils.fromBase64(staticSection, alphabet).slice(0, -2)) + "'>" +
  192. staticSection + "</span>" +
  193. "<span class='hl5'>" + offset0.substr(offset0.length - 3, 1) + "</span>" +
  194. "<span class='hl3'>" + offset0.substr(offset0.length - 2) + "</span>";
  195. } else if (len0 % 4 === 3) {
  196. staticSection = offset0.slice(0, -2);
  197. offset0 = "<span data-toggle='tooltip' data-placement='top' title='" +
  198. Utils.escapeHtml(Utils.fromBase64(staticSection, alphabet).slice(0, -1)) + "'>" +
  199. staticSection + "</span>" +
  200. "<span class='hl5'>" + offset0.substr(offset0.length - 2, 1) + "</span>" +
  201. "<span class='hl3'>" + offset0.substr(offset0.length - 1) + "</span>";
  202. } else {
  203. staticSection = offset0;
  204. offset0 = "<span data-toggle='tooltip' data-placement='top' title='" +
  205. Utils.escapeHtml(Utils.fromBase64(staticSection, alphabet)) + "'>" +
  206. staticSection + "</span>";
  207. }
  208. if (!showVariable) {
  209. offset0 = staticSection;
  210. }
  211. // Highlight offset 1
  212. padding = "<span class='hl3'>" + offset1.substr(0, 1) + "</span>" +
  213. "<span class='hl5'>" + offset1.substr(1, 1) + "</span>";
  214. offset1 = offset1.substr(2);
  215. if (len1 % 4 === 2) {
  216. staticSection = offset1.slice(0, -3);
  217. offset1 = padding + "<span data-toggle='tooltip' data-placement='top' title='" +
  218. Utils.escapeHtml(Utils.fromBase64("AA" + staticSection, alphabet).slice(1, -2)) + "'>" +
  219. staticSection + "</span>" +
  220. "<span class='hl5'>" + offset1.substr(offset1.length - 3, 1) + "</span>" +
  221. "<span class='hl3'>" + offset1.substr(offset1.length - 2) + "</span>";
  222. } else if (len1 % 4 === 3) {
  223. staticSection = offset1.slice(0, -2);
  224. offset1 = padding + "<span data-toggle='tooltip' data-placement='top' title='" +
  225. Utils.escapeHtml(Utils.fromBase64("AA" + staticSection, alphabet).slice(1, -1)) + "'>" +
  226. staticSection + "</span>" +
  227. "<span class='hl5'>" + offset1.substr(offset1.length - 2, 1) + "</span>" +
  228. "<span class='hl3'>" + offset1.substr(offset1.length - 1) + "</span>";
  229. } else {
  230. staticSection = offset1;
  231. offset1 = padding + "<span data-toggle='tooltip' data-placement='top' title='" +
  232. Utils.escapeHtml(Utils.fromBase64("AA" + staticSection, alphabet).slice(1)) + "'>" +
  233. staticSection + "</span>";
  234. }
  235. if (!showVariable) {
  236. offset1 = staticSection;
  237. }
  238. // Highlight offset 2
  239. padding = "<span class='hl3'>" + offset2.substr(0, 2) + "</span>" +
  240. "<span class='hl5'>" + offset2.substr(2, 1) + "</span>";
  241. offset2 = offset2.substr(3);
  242. if (len2 % 4 === 2) {
  243. staticSection = offset2.slice(0, -3);
  244. offset2 = padding + "<span data-toggle='tooltip' data-placement='top' title='" +
  245. Utils.escapeHtml(Utils.fromBase64("AAA" + staticSection, alphabet).slice(2, -2)) + "'>" +
  246. staticSection + "</span>" +
  247. "<span class='hl5'>" + offset2.substr(offset2.length - 3, 1) + "</span>" +
  248. "<span class='hl3'>" + offset2.substr(offset2.length - 2) + "</span>";
  249. } else if (len2 % 4 === 3) {
  250. staticSection = offset2.slice(0, -2);
  251. offset2 = padding + "<span data-toggle='tooltip' data-placement='top' title='" +
  252. Utils.escapeHtml(Utils.fromBase64("AAA" + staticSection, alphabet).slice(2, -2)) + "'>" +
  253. staticSection + "</span>" +
  254. "<span class='hl5'>" + offset2.substr(offset2.length - 2, 1) + "</span>" +
  255. "<span class='hl3'>" + offset2.substr(offset2.length - 1) + "</span>";
  256. } else {
  257. staticSection = offset2;
  258. offset2 = padding + "<span data-toggle='tooltip' data-placement='top' title='" +
  259. Utils.escapeHtml(Utils.fromBase64("AAA" + staticSection, alphabet).slice(2)) + "'>" +
  260. staticSection + "</span>";
  261. }
  262. if (!showVariable) {
  263. offset2 = staticSection;
  264. }
  265. return (showVariable ? "Characters highlighted in <span class='hl5'>green</span> could change if the input is surrounded by more data." +
  266. "\nCharacters highlighted in <span class='hl3'>red</span> are for padding purposes only." +
  267. "\nUnhighlighted characters are <span data-toggle='tooltip' data-placement='top' title='Tooltip on left'>static</span>." +
  268. "\nHover over the static sections to see what they decode to on their own.\n" +
  269. "\nOffset 0: " + offset0 +
  270. "\nOffset 1: " + offset1 +
  271. "\nOffset 2: " + offset2 +
  272. script :
  273. offset0 + "\n" + offset1 + "\n" + offset2);
  274. },
  275. /**
  276. * Highlight to Base64
  277. *
  278. * @param {Object[]} pos
  279. * @param {number} pos[].start
  280. * @param {number} pos[].end
  281. * @param {Object[]} args
  282. * @returns {Object[]} pos
  283. */
  284. highlightTo: function(pos, args) {
  285. pos[0].start = Math.floor(pos[0].start / 3 * 4);
  286. pos[0].end = Math.ceil(pos[0].end / 3 * 4);
  287. return pos;
  288. },
  289. /**
  290. * Highlight from Base64
  291. *
  292. * @param {Object[]} pos
  293. * @param {number} pos[].start
  294. * @param {number} pos[].end
  295. * @param {Object[]} args
  296. * @returns {Object[]} pos
  297. */
  298. highlightFrom: function(pos, args) {
  299. pos[0].start = Math.ceil(pos[0].start / 4 * 3);
  300. pos[0].end = Math.floor(pos[0].end / 4 * 3);
  301. return pos;
  302. },
  303. };
  304. export default Base64;