Base64.js 12 KB

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