Blame view

public/assets/addons/fastflow/plugins/jsPDF-master/src/libs/pdfsecurity.js 4.75 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  /**

   * @license

   * Licensed under the MIT License.

   * http://opensource.org/licenses/mit-license

   * Author: Owen Leong (@owenl131)

   * Date: 15 Oct 2020

   * References:

   * https://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt

   * https://github.com/foliojs/pdfkit/blob/master/lib/security.js

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

   */

  

  import { md5Bin } from "./md5.js";

  import { rc4 } from "./rc4.js";

  

  var permissionOptions = {

    print: 4,

    modify: 8,

    copy: 16,

    "annot-forms": 32

  };

  

  /**

   * Initializes encryption settings

   *

   * @name constructor

   * @function

   * @param {Array} permissions Permissions allowed for user, "print", "modify", "copy" and "annot-forms".

   * @param {String} userPassword Permissions apply to this user. Leaving this empty means the document

   *                              is not password protected but viewer has the above permissions.

   * @param {String} ownerPassword Owner has full functionalities to the file.

   * @param {String} fileId As hex string, should be same as the file ID in the trailer.

   * @example

   * var security = new PDFSecurity(["print"])

   */

  function PDFSecurity(permissions, userPassword, ownerPassword, fileId) {

    this.v = 1; // algorithm 1, future work can add in more recent encryption schemes

    this.r = 2; // revision 2

  

    // set flags for what functionalities the user can access

    let protection = 192;

    permissions.forEach(function(perm) {

      if (typeof permissionOptions.perm !== "undefined") {

        throw new Error("Invalid permission: " + perm);

      }

      protection += permissionOptions[perm];

    });

  

    // padding is used to pad the passwords to 32 bytes, also is hashed and stored in the final PDF

    this.padding =

      "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08" +

      "\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";

    let paddedUserPassword = (userPassword + this.padding).substr(0, 32);

    let paddedOwnerPassword = (ownerPassword + this.padding).substr(0, 32);

  

    this.O = this.processOwnerPassword(paddedUserPassword, paddedOwnerPassword);

    this.P = -((protection ^ 255) + 1);

    this.encryptionKey = md5Bin(

      paddedUserPassword +

        this.O +

        this.lsbFirstWord(this.P) +

        this.hexToBytes(fileId)

    ).substr(0, 5);

    this.U = rc4(this.encryptionKey, this.padding);

  }

  

  /**

   * Breaks down a 4-byte number into its individual bytes, with the least significant bit first

   *

   * @name lsbFirstWord

   * @function

   * @param {number} data 32-bit number

   * @returns {Array}

   */

  PDFSecurity.prototype.lsbFirstWord = function(data) {

    return String.fromCharCode(

      (data >> 0) & 0xff,

      (data >> 8) & 0xff,

      (data >> 16) & 0xff,

      (data >> 24) & 0xff

    );

  };

  

  /**

   * Converts a byte string to a hex string

   *

   * @name toHexString

   * @function

   * @param {String} byteString Byte string

   * @returns {String}

   */

  PDFSecurity.prototype.toHexString = function(byteString) {

    return byteString

      .split("")

      .map(function(byte) {

        return ("0" + (byte.charCodeAt(0) & 0xff).toString(16)).slice(-2);

      })

      .join("");

  };

  

  /**

   * Converts a hex string to a byte string

   *

   * @name hexToBytes

   * @function

   * @param {String} hex Hex string

   * @returns {String}

   */

  PDFSecurity.prototype.hexToBytes = function(hex) {

    for (var bytes = [], c = 0; c < hex.length; c += 2)

      bytes.push(String.fromCharCode(parseInt(hex.substr(c, 2), 16)));

    return bytes.join("");

  };

  

  /**

   * Computes the 'O' field in the encryption dictionary

   *

   * @name processOwnerPassword

   * @function

   * @param {String} paddedUserPassword Byte string of padded user password

   * @param {String} paddedOwnerPassword Byte string of padded owner password

   * @returns {String}

   */

  PDFSecurity.prototype.processOwnerPassword = function(

    paddedUserPassword,

    paddedOwnerPassword

  ) {

    let key = md5Bin(paddedOwnerPassword).substr(0, 5);

    return rc4(key, paddedUserPassword);

  };

  

  /**

   * Returns an encryptor function which can take in a byte string and returns the encrypted version

   *

   * @name encryptor

   * @function

   * @param {number} objectId

   * @param {number} generation Not sure what this is for, you can set it to 0

   * @returns {Function}

   * @example

   * out("stream");

   * encryptor = security.encryptor(object.id, 0);

   * out(encryptor(data));

   * out("endstream");

   */

  PDFSecurity.prototype.encryptor = function(objectId, generation) {

    let key = md5Bin(

      this.encryptionKey +

        String.fromCharCode(

          objectId & 0xff,

          (objectId >> 8) & 0xff,

          (objectId >> 16) & 0xff,

          generation & 0xff,

          (generation >> 8) & 0xff

        )

    ).substr(0, 10);

    return function(data) {

      return rc4(key, data);

    };

  };

  

  export { PDFSecurity };