@protontech/openpgp 4.10.8 → 5.3.1

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 +31375 -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 +43943 -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 +43880 -0
  26. package/dist/openpgp.js +41077 -41609
  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 +43868 -0
  32. package/lightweight/package.json +5 -0
  33. package/openpgp.d.ts +889 -0
  34. package/package.json +63 -57
  35. package/dist/compat/openpgp.js +0 -61152
  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 -40071
  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 -232
  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 -378
  88. package/src/key/index.js +0 -32
  89. package/src/key/key.js +0 -888
  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 -161
  109. package/src/packet/public_subkey.js +0 -44
  110. package/src/packet/secret_key.js +0 -468
  111. package/src/packet/secret_subkey.js +0 -41
  112. package/src/packet/signature.js +0 -786
  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 -84
  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 -3491
  163. package/test/general/keyring.js +0 -336
  164. package/test/general/oid.js +0 -39
  165. package/test/general/openpgp.js +0 -2577
  166. package/test/general/packet.js +0 -950
  167. package/test/general/signature.js +0 -1715
  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
@@ -1,433 +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
- /**
19
- * @requires web-stream-tools
20
- * @requires encoding/base64
21
- * @requires enums
22
- * @requires config
23
- * @requires util
24
- * @module encoding/armor
25
- */
26
-
27
- import stream from 'web-stream-tools';
28
- import base64 from './base64.js';
29
- import enums from '../enums.js';
30
- import config from '../config';
31
- import util from '../util';
32
-
33
- /**
34
- * Finds out which Ascii Armoring type is used. Throws error if unknown type.
35
- * @private
36
- * @param {String} text [String] ascii armored text
37
- * @returns {Integer} 0 = MESSAGE PART n of m
38
- * 1 = MESSAGE PART n
39
- * 2 = SIGNED MESSAGE
40
- * 3 = PGP MESSAGE
41
- * 4 = PUBLIC KEY BLOCK
42
- * 5 = PRIVATE KEY BLOCK
43
- * 6 = SIGNATURE
44
- */
45
- function getType(text) {
46
- const reHeader = /^-----BEGIN PGP (MESSAGE, PART \d+\/\d+|MESSAGE, PART \d+|SIGNED MESSAGE|MESSAGE|PUBLIC KEY BLOCK|PRIVATE KEY BLOCK|SIGNATURE)-----$/m;
47
-
48
- const header = text.match(reHeader);
49
-
50
- if (!header) {
51
- throw new Error('Unknown ASCII armor type');
52
- }
53
-
54
- // BEGIN PGP MESSAGE, PART X/Y
55
- // Used for multi-part messages, where the armor is split amongst Y
56
- // parts, and this is the Xth part out of Y.
57
- if (/MESSAGE, PART \d+\/\d+/.test(header[1])) {
58
- return enums.armor.multipart_section;
59
- } else
60
- // BEGIN PGP MESSAGE, PART X
61
- // Used for multi-part messages, where this is the Xth part of an
62
- // unspecified number of parts. Requires the MESSAGE-ID Armor
63
- // Header to be used.
64
- if (/MESSAGE, PART \d+/.test(header[1])) {
65
- return enums.armor.multipart_last;
66
- } else
67
- // BEGIN PGP SIGNED MESSAGE
68
- if (/SIGNED MESSAGE/.test(header[1])) {
69
- return enums.armor.signed;
70
- } else
71
- // BEGIN PGP MESSAGE
72
- // Used for signed, encrypted, or compressed files.
73
- if (/MESSAGE/.test(header[1])) {
74
- return enums.armor.message;
75
- } else
76
- // BEGIN PGP PUBLIC KEY BLOCK
77
- // Used for armoring public keys.
78
- if (/PUBLIC KEY BLOCK/.test(header[1])) {
79
- return enums.armor.public_key;
80
- } else
81
- // BEGIN PGP PRIVATE KEY BLOCK
82
- // Used for armoring private keys.
83
- if (/PRIVATE KEY BLOCK/.test(header[1])) {
84
- return enums.armor.private_key;
85
- } else
86
- // BEGIN PGP SIGNATURE
87
- // Used for detached signatures, OpenPGP/MIME signatures, and
88
- // cleartext signatures. Note that PGP 2.x uses BEGIN PGP MESSAGE
89
- // for detached signatures.
90
- if (/SIGNATURE/.test(header[1])) {
91
- return enums.armor.signature;
92
- }
93
- }
94
-
95
- /**
96
- * Add additional information to the armor version of an OpenPGP binary
97
- * packet block.
98
- * @author Alex
99
- * @version 2011-12-16
100
- * @param {String} customComment (optional) additional comment to add to the armored string
101
- * @returns {String} The header information
102
- */
103
- function addheader(customComment) {
104
- let result = "";
105
- if (config.show_version) {
106
- result += "Version: " + config.versionstring + '\r\n';
107
- }
108
- if (config.show_comment) {
109
- result += "Comment: " + config.commentstring + '\r\n';
110
- }
111
- if (customComment) {
112
- result += "Comment: " + customComment + '\r\n';
113
- }
114
- result += '\r\n';
115
- return result;
116
- }
117
-
118
-
119
- /**
120
- * Calculates a checksum over the given data and returns it base64 encoded
121
- * @param {String | ReadableStream<String>} data Data to create a CRC-24 checksum for
122
- * @returns {String | ReadableStream<String>} Base64 encoded checksum
123
- */
124
- function getCheckSum(data) {
125
- const crc = createcrc24(data);
126
- return base64.encode(crc);
127
- }
128
-
129
- // https://create.stephan-brumme.com/crc32/#slicing-by-8-overview
130
-
131
- const crc_table = [
132
- new Array(0xFF),
133
- new Array(0xFF),
134
- new Array(0xFF),
135
- new Array(0xFF)
136
- ];
137
-
138
- for (let i = 0; i <= 0xFF; i++) {
139
- let crc = i << 16;
140
- for (let j = 0; j < 8; j++) {
141
- crc = (crc << 1) ^ ((crc & 0x800000) !== 0 ? 0x864CFB : 0);
142
- }
143
- crc_table[0][i] =
144
- ((crc & 0xFF0000) >> 16) |
145
- (crc & 0x00FF00) |
146
- ((crc & 0x0000FF) << 16);
147
- }
148
- for (let i = 0; i <= 0xFF; i++) {
149
- crc_table[1][i] = (crc_table[0][i] >> 8) ^ crc_table[0][crc_table[0][i] & 0xFF];
150
- }
151
- for (let i = 0; i <= 0xFF; i++) {
152
- crc_table[2][i] = (crc_table[1][i] >> 8) ^ crc_table[0][crc_table[1][i] & 0xFF];
153
- }
154
- for (let i = 0; i <= 0xFF; i++) {
155
- crc_table[3][i] = (crc_table[2][i] >> 8) ^ crc_table[0][crc_table[2][i] & 0xFF];
156
- }
157
-
158
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView#Endianness
159
- const isLittleEndian = (function() {
160
- const buffer = new ArrayBuffer(2);
161
- new DataView(buffer).setInt16(0, 0xFF, true /* littleEndian */);
162
- // Int16Array uses the platform's endianness.
163
- return new Int16Array(buffer)[0] === 0xFF;
164
- }());
165
-
166
- /**
167
- * Internal function to calculate a CRC-24 checksum over a given string (data)
168
- * @param {String | ReadableStream<String>} data Data to create a CRC-24 checksum for
169
- * @returns {Uint8Array | ReadableStream<Uint8Array>} The CRC-24 checksum
170
- */
171
- function createcrc24(input) {
172
- let crc = 0xCE04B7;
173
- return stream.transform(input, value => {
174
- const len32 = isLittleEndian ? Math.floor(value.length / 4) : 0;
175
- const arr32 = new Uint32Array(value.buffer, value.byteOffset, len32);
176
- for (let i = 0; i < len32; i++) {
177
- crc ^= arr32[i];
178
- crc =
179
- crc_table[0][(crc >> 24) & 0xFF] ^
180
- crc_table[1][(crc >> 16) & 0xFF] ^
181
- crc_table[2][(crc >> 8) & 0xFF] ^
182
- crc_table[3][(crc >> 0) & 0xFF];
183
- }
184
- for (let i = len32 * 4; i < value.length; i++) {
185
- crc = (crc >> 8) ^ crc_table[0][(crc & 0xFF) ^ value[i]];
186
- }
187
- }, () => new Uint8Array([crc, crc >> 8, crc >> 16]));
188
- }
189
-
190
- /**
191
- * Verify armored headers. RFC4880, section 6.3: "OpenPGP should consider improperly formatted
192
- * Armor Headers to be corruption of the ASCII Armor."
193
- * @private
194
- * @param {Array<String>} headers Armor headers
195
- */
196
- function verifyHeaders(headers) {
197
- for (let i = 0; i < headers.length; i++) {
198
- if (!/^([^\s:]|[^\s:][^:]*[^\s:]): .+$/.test(headers[i])) {
199
- throw new Error('Improperly formatted armor header: ' + headers[i]);
200
- }
201
- if (!/^(Version|Comment|MessageID|Hash|Charset): .+$/.test(headers[i])) {
202
- util.print_debug_error(new Error('Unknown header: ' + headers[i]));
203
- }
204
- }
205
- }
206
-
207
- /**
208
- * Splits a message into two parts, the body and the checksum. This is an internal function
209
- * @param {String} text OpenPGP armored message part
210
- * @returns {Object} An object with attribute "body" containing the body
211
- * and an attribute "checksum" containing the checksum.
212
- */
213
- function splitChecksum(text) {
214
- let body = text;
215
- let checksum = "";
216
-
217
- const lastEquals = text.lastIndexOf("=");
218
-
219
- if (lastEquals >= 0 && lastEquals !== text.length - 1) { // '=' as the last char means no checksum
220
- body = text.slice(0, lastEquals);
221
- checksum = text.slice(lastEquals + 1).substr(0, 4);
222
- }
223
-
224
- return { body: body, checksum: checksum };
225
- }
226
-
227
- /**
228
- * DeArmor an OpenPGP armored message; verify the checksum and return
229
- * the encoded bytes
230
- * @param {String} text OpenPGP armored message
231
- * @returns {Promise<Object>} An object with attribute "text" containing the message text,
232
- * an attribute "data" containing a stream of bytes and "type" for the ASCII armor type
233
- * @async
234
- * @static
235
- */
236
- function dearmor(input) {
237
- return new Promise(async (resolve, reject) => {
238
- try {
239
- const reSplit = /^-----[^-]+-----$/m;
240
- const reEmptyLine = /^[ \f\r\t\u00a0\u2000-\u200a\u202f\u205f\u3000]*$/;
241
-
242
- let type;
243
- const headers = [];
244
- let lastHeaders = headers;
245
- let headersDone;
246
- let text = [];
247
- let textDone;
248
- let checksum;
249
- let data = base64.decode(stream.transformPair(input, async (readable, writable) => {
250
- const reader = stream.getReader(readable);
251
- try {
252
- while (true) {
253
- let line = await reader.readLine();
254
- if (line === undefined) {
255
- throw new Error('Misformed armored text');
256
- }
257
- // remove trailing whitespace at end of lines
258
- line = util.removeTrailingSpaces(line.replace(/[\r\n]/g, ''));
259
- if (!type) {
260
- if (reSplit.test(line)) {
261
- type = getType(line);
262
- }
263
- } else if (!headersDone) {
264
- if (reSplit.test(line)) {
265
- reject(new Error('Mandatory blank line missing between armor headers and armor data'));
266
- }
267
- if (!reEmptyLine.test(line)) {
268
- lastHeaders.push(line);
269
- } else {
270
- verifyHeaders(lastHeaders);
271
- headersDone = true;
272
- if (textDone || type !== 2) {
273
- resolve({ text, data, headers, type });
274
- break;
275
- }
276
- }
277
- } else if (!textDone && type === 2) {
278
- if (!reSplit.test(line)) {
279
- // Reverse dash-escaping for msg
280
- text.push(line.replace(/^- /, ''));
281
- } else {
282
- text = text.join('\r\n');
283
- textDone = true;
284
- verifyHeaders(lastHeaders);
285
- lastHeaders = [];
286
- headersDone = false;
287
- }
288
- }
289
- }
290
- } catch (e) {
291
- reject(e);
292
- return;
293
- }
294
- const writer = stream.getWriter(writable);
295
- try {
296
- while (true) {
297
- await writer.ready;
298
- const { done, value } = await reader.read();
299
- if (done) {
300
- throw new Error('Misformed armored text');
301
- }
302
- const line = value + '';
303
- if (line.indexOf('=') === -1 && line.indexOf('-') === -1) {
304
- await writer.write(line);
305
- } else {
306
- let remainder = await reader.readToEnd();
307
- if (!remainder.length) remainder = '';
308
- remainder = line + remainder;
309
- remainder = util.removeTrailingSpaces(remainder.replace(/\r/g, ''));
310
- const parts = remainder.split(reSplit);
311
- if (parts.length === 1) {
312
- throw new Error('Misformed armored text');
313
- }
314
- const split = splitChecksum(parts[0].slice(0, -1));
315
- checksum = split.checksum;
316
- await writer.write(split.body);
317
- break;
318
- }
319
- }
320
- await writer.ready;
321
- await writer.close();
322
- } catch (e) {
323
- await writer.abort(e);
324
- }
325
- }));
326
- data = stream.transformPair(data, async (readable, writable) => {
327
- const checksumVerified = stream.readToEnd(getCheckSum(stream.passiveClone(readable)));
328
- checksumVerified.catch(() => {});
329
- await stream.pipe(readable, writable, {
330
- preventClose: true
331
- });
332
- const writer = stream.getWriter(writable);
333
- try {
334
- const checksumVerifiedString = (await checksumVerified).replace('\r\n', '');
335
- if (checksum !== checksumVerifiedString && (checksum || config.checksum_required)) {
336
- throw new Error("Ascii armor integrity check on message failed: '" + checksum + "' should be '" +
337
- checksumVerifiedString + "'");
338
- }
339
- await writer.ready;
340
- await writer.close();
341
- } catch (e) {
342
- await writer.abort(e);
343
- }
344
- });
345
- } catch (e) {
346
- reject(e);
347
- }
348
- });
349
- }
350
-
351
-
352
- /**
353
- * Armor an OpenPGP binary packet block
354
- * @param {Integer} messagetype type of the message
355
- * @param body
356
- * @param {Integer} partindex
357
- * @param {Integer} parttotal
358
- * @param {String} customComment (optional) additional comment to add to the armored string
359
- * @returns {String | ReadableStream<String>} Armored text
360
- * @static
361
- */
362
- function armor(messagetype, body, partindex, parttotal, customComment) {
363
- let text;
364
- let hash;
365
- if (messagetype === enums.armor.signed) {
366
- text = body.text;
367
- hash = body.hash;
368
- body = body.data;
369
- }
370
- const bodyClone = stream.passiveClone(body);
371
- const result = [];
372
- switch (messagetype) {
373
- case enums.armor.multipart_section:
374
- result.push("-----BEGIN PGP MESSAGE, PART " + partindex + "/" + parttotal + "-----\r\n");
375
- result.push(addheader(customComment));
376
- result.push(base64.encode(body));
377
- result.push("=", getCheckSum(bodyClone));
378
- result.push("-----END PGP MESSAGE, PART " + partindex + "/" + parttotal + "-----\r\n");
379
- break;
380
- case enums.armor.multipart_last:
381
- result.push("-----BEGIN PGP MESSAGE, PART " + partindex + "-----\r\n");
382
- result.push(addheader(customComment));
383
- result.push(base64.encode(body));
384
- result.push("=", getCheckSum(bodyClone));
385
- result.push("-----END PGP MESSAGE, PART " + partindex + "-----\r\n");
386
- break;
387
- case enums.armor.signed:
388
- result.push("\r\n-----BEGIN PGP SIGNED MESSAGE-----\r\n");
389
- result.push("Hash: " + hash + "\r\n\r\n");
390
- result.push(text.replace(/^-/mg, "- -"));
391
- result.push("\r\n-----BEGIN PGP SIGNATURE-----\r\n");
392
- result.push(addheader(customComment));
393
- result.push(base64.encode(body));
394
- result.push("=", getCheckSum(bodyClone));
395
- result.push("-----END PGP SIGNATURE-----\r\n");
396
- break;
397
- case enums.armor.message:
398
- result.push("-----BEGIN PGP MESSAGE-----\r\n");
399
- result.push(addheader(customComment));
400
- result.push(base64.encode(body));
401
- result.push("=", getCheckSum(bodyClone));
402
- result.push("-----END PGP MESSAGE-----\r\n");
403
- break;
404
- case enums.armor.public_key:
405
- result.push("-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n");
406
- result.push(addheader(customComment));
407
- result.push(base64.encode(body));
408
- result.push("=", getCheckSum(bodyClone));
409
- result.push("-----END PGP PUBLIC KEY BLOCK-----\r\n");
410
- break;
411
- case enums.armor.private_key:
412
- result.push("-----BEGIN PGP PRIVATE KEY BLOCK-----\r\n");
413
- result.push(addheader(customComment));
414
- result.push(base64.encode(body));
415
- result.push("=", getCheckSum(bodyClone));
416
- result.push("-----END PGP PRIVATE KEY BLOCK-----\r\n");
417
- break;
418
- case enums.armor.signature:
419
- result.push("-----BEGIN PGP SIGNATURE-----\r\n");
420
- result.push(addheader(customComment));
421
- result.push(base64.encode(body));
422
- result.push("=", getCheckSum(bodyClone));
423
- result.push("-----END PGP SIGNATURE-----\r\n");
424
- break;
425
- }
426
-
427
- return util.concat(result);
428
- }
429
-
430
- export default {
431
- encode: armor,
432
- decode: dearmor
433
- };
@@ -1,96 +0,0 @@
1
- /* OpenPGP radix-64/base64 string encoding/decoding
2
- * Copyright 2005 Herbert Hanewinkel, www.haneWIN.de
3
- * version 1.0, check www.haneWIN.de for the latest version
4
- *
5
- * This software is provided as-is, without express or implied warranty.
6
- * Permission to use, copy, modify, distribute or sell this software, with or
7
- * without fee, for any purpose and by any individual or organization, is hereby
8
- * granted, provided that the above copyright notice and this paragraph appear
9
- * in all copies. Distribution as a part of an application or binary must
10
- * include the above copyright notice in the documentation and/or other materials
11
- * provided with the application or distribution.
12
- */
13
-
14
- /**
15
- * @requires web-stream-tools
16
- * @requires util
17
- * @module encoding/base64
18
- */
19
-
20
- import stream from 'web-stream-tools';
21
- import util from '../util';
22
-
23
- const Buffer = util.getNodeBuffer();
24
-
25
- let encodeChunk;
26
- let decodeChunk;
27
- if (Buffer) {
28
- encodeChunk = buf => Buffer.from(buf).toString('base64');
29
- decodeChunk = str => {
30
- const b = Buffer.from(str, 'base64');
31
- return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
32
- };
33
- } else {
34
- encodeChunk = buf => btoa(util.Uint8Array_to_str(buf));
35
- decodeChunk = str => util.str_to_Uint8Array(atob(str));
36
- }
37
-
38
- /**
39
- * Convert binary array to radix-64
40
- * @param {Uint8Array | ReadableStream<Uint8Array>} data Uint8Array to convert
41
- * @returns {String | ReadableStream<String>} radix-64 version of input string
42
- * @static
43
- */
44
- function encode(data) {
45
- let buf = new Uint8Array();
46
- return stream.transform(data, value => {
47
- buf = util.concatUint8Array([buf, value]);
48
- const r = [];
49
- const bytesPerLine = 45; // 60 chars per line * (3 bytes / 4 chars of base64).
50
- const lines = Math.floor(buf.length / bytesPerLine);
51
- const bytes = lines * bytesPerLine;
52
- const encoded = encodeChunk(buf.subarray(0, bytes));
53
- for (let i = 0; i < lines; i++) {
54
- r.push(encoded.substr(i * 60, 60));
55
- r.push('\r\n');
56
- }
57
- buf = buf.subarray(bytes);
58
- return r.join('');
59
- }, () => (buf.length ? encodeChunk(buf) + '\r\n' : ''));
60
- }
61
-
62
- /**
63
- * Convert radix-64 to binary array
64
- * @param {String | ReadableStream<String>} data radix-64 string to convert
65
- * @returns {Uint8Array | ReadableStream<Uint8Array>} binary array version of input string
66
- * @static
67
- */
68
- function decode(data) {
69
- let buf = '';
70
- return stream.transform(data, value => {
71
- buf += value;
72
-
73
- // Count how many whitespace characters there are in buf
74
- let spaces = 0;
75
- const spacechars = [' ', '\t', '\r', '\n'];
76
- for (let i = 0; i < spacechars.length; i++) {
77
- const spacechar = spacechars[i];
78
- for (let pos = buf.indexOf(spacechar); pos !== -1; pos = buf.indexOf(spacechar, pos + 1)) {
79
- spaces++;
80
- }
81
- }
82
-
83
- // Backtrack until we have 4n non-whitespace characters
84
- // that we can safely base64-decode
85
- let length = buf.length;
86
- for (; length > 0 && (length - spaces) % 4 !== 0; length--) {
87
- if (spacechars.includes(buf[length])) spaces--;
88
- }
89
-
90
- const decoded = decodeChunk(buf.substr(0, length));
91
- buf = buf.substr(length);
92
- return decoded;
93
- }, () => decodeChunk(buf));
94
- }
95
-
96
- export default { encode, decode };