UNPKG

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