@protontech/openpgp 4.10.6 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/README.md +311 -239
  2. package/dist/lightweight/bn.interface.min.mjs +3 -0
  3. package/dist/lightweight/bn.interface.min.mjs.map +1 -0
  4. package/dist/lightweight/bn.interface.mjs +340 -0
  5. package/dist/lightweight/bn.min.mjs +3 -0
  6. package/dist/lightweight/bn.min.mjs.map +1 -0
  7. package/dist/lightweight/bn.mjs +3434 -0
  8. package/dist/lightweight/elliptic.min.mjs +3 -0
  9. package/dist/lightweight/elliptic.min.mjs.map +1 -0
  10. package/dist/lightweight/elliptic.mjs +4313 -0
  11. package/dist/lightweight/openpgp.min.mjs +3 -0
  12. package/dist/lightweight/openpgp.min.mjs.map +1 -0
  13. package/dist/lightweight/openpgp.mjs +31379 -0
  14. package/dist/lightweight/ponyfill.es6.min.mjs +3 -0
  15. package/dist/lightweight/ponyfill.es6.min.mjs.map +1 -0
  16. package/dist/lightweight/ponyfill.es6.mjs +3831 -0
  17. package/dist/lightweight/web-streams-adapter.min.mjs +17 -0
  18. package/dist/lightweight/web-streams-adapter.min.mjs.map +1 -0
  19. package/dist/lightweight/web-streams-adapter.mjs +561 -0
  20. package/dist/node/openpgp.js +43947 -0
  21. package/dist/node/openpgp.min.js +17 -0
  22. package/dist/node/openpgp.min.js.map +1 -0
  23. package/dist/node/openpgp.min.mjs +17 -0
  24. package/dist/node/openpgp.min.mjs.map +1 -0
  25. package/dist/node/openpgp.mjs +43884 -0
  26. package/dist/openpgp.js +41082 -41563
  27. package/dist/openpgp.min.js +17 -2
  28. package/dist/openpgp.min.js.map +1 -0
  29. package/dist/openpgp.min.mjs +17 -0
  30. package/dist/openpgp.min.mjs.map +1 -0
  31. package/dist/openpgp.mjs +43872 -0
  32. package/lightweight/package.json +5 -0
  33. package/openpgp.d.ts +890 -0
  34. package/package.json +63 -57
  35. package/dist/compat/openpgp.js +0 -61067
  36. package/dist/compat/openpgp.min.js +0 -2
  37. package/dist/compat/openpgp.worker.js +0 -173
  38. package/dist/compat/openpgp.worker.min.js +0 -2
  39. package/dist/lightweight/elliptic.min.js +0 -5
  40. package/dist/lightweight/openpgp.js +0 -40024
  41. package/dist/lightweight/openpgp.min.js +0 -2
  42. package/dist/lightweight/openpgp.worker.js +0 -173
  43. package/dist/lightweight/openpgp.worker.min.js +0 -2
  44. package/dist/openpgp.worker.js +0 -173
  45. package/dist/openpgp.worker.min.js +0 -2
  46. package/src/cleartext.js +0 -220
  47. package/src/config/config.js +0 -224
  48. package/src/config/index.js +0 -7
  49. package/src/config/localStorage.js +0 -35
  50. package/src/crypto/aes_kw.js +0 -153
  51. package/src/crypto/cfb.js +0 -169
  52. package/src/crypto/cipher/aes.js +0 -27
  53. package/src/crypto/cipher/blowfish.js +0 -398
  54. package/src/crypto/cipher/cast5.js +0 -610
  55. package/src/crypto/cipher/des.js +0 -476
  56. package/src/crypto/cipher/index.js +0 -91
  57. package/src/crypto/cipher/twofish.js +0 -346
  58. package/src/crypto/cmac.js +0 -98
  59. package/src/crypto/crypto.js +0 -394
  60. package/src/crypto/eax.js +0 -172
  61. package/src/crypto/gcm.js +0 -141
  62. package/src/crypto/hash/index.js +0 -163
  63. package/src/crypto/hash/md5.js +0 -205
  64. package/src/crypto/index.js +0 -57
  65. package/src/crypto/ocb.js +0 -274
  66. package/src/crypto/pkcs1.js +0 -170
  67. package/src/crypto/pkcs5.js +0 -55
  68. package/src/crypto/public_key/dsa.js +0 -188
  69. package/src/crypto/public_key/elgamal.js +0 -137
  70. package/src/crypto/public_key/elliptic/curves.js +0 -385
  71. package/src/crypto/public_key/elliptic/ecdh.js +0 -414
  72. package/src/crypto/public_key/elliptic/ecdsa.js +0 -348
  73. package/src/crypto/public_key/elliptic/eddsa.js +0 -119
  74. package/src/crypto/public_key/elliptic/index.js +0 -34
  75. package/src/crypto/public_key/elliptic/indutnyKey.js +0 -85
  76. package/src/crypto/public_key/index.js +0 -28
  77. package/src/crypto/public_key/prime.js +0 -275
  78. package/src/crypto/public_key/rsa.js +0 -597
  79. package/src/crypto/random.js +0 -145
  80. package/src/crypto/signature.js +0 -137
  81. package/src/encoding/armor.js +0 -433
  82. package/src/encoding/base64.js +0 -96
  83. package/src/enums.js +0 -493
  84. package/src/hkp.js +0 -89
  85. package/src/index.js +0 -161
  86. package/src/key/factory.js +0 -326
  87. package/src/key/helper.js +0 -363
  88. package/src/key/index.js +0 -32
  89. package/src/key/key.js +0 -890
  90. package/src/key/subkey.js +0 -187
  91. package/src/key/user.js +0 -230
  92. package/src/keyring/index.js +0 -12
  93. package/src/keyring/keyring.js +0 -229
  94. package/src/keyring/localstore.js +0 -119
  95. package/src/lightweight_helper.js +0 -26
  96. package/src/message.js +0 -825
  97. package/src/openpgp.js +0 -717
  98. package/src/packet/all_packets.js +0 -116
  99. package/src/packet/clone.js +0 -189
  100. package/src/packet/compressed.js +0 -194
  101. package/src/packet/index.js +0 -20
  102. package/src/packet/literal.js +0 -168
  103. package/src/packet/marker.js +0 -62
  104. package/src/packet/one_pass_signature.js +0 -156
  105. package/src/packet/packet.js +0 -300
  106. package/src/packet/packetlist.js +0 -232
  107. package/src/packet/public_key.js +0 -280
  108. package/src/packet/public_key_encrypted_session_key.js +0 -156
  109. package/src/packet/public_subkey.js +0 -44
  110. package/src/packet/secret_key.js +0 -448
  111. package/src/packet/secret_subkey.js +0 -41
  112. package/src/packet/signature.js +0 -782
  113. package/src/packet/sym_encrypted_aead_protected.js +0 -189
  114. package/src/packet/sym_encrypted_integrity_protected.js +0 -139
  115. package/src/packet/sym_encrypted_session_key.js +0 -204
  116. package/src/packet/symmetrically_encrypted.js +0 -118
  117. package/src/packet/trust.js +0 -35
  118. package/src/packet/user_attribute.js +0 -94
  119. package/src/packet/userid.js +0 -87
  120. package/src/polyfills.js +0 -64
  121. package/src/signature.js +0 -73
  122. package/src/type/ecdh_symkey.js +0 -69
  123. package/src/type/kdf_params.js +0 -114
  124. package/src/type/keyid.js +0 -110
  125. package/src/type/mpi.js +0 -138
  126. package/src/type/oid.js +0 -110
  127. package/src/type/s2k.js +0 -203
  128. package/src/util.js +0 -836
  129. package/src/wkd.js +0 -88
  130. package/src/worker/async_proxy.js +0 -190
  131. package/src/worker/worker.js +0 -167
  132. package/test/crypto/aes_kw.js +0 -57
  133. package/test/crypto/cipher/aes.js +0 -86
  134. package/test/crypto/cipher/blowfish.js +0 -58
  135. package/test/crypto/cipher/cast5.js +0 -25
  136. package/test/crypto/cipher/des.js +0 -143
  137. package/test/crypto/cipher/index.js +0 -7
  138. package/test/crypto/cipher/twofish.js +0 -71
  139. package/test/crypto/crypto.js +0 -383
  140. package/test/crypto/eax.js +0 -150
  141. package/test/crypto/ecdh.js +0 -359
  142. package/test/crypto/elliptic.js +0 -251
  143. package/test/crypto/elliptic_data.js +0 -102
  144. package/test/crypto/hash/index.js +0 -5
  145. package/test/crypto/hash/md5.js +0 -16
  146. package/test/crypto/hash/ripemd.js +0 -14
  147. package/test/crypto/hash/sha.js +0 -20
  148. package/test/crypto/index.js +0 -14
  149. package/test/crypto/ocb.js +0 -183
  150. package/test/crypto/pkcs5.js +0 -39
  151. package/test/crypto/random.js +0 -79
  152. package/test/crypto/rsa.js +0 -180
  153. package/test/crypto/validate.js +0 -387
  154. package/test/general/armor.js +0 -408
  155. package/test/general/brainpool.js +0 -360
  156. package/test/general/decompression.js +0 -60
  157. package/test/general/ecc_nist.js +0 -115
  158. package/test/general/ecc_secp256k1.js +0 -242
  159. package/test/general/forwarding.js +0 -43
  160. package/test/general/hkp.js +0 -165
  161. package/test/general/index.js +0 -20
  162. package/test/general/key.js +0 -3402
  163. package/test/general/keyring.js +0 -336
  164. package/test/general/oid.js +0 -39
  165. package/test/general/openpgp.js +0 -2542
  166. package/test/general/packet.js +0 -937
  167. package/test/general/signature.js +0 -1665
  168. package/test/general/streaming.js +0 -944
  169. package/test/general/testInputs.js +0 -18
  170. package/test/general/util.js +0 -183
  171. package/test/general/wkd.js +0 -48
  172. package/test/general/x25519.js +0 -556
  173. package/test/unittests.js +0 -64
