base64.js 3.6 KB

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