base64.js 3.2 KB

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