package/src/util.js DELETED
@@ -1,836 +0,0 @@
1
- // GPG4Browsers - An OpenPGP implementation in javascript
2
- // Copyright (C) 2011 Recurity Labs GmbH
3
- //
4
- // This library is free software; you can redistribute it and/or
5
- // modify it under the terms of the GNU Lesser General Public
6
- // License as published by the Free Software Foundation; either
7
- // version 3.0 of the License, or (at your option) any later version.
8
- //
9
- // This library is distributed in the hope that it will be useful,
10
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- // Lesser General Public License for more details.
13
- //
14
- // You should have received a copy of the GNU Lesser General Public
15
- // License along with this library; if not, write to the Free Software
16
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
-
18
- /* eslint-disable no-console */
19
-
20
- /**
21
- * This object contains utility functions
22
- * @requires email-addresses
23
- * @requires web-stream-tools
24
- * @requires config
25
- * @requires encoding/base64
26
- * @module util
27
- */
28
-
29
- import emailAddresses from 'email-addresses';
30
- import stream from 'web-stream-tools';
31
- import config from './config';
32
- import util from './util'; // re-import module to access util functions
33
- import b64 from './encoding/base64';
34
-
35
- export default {
36
- isString: function(data) {
37
- return typeof data === 'string' || String.prototype.isPrototypeOf(data);
38
- },
39
-
40
- isArray: function(data) {
41
- return Array.prototype.isPrototypeOf(data);
42
- },
43
-
44
- isUint8Array: stream.isUint8Array,
45
-
46
- isStream: stream.isStream,
47
-
48
- /**
49
- * Get transferable objects to pass buffers with zero copy (similar to "pass by reference" in C++)
50
- * See: https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage
51
- * Also, convert ReadableStreams to MessagePorts
52
- * @param {Object} obj the options object to be passed to the web worker
53
- * @returns {Array<ArrayBuffer>} an array of binary data to be passed
54
- */
55
- getTransferables: function(obj, zero_copy) {
56
- const transferables = [];
57
- util.collectTransferables(obj, transferables, zero_copy);
58
- return transferables.length ? transferables : undefined;
59
- },
60
-
61
- collectTransferables: function(obj, collection, zero_copy) {
62
- if (!obj) {
63
- return;
64
- }
65
-
66
- if (util.isUint8Array(obj)) {
67
- if (zero_copy && collection.indexOf(obj.buffer) === -1 && !(
68
- navigator.userAgent.indexOf('Version/11.1') !== -1 || // Safari 11.1
69
- ((navigator.userAgent.match(/Chrome\/(\d+)/) || [])[1] < 56 && navigator.userAgent.indexOf('Edge') === -1) // Chrome < 56
70
- )) {
71
- collection.push(obj.buffer);
72
- }
73
- return;
74
- }
75
- if (Object.prototype.isPrototypeOf(obj)) {
76
- Object.entries(obj).forEach(([key, value]) => { // recursively search all children
77
- if (util.isStream(value)) {
78
- if (value.locked) {
79
- obj[key] = null;
80
- } else {
81
- const transformed = stream.transformPair(value, async readable => {
82
- const reader = stream.getReader(readable);
83
- const { port1, port2 } = new MessageChannel();
84
- port1.onmessage = async function({ data: { action } }) {
85
- if (action === 'read') {
86
- try {
87
- const result = await reader.read();
88
- port1.postMessage(result, util.getTransferables(result));
89
- } catch (e) {
90
- port1.postMessage({ error: e.message });
91
- }
92
- } else if (action === 'cancel') {
93
- await transformed.cancel();
94
- port1.postMessage();
95
- }
96
- };
97
- obj[key] = port2;
98
- collection.push(port2);
99
- });
100
- }
101
- return;
102
- }
103
- if (Object.prototype.toString.call(value) === '[object MessagePort]') {
104
- throw new Error("Can't transfer the same stream twice.");
105
- }
106
- util.collectTransferables(value, collection, zero_copy);
107
- });
108
- }
109
- },
110
-
111
- /**
112
- * Convert MessagePorts back to ReadableStreams
113
- * @param {Object} obj
114
- * @returns {Object}
115
- */
116
- restoreStreams: function(obj) {
117
- if (Object.prototype.isPrototypeOf(obj) && !Uint8Array.prototype.isPrototypeOf(obj)) {
118
- Object.entries(obj).forEach(([key, value]) => { // recursively search all children
119
- if (Object.prototype.toString.call(value) === '[object MessagePort]') {
120
- obj[key] = new ReadableStream({
121
- pull(controller) {
122
- return new Promise(resolve => {
123
- value.onmessage = evt => {
124
- const { done, value, error } = evt.data;
125
- if (error) {
126
- controller.error(new Error(error));
127
- } else if (!done) {
128
- controller.enqueue(value);
129
- } else {
130
- controller.close();
131
- }
132
- resolve();
133
- };
134
- value.postMessage({ action: 'read' });
135
- });
136
- },
137
- cancel() {
138
- return new Promise(resolve => {
139
- value.onmessage = resolve;
140
- value.postMessage({ action: 'cancel' });
141
- });
142
- }
143
- }, { highWaterMark: 0 });
144
- return;
145
- }
146
- util.restoreStreams(value);
147
- });
148
- }
149
- return obj;
150
- },
151
-
152
- readNumber: function (bytes) {
153
- let n = 0;
154
- for (let i = 0; i < bytes.length; i++) {
155
- n += (256 ** i) * bytes[bytes.length - 1 - i];
156
- }
157
- return n;
158
- },
159
-
160
- writeNumber: function (n, bytes) {
161
- const b = new Uint8Array(bytes);
162
- for (let i = 0; i < bytes; i++) {
163
- b[i] = (n >> (8 * (bytes - i - 1))) & 0xFF;
164
- }
165
-
166
- return b;
167
- },
168
-
169
- readDate: function (bytes) {
170
- const n = util.readNumber(bytes);
171
- const d = new Date(n * 1000);
172
- return d;
173
- },
174
-
175
- writeDate: function (time) {
176
- const numeric = Math.floor(time.getTime() / 1000);
177
-
178
- return util.writeNumber(numeric, 4);
179
- },
180
-
181
- normalizeDate: function (time = Date.now()) {
182
- return time === null || time === Infinity ? time : new Date(Math.floor(+time / 1000) * 1000);
183
- },
184
-
185
- /**
186
- * Create hex string from a binary
187
- * @param {String} str String to convert
188
- * @returns {String} String containing the hexadecimal values
189
- */
190
- str_to_hex: function (str) {
191
- if (str === null) {
192
- return "";
193
- }
194
- const r = [];
195
- const e = str.length;
196
- let c = 0;
197
- let h;
198
- while (c < e) {
199
- h = str.charCodeAt(c++).toString(16);
200
- while (h.length < 2) {
201
- h = "0" + h;
202
- }
203
- r.push("" + h);
204
- }
205
- return r.join('');
206
- },
207
-
208
- /**
209
- * Create binary string from a hex encoded string
210
- * @param {String} str Hex string to convert
211
- * @returns {String}
212
- */
213
- hex_to_str: function (hex) {
214
- let str = '';
215
- for (let i = 0; i < hex.length; i += 2) {
216
- str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
217
- }
218
- return str;
219
- },
220
-
221
- /**
222
- * Convert a Uint8Array to an MPI-formatted Uint8Array.
223
- * Note: the output is **not** an MPI object.
224
- * @see {@link module:type/mpi/MPI.fromUint8Array}
225
- * @see {@link module:type/mpi/MPI.toUint8Array}
226
- * @param {Uint8Array} bin An array of 8-bit integers to convert
227
- * @returns {Uint8Array} MPI-formatted Uint8Array
228
- */
229
- Uint8Array_to_MPI: function (bin) {
230
- const size = (bin.length - 1) * 8 + util.nbits(bin[0]);
231
- const prefix = Uint8Array.from([(size & 0xFF00) >> 8, size & 0xFF]);
232
- return util.concatUint8Array([prefix, bin]);
233
- },
234
-
235
- /**
236
- * Convert a Base-64 encoded string an array of 8-bit integer
237
- *
238
- * Note: accepts both Radix-64 and URL-safe strings
239
- * @param {String} base64 Base-64 encoded string to convert
240
- * @returns {Uint8Array} An array of 8-bit integers
241
- */
242
- b64_to_Uint8Array: function (base64) {
243
- return b64.decode(base64.replace(/-/g, '+').replace(/_/g, '/'));
244
- },
245
-
246
- /**
247
- * Convert an array of 8-bit integer to a Base-64 encoded string
248
- * @param {Uint8Array} bytes An array of 8-bit integers to convert
249
- * @param {bool} url If true, output is URL-safe
250
- * @returns {String} Base-64 encoded string
251
- */
252
- Uint8Array_to_b64: function (bytes, url) {
253
- let encoded = b64.encode(bytes).replace(/[\r\n]/g, '');
254
- if (url) {
255
- encoded = encoded.replace(/[+]/g, '-').replace(/[/]/g, '_').replace(/[=]/g, '');
256
- }
257
- return encoded;
258
- },
259
-
260
- /**
261
- * Convert a hex string to an array of 8-bit integers
262
- * @param {String} hex A hex string to convert
263
- * @returns {Uint8Array} An array of 8-bit integers
264
- */
265
- hex_to_Uint8Array: function (hex) {
266
- const result = new Uint8Array(hex.length >> 1);
267
- for (let k = 0; k < hex.length >> 1; k++) {
268
- result[k] = parseInt(hex.substr(k << 1, 2), 16);
269
- }
270
- return result;
271
- },
272
-
273
- /**
274
- * Convert an array of 8-bit integers to a hex string
275
- * @param {Uint8Array} bytes Array of 8-bit integers to convert
276
- * @returns {String} Hexadecimal representation of the array
277
- */
278
- Uint8Array_to_hex: function (bytes) {
279
- const r = [];
280
- const e = bytes.length;
281
- let c = 0;
282
- let h;
283
- while (c < e) {
284
- h = bytes[c++].toString(16);
285
- while (h.length < 2) {
286
- h = "0" + h;
287
- }
288
- r.push("" + h);
289
- }
290
- return r.join('');
291
- },
292
-
293
- /**
294
- * Convert a string to an array of 8-bit integers
295
- * @param {String} str String to convert
296
- * @returns {Uint8Array} An array of 8-bit integers
297
- */
298
- str_to_Uint8Array: function (str) {
299
- return stream.transform(str, str => {
300
- if (!util.isString(str)) {
301
- throw new Error('str_to_Uint8Array: Data must be in the form of a string');
302
- }
303
-
304
- const result = new Uint8Array(str.length);
305
- for (let i = 0; i < str.length; i++) {
306
- result[i] = str.charCodeAt(i);
307
- }
308
- return result;
309
- });
310
- },
311
-
312
- /**
313
- * Convert an array of 8-bit integers to a string
314
- * @param {Uint8Array} bytes An array of 8-bit integers to convert
315
- * @returns {String} String representation of the array
316
- */
317
- Uint8Array_to_str: function (bytes) {
318
- bytes = new Uint8Array(bytes);
319
- const result = [];
320
- const bs = 1 << 14;
321
- const j = bytes.length;
322
-
323
- for (let i = 0; i < j; i += bs) {
324
- result.push(String.fromCharCode.apply(String, bytes.subarray(i, i + bs < j ? i + bs : j)));
325
- }
326
- return result.join('');
327
- },
328
-
329
- /**
330
- * Convert a native javascript string to a Uint8Array of utf8 bytes
331
- * @param {String|ReadableStream} str The string to convert
332
- * @returns {Uint8Array|ReadableStream} A valid squence of utf8 bytes
333
- */
334
- encode_utf8: function (str) {
335
- const encoder = new TextEncoder('utf-8');
336
- // eslint-disable-next-line no-inner-declarations
337
- function process(value, lastChunk = false) {
338
- return encoder.encode(value, { stream: !lastChunk });
339
- }
340
- return stream.transform(str, process, () => process('', true));
341
- },
342
-
343
- /**
344
- * Convert a Uint8Array of utf8 bytes to a native javascript string
345
- * @param {Uint8Array|ReadableStream} utf8 A valid squence of utf8 bytes
346
- * @returns {String|ReadableStream} A native javascript string
347
- */
348
- decode_utf8: function (utf8) {
349
- const decoder = new TextDecoder('utf-8');
350
- // eslint-disable-next-line no-inner-declarations
351
- function process(value, lastChunk = false) {
352
- return decoder.decode(value, { stream: !lastChunk });
353
- }
354
- return stream.transform(utf8, process, () => process(new Uint8Array(), true));
355
- },
356
-
357
- /**
358
- * Concat a list of Uint8Arrays, Strings or Streams
359
- * The caller must not mix Uint8Arrays with Strings, but may mix Streams with non-Streams.
360
- * @param {Array<Uint8Array|String|ReadableStream>} Array of Uint8Arrays/Strings/Streams to concatenate
361
- * @returns {Uint8Array|String|ReadableStream} Concatenated array
362
- */
363
- concat: stream.concat,
364
-
365
- /**
366
- * Concat Uint8Arrays
367
- * @param {Array<Uint8Array>} Array of Uint8Arrays to concatenate
368
- * @returns {Uint8Array} Concatenated array
369
- */
370
- concatUint8Array: stream.concatUint8Array,
371
-
372
- /**
373
- * Check Uint8Array equality
374
- * @param {Uint8Array} first array
375
- * @param {Uint8Array} second array
376
- * @returns {Boolean} equality
377
- */
378
- equalsUint8Array: function (array1, array2) {
379
- if (!util.isUint8Array(array1) || !util.isUint8Array(array2)) {
380
- throw new Error('Data must be in the form of a Uint8Array');
381
- }
382
-
383
- if (array1.length !== array2.length) {
384
- return false;
385
- }
386
-
387
- for (let i = 0; i < array1.length; i++) {
388
- if (array1[i] !== array2[i]) {
389
- return false;
390
- }
391
- }
392
- return true;
393
- },
394
-
395
- /**
396
- * Calculates a 16bit sum of a Uint8Array by adding each character
397
- * codes modulus 65535
398
- * @param {Uint8Array} Uint8Array to create a sum of
399
- * @returns {Uint8Array} 2 bytes containing the sum of all charcodes % 65535
400
- */
401
- write_checksum: function (text) {
402
- let s = 0;
403
- for (let i = 0; i < text.length; i++) {
404
- s = (s + text[i]) & 0xFFFF;
405
- }
406
- return util.writeNumber(s, 2);
407
- },
408
-
409
- /**
410
- * Helper function to print a debug message. Debug
411
- * messages are only printed if
412
- * @link module:config/config.debug is set to true.
413
- * @param {String} str String of the debug message
414
- */
415
- print_debug: function (str) {
416
- if (config.debug) {
417
- console.log(str);
418
- }
419
- },
420
-
421
- /**
422
- * Helper function to print a debug message. Debug
423
- * messages are only printed if
424
- * @link module:config/config.debug is set to true.
425
- * Different than print_debug because will call Uint8Array_to_hex iff necessary.
426
- * @param {String} str String of the debug message
427
- */
428
- print_debug_hexarray_dump: function (str, arrToHex) {
429
- if (config.debug) {
430
- str += ': ' + util.Uint8Array_to_hex(arrToHex);
431
- console.log(str);
432
- }
433
- },
434
-
435
- /**
436
- * Helper function to print a debug message. Debug
437
- * messages are only printed if
438
- * @link module:config/config.debug is set to true.
439
- * Different than print_debug because will call str_to_hex iff necessary.
440
- * @param {String} str String of the debug message
441
- */
442
- print_debug_hexstr_dump: function (str, strToHex) {
443
- if (config.debug) {
444
- str += util.str_to_hex(strToHex);
445
- console.log(str);
446
- }
447
- },
448
-
449
- /**
450
- * Helper function to print a debug error. Debug
451
- * messages are only printed if
452
- * @link module:config/config.debug is set to true.
453
- * @param {String} str String of the debug message
454
- */
455
- print_debug_error: function (error) {
456
- if (config.debug) {
457
- console.error(error);
458
- }
459
- },
460
-
461
- /**
462
- * Read a stream to the end and print it to the console when it's closed.
463
- * @param {String} str String of the debug message
464
- * @param {ReadableStream|Uint8array|String} input Stream to print
465
- * @param {Function} concat Function to concatenate chunks of the stream (defaults to util.concat).
466
- */
467
- print_entire_stream: function (str, input, concat) {
468
- stream.readToEnd(stream.clone(input), concat).then(result => {
469
- console.log(str + ': ', result);
470
- });
471
- },
472
-
473
- // returns bit length of the integer x
474
- nbits: function (x) {
475
- let r = 1;
476
- let t = x >>> 16;
477
- if (t !== 0) {
478
- x = t;
479
- r += 16;
480
- }
481
- t = x >> 8;
482
- if (t !== 0) {
483
- x = t;
484
- r += 8;
485
- }
486
- t = x >> 4;
487
- if (t !== 0) {
488
- x = t;
489
- r += 4;
490
- }
491
- t = x >> 2;
492
- if (t !== 0) {
493
- x = t;
494
- r += 2;
495
- }
496
- t = x >> 1;
497
- if (t !== 0) {
498
- x = t;
499
- r += 1;
500
- }
501
- return r;
502
- },
503
-
504
- /**
505
- * If S[1] == 0, then double(S) == (S[2..128] || 0);
506
- * otherwise, double(S) == (S[2..128] || 0) xor
507
- * (zeros(120) || 10000111).
508
- *
509
- * Both OCB and EAX (through CMAC) require this function to be constant-time.
510
- *
511
- * @param {Uint8Array} data
512
- */
513
- double: function(data) {
514
- const double_var = new Uint8Array(data.length);
515
- const last = data.length - 1;
516
- for (let i = 0; i < last; i++) {
517
- double_var[i] = (data[i] << 1) ^ (data[i + 1] >> 7);
518
- }
519
- double_var[last] = (data[last] << 1) ^ ((data[0] >> 7) * 0x87);
520
- return double_var;
521
- },
522
-
523
- /**
524
- * Shift a Uint8Array to the right by n bits
525
- * @param {Uint8Array} array The array to shift
526
- * @param {Integer} bits Amount of bits to shift (MUST be smaller
527
- * than 8)
528
- * @returns {String} Resulting array.
529
- */
530
- shiftRight: function (array, bits) {
531
- if (bits) {
532
- for (let i = array.length - 1; i >= 0; i--) {
533
- array[i] >>= bits;
534
- if (i > 0) {
535
- array[i] |= (array[i - 1] << (8 - bits));
536
- }
537
- }
538
- }
539
- return array;
540
- },
541
-
542
- /**
543
- * Get native Web Cryptography api, only the current version of the spec.
544
- * The default configuration is to use the api when available. But it can
545
- * be deactivated with config.use_native
546
- * @returns {Object} The SubtleCrypto api or 'undefined'
547
- */
548
- getWebCrypto: function() {
549
- if (!config.use_native) {
550
- return;
551
- }
552
-
553
- return typeof global !== 'undefined' && global.crypto && global.crypto.subtle;
554
- },
555
-
556
- /**
557
- * Get native Web Cryptography api for all browsers, including legacy
558
- * implementations of the spec e.g IE11 and Safari 8/9. The default
559
- * configuration is to use the api when available. But it can be deactivated
560
- * with config.use_native
561
- * @returns {Object} The SubtleCrypto api or 'undefined'
562
- */
563
- getWebCryptoAll: function() {
564
- if (!config.use_native) {
565
- return;
566
- }
567
-
568
- if (typeof global !== 'undefined') {
569
- if (global.crypto) {
570
- return global.crypto.subtle || global.crypto.webkitSubtle;
571
- }
572
- if (global.msCrypto) {
573
- return global.msCrypto.subtle;
574
- }
575
- }
576
- },
577
-
578
- /**
579
- * Detect Node.js runtime.
580
- */
581
- detectNode: function() {
582
- return typeof global.process === 'object' &&
583
- typeof global.process.versions === 'object';
584
- },
585
-
586
- /**
587
- * Get native Node.js module
588
- * @param {String} The module to require
589
- * @returns {Object} The required module or 'undefined'
590
- */
591
- nodeRequire: function(module) {
592
- if (!util.detectNode()) {
593
- return;
594
- }
595
-
596
- // Requiring the module dynamically allows us to access the native node module.
597
- // otherwise, it gets replaced with the browserified version
598
- // eslint-disable-next-line import/no-dynamic-require
599
- return require(module);
600
- },
601
-
602
- /**
603
- * Get native Node.js crypto api. The default configuration is to use
604
- * the api when available. But it can also be deactivated with config.use_native
605
- * @returns {Object} The crypto module or 'undefined'
606
- */
607
- getNodeCrypto: function() {
608
- if (!config.use_native) {
609
- return;
610
- }
611
-
612
- return util.nodeRequire('crypto');
613
- },
614
-
615
- getNodeZlib: function() {
616
- if (!config.use_native) {
617
- return;
618
- }
619
-
620
- return util.nodeRequire('zlib');
621
- },
622
-
623
- /**
624
- * Get native Node.js Buffer constructor. This should be used since
625
- * Buffer is not available under browserify.
626
- * @returns {Function} The Buffer constructor or 'undefined'
627
- */
628
- getNodeBuffer: function() {
629
- return (util.nodeRequire('buffer') || {}).Buffer;
630
- },
631
-
632
- getNodeStream: function() {
633
- return (util.nodeRequire('stream') || {}).Readable;
634
- },
635
-
636
- getHardwareConcurrency: function() {
637
- if (util.detectNode()) {
638
- const os = util.nodeRequire('os');
639
- return os.cpus().length;
640
- }
641
-
642
- return navigator.hardwareConcurrency || 1;
643
- },
644
-
645
- isEmailAddress: function(data) {
646
- if (!util.isString(data)) {
647
- return false;
648
- }
649
- const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+([a-zA-Z]{2,}|xn--[a-zA-Z\-0-9]+)))$/;
650
- return re.test(data);
651
- },
652
-
653
- /**
654
- * Format user id for internal use.
655
- */
656
- formatUserId: function(id) {
657
- // name, email address and comment can be empty but must be of the correct type
658
- if ((id.name && !util.isString(id.name)) ||
659
- (id.email && !util.isEmailAddress(id.email)) ||
660
- (id.comment && !util.isString(id.comment))) {
661
- throw new Error('Invalid user id format');
662
- }
663
- const components = [];
664
- if (id.name) {
665
- components.push(id.name);
666
- }
667
- if (id.comment) {
668
- components.push(`(${id.comment})`);
669
- }
670
- if (id.email) {
671
- components.push(`<${id.email}>`);
672
- }
673
- return components.join(' ');
674
- },
675
-
676
- /**
677
- * Parse user id.
678
- */
679
- parseUserId: function(userid) {
680
- if (userid.length > config.max_userid_length) {
681
- throw new Error('User id string is too long');
682
- }
683
- try {
684
- const { name, address: email, comments } = emailAddresses.parseOneAddress({ input: userid, atInDisplayName: true });
685
- return { name, email, comment: comments.replace(/^\(|\)$/g, '') };
686
- } catch (e) {
687
- throw new Error('Invalid user id format');
688
- }
689
- },
690
-
691
- /**
692
- * Normalize line endings to <CR><LF>
693
- * Support any encoding where CR=0x0D, LF=0x0A
694
- */
695
- canonicalizeEOL: function(data) {
696
- const CR = 13;
697
- const LF = 10;
698
- let carryOverCR = false;
699
-
700
- return stream.transform(data, bytes => {
701
- if (carryOverCR) {
702
- bytes = util.concatUint8Array([new Uint8Array([CR]), bytes]);
703
- }
704
-
705
- if (bytes[bytes.length - 1] === CR) {
706
- carryOverCR = true;
707
- bytes = bytes.subarray(0, -1);
708
- } else {
709
- carryOverCR = false;
710
- }
711
-
712
- let index;
713
- const indices = [];
714
- for (let i = 0; ; i = index) {
715
- index = bytes.indexOf(LF, i) + 1;
716
- if (index) {
717
- if (bytes[index - 2] !== CR) indices.push(index);
718
- } else {
719
- break;
720
- }
721
- }
722
- if (!indices.length) {
723
- return bytes;
724
- }
725
-
726
- const normalized = new Uint8Array(bytes.length + indices.length);
727
- let j = 0;
728
- for (let i = 0; i < indices.length; i++) {
729
- const sub = bytes.subarray(indices[i - 1] || 0, indices[i]);
730
- normalized.set(sub, j);
731
- j += sub.length;
732
- normalized[j - 1] = CR;
733
- normalized[j] = LF;
734
- j++;
735
- }
736
- normalized.set(bytes.subarray(indices[indices.length - 1] || 0), j);
737
- return normalized;
738
- }, () => (carryOverCR ? new Uint8Array([CR]) : undefined));
739
- },
740
-
741
- /**
742
- * Convert line endings from canonicalized <CR><LF> to native <LF>
743
- * Support any encoding where CR=0x0D, LF=0x0A
744
- */
745
- nativeEOL: function(data) {
746
- const CR = 13;
747
- const LF = 10;
748
- let carryOverCR = false;
749
-
750
- return stream.transform(data, bytes => {
751
- if (carryOverCR && bytes[0] !== LF) {
752
- bytes = util.concatUint8Array([new Uint8Array([CR]), bytes]);
753
- } else {
754
- bytes = new Uint8Array(bytes); // Don't mutate passed bytes
755
- }
756
-
757
- if (bytes[bytes.length - 1] === CR) {
758
- carryOverCR = true;
759
- bytes = bytes.subarray(0, -1);
760
- } else {
761
- carryOverCR = false;
762
- }
763
-
764
- let index;
765
- let j = 0;
766
- for (let i = 0; i !== bytes.length; i = index) {
767
- index = bytes.indexOf(CR, i) + 1;
768
- if (!index) index = bytes.length;
769
- const last = index - (bytes[index] === LF ? 1 : 0);
770
- if (i) bytes.copyWithin(j, i, last);
771
- j += last - i;
772
- }
773
- return bytes.subarray(0, j);
774
- }, () => (carryOverCR ? new Uint8Array([CR]) : undefined));
775
- },
776
-
777
- /**
778
- * Remove trailing spaces and tabs from each line
779
- */
780
- removeTrailingSpaces: function(text) {
781
- return text.split('\n').map(line => {
782
- let i = line.length - 1;
783
- for (; i >= 0 && (line[i] === ' ' || line[i] === '\t'); i--);
784
- return line.substr(0, i + 1);
785
- }).join('\n');
786
- },
787
-
788
- /**
789
- * Encode input buffer using Z-Base32 encoding.
790
- * See: https://tools.ietf.org/html/rfc6189#section-5.1.6
791
- *
792
- * @param {Uint8Array} data The binary data to encode
793
- * @returns {String} Binary data encoded using Z-Base32
794
- */
795
- encodeZBase32: function(data) {
796
- if (data.length === 0) {
797
- return "";
798
- }
799
- const ALPHABET = "ybndrfg8ejkmcpqxot1uwisza345h769";
800
- const SHIFT = 5;
801
- const MASK = 31;
802
- let buffer = data[0];
803
- let index = 1;
804
- let bitsLeft = 8;
805
- let result = '';
806
- while (bitsLeft > 0 || index < data.length) {
807
- if (bitsLeft < SHIFT) {
808
- if (index < data.length) {
809
- buffer <<= 8;
810
- buffer |= data[index++] & 0xff;
811
- bitsLeft += 8;
812
- } else {
813
- const pad = SHIFT - bitsLeft;
814
- buffer <<= pad;
815
- bitsLeft += pad;
816
- }
817
- }
818
- bitsLeft -= SHIFT;
819
- result += ALPHABET[MASK & (buffer >> bitsLeft)];
820
- }
821
- return result;
822
- },
823
-
824
- wrapError: function(message, error) {
825
- if (!error) {
826
- return new Error(message);
827
- }
828
-
829
- // update error message
830
- try {
831
- error.message = message + ': ' + error.message;
832
- } catch (e) {}
833
-
834
- return error;
835
- }
836
- };