1
0

base64.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. *
  3. * Base64 encode / decode
  4. * Source: http://www.webtoolkit.info/
  5. * Modified by: @lucascalion - 24/07/2019
  6. *
  7. **/
  8. const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  9. export function encode (input) {
  10. let output = "";
  11. let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  12. let i = 0;
  13. input = _utf8_encode(input);
  14. while (i < input.length) {
  15. chr1 = input.charCodeAt(i++);
  16. chr2 = input.charCodeAt(i++);
  17. chr3 = input.charCodeAt(i++);
  18. enc1 = chr1 >> 2;
  19. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  20. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  21. enc4 = chr3 & 63;
  22. if (isNaN(chr2)) {
  23. enc3 = enc4 = 64;
  24. } else if (isNaN(chr3)) {
  25. enc4 = 64;
  26. }
  27. output = output +
  28. _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  29. _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  30. }
  31. return output;
  32. }
  33. export function decode (input) {
  34. let output = "";
  35. let chr1, chr2, chr3;
  36. let enc1, enc2, enc3, enc4;
  37. let i = 0;
  38. input = input.replace(/[^A-Za-z0-9+/=]/g, "");
  39. while (i < input.length) {
  40. enc1 = _keyStr.indexOf(input.charAt(i++));
  41. enc2 = _keyStr.indexOf(input.charAt(i++));
  42. enc3 = _keyStr.indexOf(input.charAt(i++));
  43. enc4 = _keyStr.indexOf(input.charAt(i++));
  44. chr1 = (enc1 << 2) | (enc2 >> 4);
  45. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  46. chr3 = ((enc3 & 3) << 6) | enc4;
  47. output = output + String.fromCharCode(chr1);
  48. if (enc3 != 64) {
  49. output = output + String.fromCharCode(chr2);
  50. }
  51. if (enc4 != 64) {
  52. output = output + String.fromCharCode(chr3);
  53. }
  54. }
  55. output = _utf8_decode(output);
  56. return output;
  57. }
  58. function _utf8_encode (string) {
  59. string = string.replace(/\r\n/g,"\n");
  60. let utftext = "";
  61. for (let n = 0; n < string.length; n++) {
  62. const c = string.charCodeAt(n);
  63. if (c < 128) {
  64. utftext += String.fromCharCode(c);
  65. }
  66. else if((c > 127) && (c < 2048)) {
  67. utftext += String.fromCharCode((c >> 6) | 192);
  68. utftext += String.fromCharCode((c & 63) | 128);
  69. }
  70. else {
  71. utftext += String.fromCharCode((c >> 12) | 224);
  72. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  73. utftext += String.fromCharCode((c & 63) | 128);
  74. }
  75. }
  76. return utftext;
  77. }
  78. function _utf8_decode (utftext) {
  79. let string = "";
  80. let i = 0;
  81. let c, c2, c3;
  82. c = c2 = c3 = 0;
  83. while ( i < utftext.length ) {
  84. c = utftext.charCodeAt(i);
  85. if (c < 128) {
  86. string += String.fromCharCode(c);
  87. i++;
  88. }
  89. else if((c > 191) && (c < 224)) {
  90. c2 = utftext.charCodeAt(i+1);
  91. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  92. i += 2;
  93. }
  94. else {
  95. c2 = utftext.charCodeAt(i+1);
  96. c3 = utftext.charCodeAt(i+2);
  97. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  98. i += 3;
  99. }
  100. }
  101. return string;
  102. }