Blame view

public/assets/addons/fastflow/plugins/jsPDF-master/src/libs/rc4.js 1.4 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
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
  /**

   * @license

   * FPDF is released under a permissive license: there is no usage restriction.

   * You may embed it freely in your application (commercial or not), with or

   * without modifications.

   *

   * Reference: http://www.fpdf.org/en/script/script37.php

   */

  

  function repeat(str, num) {

    return new Array(num + 1).join(str);

  }

  

  /**

   * Converts a byte string to a hex string

   *

   * @name rc4

   * @function

   * @param {string} key Byte string of encryption key

   * @param {string} data Byte string of data to be encrypted

   * @returns {string} Encrypted string

   */

  function rc4(key, data) {

    var lastKey, lastState;

    if (key !== lastKey) {

      var k = repeat(key, ((256 / key.length) >> 0) + 1);

      var state = [];

      for (var i = 0; i < 256; i++) {

        state[i] = i;

      }

      var j = 0;

      for (var i = 0; i < 256; i++) {

        var t = state[i];

        j = (j + t + k.charCodeAt(i)) % 256;

        state[i] = state[j];

        state[j] = t;

      }

      lastKey = key;

      lastState = state;

    } else {

      state = lastState;

    }

    var length = data.length;

    var a = 0;

    var b = 0;

    var out = "";

    for (var i = 0; i < length; i++) {

      a = (a + 1) % 256;

      t = state[a];

      b = (b + t) % 256;

      state[a] = state[b];

      state[b] = t;

      k = state[(state[a] + state[b]) % 256];

      out += String.fromCharCode(data.charCodeAt(i) ^ k);

    }

    return out;

  }

  

  export { rc4 };