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.

enc-base64.js 4.0 KiB

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