爱简收旧衣按件回收前端代码仓库
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.

93 lines
2.9 KiB

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