UNPKG

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