base64.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. module.exports ={
  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 = this._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 +
  24. this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  25. this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  26. }
  27. return output;
  28. },
  29. // public method for decoding
  30. decode: function (input) {
  31. var output = "";
  32. var chr1, chr2, chr3;
  33. var enc1, enc2, enc3, enc4;
  34. var i = 0;
  35. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  36. while (i < input.length) {
  37. enc1 = this._keyStr.indexOf(input.charAt(i++));
  38. enc2 = this._keyStr.indexOf(input.charAt(i++));
  39. enc3 = this._keyStr.indexOf(input.charAt(i++));
  40. enc4 = this._keyStr.indexOf(input.charAt(i++));
  41. chr1 = (enc1 << 2) | (enc2 >> 4);
  42. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  43. chr3 = ((enc3 & 3) << 6) | enc4;
  44. output = output + String.fromCharCode(chr1);
  45. if (enc3 != 64) {
  46. output = output + String.fromCharCode(chr2);
  47. }
  48. if (enc4 != 64) {
  49. output = output + String.fromCharCode(chr3);
  50. }
  51. }
  52. output = this._utf8_decode(output);
  53. return output;
  54. },
  55. // private method for UTF-8 encoding
  56. _utf8_encode: function (string) {
  57. string = string.replace(/\r\n/g, "\n");
  58. var utftext = "";
  59. for (var n = 0; n < string.length; n++) {
  60. var c = string.charCodeAt(n);
  61. if (c < 128) {
  62. utftext += String.fromCharCode(c);
  63. }
  64. else if ((c > 127) && (c < 2048)) {
  65. utftext += String.fromCharCode((c >> 6) | 192);
  66. utftext += String.fromCharCode((c & 63) | 128);
  67. }
  68. else {
  69. utftext += String.fromCharCode((c >> 12) | 224);
  70. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  71. utftext += String.fromCharCode((c & 63) | 128);
  72. }
  73. }
  74. return utftext;
  75. },
  76. // private method for UTF-8 decoding
  77. _utf8_decode: function (utftext) {
  78. var string = "";
  79. var i = 0;
  80. var c = c1 = c2 = 0;
  81. while (i < utftext.length) {
  82. c = utftext.charCodeAt(i);
  83. if (c < 128) {
  84. string += String.fromCharCode(c);
  85. i++;
  86. }
  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. }
  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. }