lgq
3 天以前 081f12a52906abe6c2d139fdc144135978681009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {
  WordArray,
} from './core.js';
import {
  parseLoop,
} from './enc-base64.js'
 
/**
 * Base64url encoding strategy.
 */
export const Base64url = {
  /**
   * Converts a word array to a Base64url string.
   *
   * @param {WordArray} wordArray The word array.
   * 
   * @param {boolean} urlSafe Whether to use url safe.
   *
   * @return {string} The Base64url string.
   *
   * @static
   *
   * @example
   *
   *     const base64String = CryptoJS.enc.Base64.stringify(wordArray);
   */
  stringify(wordArray, urlSafe = true) {
    // Shortcuts
    const { words, sigBytes } = wordArray;
    const map = urlSafe ? this._safeMap : this._map;
 
    // Clamp excess bits
    wordArray.clamp();
 
    // Convert
    const base64Chars = [];
    for (let i = 0; i < sigBytes; i += 3) {
      const byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
      const byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
      const byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
 
      const triplet = (byte1 << 16) | (byte2 << 8) | byte3;
 
      for (let j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j += 1) {
        base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
      }
    }
 
    // Add padding
    const paddingChar = map.charAt(64);
    if (paddingChar) {
      while (base64Chars.length % 4) {
        base64Chars.push(paddingChar);
      }
    }
 
    return base64Chars.join('');
  },
 
  /**
   * Converts a Base64url string to a word array.
   *
   * @param {string} base64Str The Base64url string.
   * 
   * @param {boolean} urlSafe Whether to use url safe.
   *
   * @return {WordArray} The word array.
   *
   * @static
   *
   * @example
   *
   *     const wordArray = CryptoJS.enc.Base64.parse(base64String);
   */
  parse(base64Str, urlSafe = true) {
    // Shortcuts
    let base64StrLength = base64Str.length;
    const map = urlSafe ? this._safeMap : this._map;
    let reverseMap = this._reverseMap;
 
    if (!reverseMap) {
      this._reverseMap = [];
      reverseMap = this._reverseMap;
      for (let j = 0; j < map.length; j += 1) {
        reverseMap[map.charCodeAt(j)] = j;
      }
    }
 
    // Ignore padding
    const paddingChar = map.charAt(64);
    if (paddingChar) {
      const paddingIndex = base64Str.indexOf(paddingChar);
      if (paddingIndex !== -1) {
        base64StrLength = paddingIndex;
      }
    }
 
    // Convert
    return parseLoop(base64Str, base64StrLength, reverseMap);
  },
 
  _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
 
  _safeMap: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
};