You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.1 KiB

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