enc-base64url.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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) {
  41. if (urlSafe === undefined) {
  42. urlSafe = true
  43. }
  44. // Shortcuts
  45. var words = wordArray.words;
  46. var sigBytes = wordArray.sigBytes;
  47. var map = urlSafe ? this._safe_map : this._map;
  48. // Clamp excess bits
  49. wordArray.clamp();
  50. // Convert
  51. var base64Chars = [];
  52. for (var i = 0; i < sigBytes; i += 3) {
  53. var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  54. var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
  55. var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
  56. var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
  57. for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
  58. base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
  59. }
  60. }
  61. // Add padding
  62. var paddingChar = map.charAt(64);
  63. if (paddingChar) {
  64. while (base64Chars.length % 4) {
  65. base64Chars.push(paddingChar);
  66. }
  67. }
  68. return base64Chars.join('');
  69. },
  70. /**
  71. * Converts a Base64url string to a word array.
  72. *
  73. * @param {string} base64Str The Base64url string.
  74. *
  75. * @param {boolean} urlSafe Whether to use url safe
  76. *
  77. * @return {WordArray} The word array.
  78. *
  79. * @static
  80. *
  81. * @example
  82. *
  83. * var wordArray = CryptoJS.enc.Base64url.parse(base64String);
  84. */
  85. parse: function (base64Str, urlSafe) {
  86. if (urlSafe === undefined) {
  87. urlSafe = true
  88. }
  89. // Shortcuts
  90. var base64StrLength = base64Str.length;
  91. var map = urlSafe ? this._safe_map : this._map;
  92. var reverseMap = this._reverseMap;
  93. if (!reverseMap) {
  94. reverseMap = this._reverseMap = [];
  95. for (var j = 0; j < map.length; j++) {
  96. reverseMap[map.charCodeAt(j)] = j;
  97. }
  98. }
  99. // Ignore padding
  100. var paddingChar = map.charAt(64);
  101. if (paddingChar) {
  102. var paddingIndex = base64Str.indexOf(paddingChar);
  103. if (paddingIndex !== -1) {
  104. base64StrLength = paddingIndex;
  105. }
  106. }
  107. // Convert
  108. return parseLoop(base64Str, base64StrLength, reverseMap);
  109. },
  110. _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  111. _safe_map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
  112. };
  113. function parseLoop(base64Str, base64StrLength, reverseMap) {
  114. var words = [];
  115. var nBytes = 0;
  116. for (var i = 0; i < base64StrLength; i++) {
  117. if (i % 4) {
  118. var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
  119. var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
  120. var bitsCombined = bits1 | bits2;
  121. words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8);
  122. nBytes++;
  123. }
  124. }
  125. return WordArray.create(words, nBytes);
  126. }
  127. }());
  128. return CryptoJS.enc.Base64url;
  129. }));