拓恒河湖长制全民护河平台WEB端
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.

140 lines
4.3KB

  1. ;(function (root, factory) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"));
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. // AMD
  8. define(["./core"], factory);
  9. }
  10. else {
  11. // Global (browser)
  12. factory(root.CryptoJS);
  13. }
  14. }(this, function (CryptoJS) {
  15. (function () {
  16. // Shortcuts
  17. var C = CryptoJS;
  18. var C_lib = C.lib;
  19. var WordArray = C_lib.WordArray;
  20. var C_enc = C.enc;
  21. /**
  22. * Base64url encoding strategy.
  23. */
  24. var Base64url = C_enc.Base64url = {
  25. /**
  26. * Converts a word array to a Base64url string.
  27. *
  28. * @param {WordArray} wordArray The word array.
  29. *
  30. * @param {boolean} urlSafe Whether to use url safe
  31. *
  32. * @return {string} The Base64url string.
  33. *
  34. * @static
  35. *
  36. * @example
  37. *
  38. * var base64String = CryptoJS.enc.Base64url.stringify(wordArray);
  39. */
  40. stringify: function (wordArray, urlSafe=true) {
  41. // Shortcuts
  42. var words = wordArray.words;
  43. var sigBytes = wordArray.sigBytes;
  44. var map = urlSafe ? this._safe_map : this._map;
  45. // Clamp excess bits
  46. wordArray.clamp();
  47. // Convert
  48. var base64Chars = [];
  49. for (var i = 0; i < sigBytes; i += 3) {
  50. var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  51. var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
  52. var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
  53. var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
  54. for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
  55. base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
  56. }
  57. }
  58. // Add padding
  59. var paddingChar = map.charAt(64);
  60. if (paddingChar) {
  61. while (base64Chars.length % 4) {
  62. base64Chars.push(paddingChar);
  63. }
  64. }
  65. return base64Chars.join('');
  66. },
  67. /**
  68. * Converts a Base64url string to a word array.
  69. *
  70. * @param {string} base64Str The Base64url string.
  71. *
  72. * @param {boolean} urlSafe Whether to use url safe
  73. *
  74. * @return {WordArray} The word array.
  75. *
  76. * @static
  77. *
  78. * @example
  79. *
  80. * var wordArray = CryptoJS.enc.Base64url.parse(base64String);
  81. */
  82. parse: function (base64Str, urlSafe=true) {
  83. // Shortcuts
  84. var base64StrLength = base64Str.length;
  85. var map = urlSafe ? this._safe_map : this._map;
  86. var reverseMap = this._reverseMap;
  87. if (!reverseMap) {
  88. reverseMap = this._reverseMap = [];
  89. for (var j = 0; j < map.length; j++) {
  90. reverseMap[map.charCodeAt(j)] = j;
  91. }
  92. }
  93. // Ignore padding
  94. var paddingChar = map.charAt(64);
  95. if (paddingChar) {
  96. var paddingIndex = base64Str.indexOf(paddingChar);
  97. if (paddingIndex !== -1) {
  98. base64StrLength = paddingIndex;
  99. }
  100. }
  101. // Convert
  102. return parseLoop(base64Str, base64StrLength, reverseMap);
  103. },
  104. _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  105. _safe_map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
  106. };
  107. function parseLoop(base64Str, base64StrLength, reverseMap) {
  108. var words = [];
  109. var nBytes = 0;
  110. for (var i = 0; i < base64StrLength; i++) {
  111. if (i % 4) {
  112. var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
  113. var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
  114. var bitsCombined = bits1 | bits2;
  115. words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8);
  116. nBytes++;
  117. }
  118. }
  119. return WordArray.create(words, nBytes);
  120. }
  121. }());
  122. return CryptoJS.enc.Base64url;
  123. }));