enc-base64.js 4.3 KB

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