AI销管
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

enc-base64url.js 4.3 KiB

il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. }));