Base64.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Base64 encode / decode
  3. */
  4. function Base64() {
  5. // private property
  6. var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  7. // public method for encoding
  8. this.encode = function (input) {
  9. var output = "";
  10. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  11. var i = 0;
  12. //input = _utf8_encode(input);
  13. while (i < input.length) {
  14. chr1 = input.charCodeAt(i++);
  15. chr2 = input.charCodeAt(i++);
  16. chr3 = input.charCodeAt(i++);
  17. enc1 = chr1 >> 2;
  18. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  19. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  20. enc4 = chr3 & 63;
  21. if (isNaN(chr2)) {
  22. enc3 = enc4 = 64;
  23. } else if (isNaN(chr3)) {
  24. enc4 = 64;
  25. }
  26. output = output +
  27. _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  28. _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  29. }
  30. return output;
  31. };
  32. // public method for decoding
  33. this.decode = function (input) {
  34. var output = "";
  35. var chr1, chr2, chr3;
  36. var enc1, enc2, enc3, enc4;
  37. var 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. // private method for UTF-8 encoding
  59. var _utf8_encode = function (string) {
  60. string = string.replace(/\r\n/g, "\n");
  61. var utftext = "";
  62. for (var n = 0; n < string.length; n++) {
  63. var c = string.charCodeAt(n);
  64. if (c < 128) {
  65. utftext += String.fromCharCode(c);
  66. } else if ((c > 127) && (c < 2048)) {
  67. utftext += String.fromCharCode((c >> 6) | 192);
  68. utftext += String.fromCharCode((c & 63) | 128);
  69. } else {
  70. utftext += String.fromCharCode((c >> 12) | 224);
  71. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  72. utftext += String.fromCharCode((c & 63) | 128);
  73. }
  74. }
  75. return utftext;
  76. }
  77. // private method for UTF-8 decoding
  78. var _utf8_decode = function (utftext) {
  79. var string = "";
  80. var i = 0;
  81. var c = c1 = c2 = 0;
  82. while (i < utftext.length) {
  83. c = utftext.charCodeAt(i);
  84. if (c < 128) {
  85. string += String.fromCharCode(c);
  86. i++;
  87. } else if ((c > 191) && (c < 224)) {
  88. c2 = utftext.charCodeAt(i + 1);
  89. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  90. i += 2;
  91. } else {
  92. c2 = utftext.charCodeAt(i + 1);
  93. c3 = utftext.charCodeAt(i + 2);
  94. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  95. i += 3;
  96. }
  97. }
  98. return string;
  99. }
  100. }
  101. const base = new Base64();
  102. export {
  103. base
  104. }