mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2025-03-30 15:08:32 +00:00
Internal: improve tree-shaking of web-stream-tools
Import single functions instead of entire lib.
This commit is contained in:
parent
a5d894f514
commit
8e5da78971
@ -23,7 +23,7 @@
|
||||
|
||||
import { cfb as nobleAesCfb, unsafe as nobleAesHelpers } from '@noble/ciphers/aes';
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { transform as streamTransform } from '@openpgp/web-stream-tools';
|
||||
import util from '../../util';
|
||||
import enums from '../../enums';
|
||||
import { getLegacyCipher, getCipherParams } from '../cipher';
|
||||
@ -99,7 +99,7 @@ export async function encrypt(algo, key, plaintext, iv, config) {
|
||||
}
|
||||
return ciphertext.subarray(0, j);
|
||||
};
|
||||
return stream.transform(plaintext, process, process);
|
||||
return streamTransform(plaintext, process, process);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -142,7 +142,7 @@ export async function decrypt(algo, key, ciphertext, iv) {
|
||||
}
|
||||
return plaintext.subarray(0, j);
|
||||
};
|
||||
return stream.transform(ciphertext, process, process);
|
||||
return streamTransform(ciphertext, process, process);
|
||||
}
|
||||
|
||||
class WebCryptoEncryptor {
|
||||
@ -346,10 +346,10 @@ class NobleStreamProcessor {
|
||||
async function aesEncrypt(algo, key, pt, iv) {
|
||||
if (webCrypto && await WebCryptoEncryptor.isSupported(algo)) { // Chromium does not implement AES with 192-bit keys
|
||||
const cfb = new WebCryptoEncryptor(algo, key, iv);
|
||||
return util.isStream(pt) ? stream.transform(pt, value => cfb.encryptChunk(value), () => cfb.finish()) : cfb.encrypt(pt);
|
||||
} else if (util.isStream(pt)) { // async callbacks are not accepted by stream.transform unless the input is a stream
|
||||
return util.isStream(pt) ? streamTransform(pt, value => cfb.encryptChunk(value), () => cfb.finish()) : cfb.encrypt(pt);
|
||||
} else if (util.isStream(pt)) { // async callbacks are not accepted by streamTransform unless the input is a stream
|
||||
const cfb = new NobleStreamProcessor(true, algo, key, iv);
|
||||
return stream.transform(pt, value => cfb.processChunk(value), () => cfb.finish());
|
||||
return streamTransform(pt, value => cfb.processChunk(value), () => cfb.finish());
|
||||
}
|
||||
return nobleAesCfb(key, iv).encrypt(pt);
|
||||
}
|
||||
@ -357,7 +357,7 @@ async function aesEncrypt(algo, key, pt, iv) {
|
||||
async function aesDecrypt(algo, key, ct, iv) {
|
||||
if (util.isStream(ct)) {
|
||||
const cfb = new NobleStreamProcessor(false, algo, key, iv);
|
||||
return stream.transform(ct, value => cfb.processChunk(value), () => cfb.finish());
|
||||
return streamTransform(ct, value => cfb.processChunk(value), () => cfb.finish());
|
||||
}
|
||||
return nobleAesCfb(key, iv).decrypt(ct);
|
||||
}
|
||||
@ -374,11 +374,11 @@ const getUint32Array = arr => new Uint32Array(arr.buffer, arr.byteOffset, Math.f
|
||||
function nodeEncrypt(algo, key, pt, iv) {
|
||||
const algoName = enums.read(enums.symmetric, algo);
|
||||
const cipherObj = new nodeCrypto.createCipheriv(nodeAlgos[algoName], key, iv);
|
||||
return stream.transform(pt, value => new Uint8Array(cipherObj.update(value)));
|
||||
return streamTransform(pt, value => new Uint8Array(cipherObj.update(value)));
|
||||
}
|
||||
|
||||
function nodeDecrypt(algo, key, ct, iv) {
|
||||
const algoName = enums.read(enums.symmetric, algo);
|
||||
const decipherObj = new nodeCrypto.createDecipheriv(nodeAlgos[algoName], key, iv);
|
||||
return stream.transform(ct, value => new Uint8Array(decipherObj.update(value)));
|
||||
return streamTransform(ct, value => new Uint8Array(decipherObj.update(value)));
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
* @module crypto/hash
|
||||
*/
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { transform as streamTransform, isArrayStream, readToEnd as streamReadToEnd } from '@openpgp/web-stream-tools';
|
||||
import util from '../../util';
|
||||
import enums from '../../enums';
|
||||
|
||||
@ -19,7 +19,7 @@ function nodeHash(type) {
|
||||
}
|
||||
return async function (data) {
|
||||
const shasum = nodeCrypto.createHash(type);
|
||||
return stream.transform(data, value => {
|
||||
return streamTransform(data, value => {
|
||||
shasum.update(value);
|
||||
}, () => new Uint8Array(shasum.digest()));
|
||||
};
|
||||
@ -34,14 +34,14 @@ function nobleHash(nobleHashName, webCryptoHashName) {
|
||||
};
|
||||
|
||||
return async function(data) {
|
||||
if (stream.isArrayStream(data)) {
|
||||
data = await stream.readToEnd(data);
|
||||
if (isArrayStream(data)) {
|
||||
data = await streamReadToEnd(data);
|
||||
}
|
||||
if (util.isStream(data)) {
|
||||
const hash = await getNobleHash();
|
||||
|
||||
const hashInstance = hash.create();
|
||||
return stream.transform(data, value => {
|
||||
return streamTransform(data, value => {
|
||||
hashInstance.update(value);
|
||||
}, () => hashInstance.digest());
|
||||
} else if (webCrypto && webCryptoHashName) {
|
||||
|
@ -15,7 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { transform as streamTransform, transformPair as streamTransformPair, getReader as streamGetReader, getWriter as streamGetWriter, isArrayStream, readToEnd as streamReadToEnd, passiveClone as streamPassiveClone } from '@openpgp/web-stream-tools';
|
||||
import * as base64 from './base64';
|
||||
import enums from '../enums';
|
||||
import util from '../util';
|
||||
@ -163,7 +163,7 @@ const isLittleEndian = (function() {
|
||||
*/
|
||||
function createcrc24(input) {
|
||||
let crc = 0xCE04B7;
|
||||
return stream.transform(input, value => {
|
||||
return streamTransform(input, value => {
|
||||
const len32 = isLittleEndian ? Math.floor(value.length / 4) : 0;
|
||||
const arr32 = new Uint32Array(value.buffer, value.byteOffset, len32);
|
||||
for (let i = 0; i < len32; i++) {
|
||||
@ -239,8 +239,8 @@ export function unarmor(input) {
|
||||
let headersDone;
|
||||
let text = [];
|
||||
let textDone;
|
||||
const data = base64.decode(stream.transformPair(input, async (readable, writable) => {
|
||||
const reader = stream.getReader(readable);
|
||||
const data = base64.decode(streamTransformPair(input, async (readable, writable) => {
|
||||
const reader = streamGetReader(readable);
|
||||
try {
|
||||
while (true) {
|
||||
let line = await reader.readLine();
|
||||
@ -284,7 +284,7 @@ export function unarmor(input) {
|
||||
reject(e);
|
||||
return;
|
||||
}
|
||||
const writer = stream.getWriter(writable);
|
||||
const writer = streamGetWriter(writable);
|
||||
try {
|
||||
while (true) {
|
||||
await writer.ready;
|
||||
@ -319,8 +319,8 @@ export function unarmor(input) {
|
||||
reject(e);
|
||||
}
|
||||
}).then(async result => {
|
||||
if (stream.isArrayStream(result.data)) {
|
||||
result.data = await stream.readToEnd(result.data);
|
||||
if (isArrayStream(result.data)) {
|
||||
result.data = await streamReadToEnd(result.data);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
@ -350,7 +350,7 @@ export function armor(messageType, body, partIndex, partTotal, customComment, em
|
||||
}
|
||||
// unless explicitly forbidden by the spec, we need to include the checksum to work around a GnuPG bug
|
||||
// where data fails to be decoded if the base64 ends with no padding chars (=) (see https://dev.gnupg.org/T7071)
|
||||
const maybeBodyClone = emitChecksum && stream.passiveClone(body);
|
||||
const maybeBodyClone = emitChecksum && streamPassiveClone(body);
|
||||
|
||||
const result = [];
|
||||
switch (messageType) {
|
||||
|
@ -15,7 +15,7 @@
|
||||
* @module encoding/base64
|
||||
*/
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { transform as streamTransform } from '@openpgp/web-stream-tools';
|
||||
import util from '../util';
|
||||
|
||||
const Buffer = util.getNodeBuffer();
|
||||
@ -41,7 +41,7 @@ if (Buffer) {
|
||||
*/
|
||||
export function encode(data) {
|
||||
let buf = new Uint8Array();
|
||||
return stream.transform(data, value => {
|
||||
return streamTransform(data, value => {
|
||||
buf = util.concatUint8Array([buf, value]);
|
||||
const r = [];
|
||||
const bytesPerLine = 45; // 60 chars per line * (3 bytes / 4 chars of base64).
|
||||
@ -65,7 +65,7 @@ export function encode(data) {
|
||||
*/
|
||||
export function decode(data) {
|
||||
let buf = '';
|
||||
return stream.transform(data, value => {
|
||||
return streamTransform(data, value => {
|
||||
buf += value;
|
||||
|
||||
// Count how many whitespace characters there are in buf
|
||||
|
@ -15,7 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { isArrayStream, cancel as streamCancel, readToEnd as streamReadToEnd, fromAsync as streamFromAsync, transformPair as streamTransformPair, getWriter as streamGetWriter, getReader as streamGetReader } from '@openpgp/web-stream-tools';
|
||||
import { armor, unarmor } from './encoding/armor';
|
||||
import { Argon2OutOfMemoryError } from './type/s2k';
|
||||
import defaultConfig from './config';
|
||||
@ -137,7 +137,7 @@ export class Message {
|
||||
}
|
||||
}));
|
||||
// We don't await stream.cancel here because it only returns when the other copy is canceled too.
|
||||
stream.cancel(symEncryptedPacket.encrypted); // Don't keep copy of encrypted data in memory.
|
||||
streamCancel(symEncryptedPacket.encrypted); // Don't keep copy of encrypted data in memory.
|
||||
symEncryptedPacket.encrypted = null;
|
||||
await decryptedPromise;
|
||||
|
||||
@ -282,7 +282,7 @@ export class Message {
|
||||
}
|
||||
}));
|
||||
}));
|
||||
stream.cancel(pkeskPacket.encrypted); // Don't keep copy of encrypted data in memory.
|
||||
streamCancel(pkeskPacket.encrypted); // Don't keep copy of encrypted data in memory.
|
||||
pkeskPacket.encrypted = null;
|
||||
}));
|
||||
} else {
|
||||
@ -588,24 +588,24 @@ export class Message {
|
||||
if (literalDataList.length !== 1) {
|
||||
throw new Error('Can only verify message with one literal data packet.');
|
||||
}
|
||||
if (stream.isArrayStream(msg.packets.stream)) {
|
||||
msg.packets.push(...await stream.readToEnd(msg.packets.stream, _ => _ || []));
|
||||
if (isArrayStream(msg.packets.stream)) {
|
||||
msg.packets.push(...await streamReadToEnd(msg.packets.stream, _ => _ || []));
|
||||
}
|
||||
const onePassSigList = msg.packets.filterByTag(enums.packet.onePassSignature).reverse();
|
||||
const signatureList = msg.packets.filterByTag(enums.packet.signature);
|
||||
if (onePassSigList.length && !signatureList.length && util.isStream(msg.packets.stream) && !stream.isArrayStream(msg.packets.stream)) {
|
||||
if (onePassSigList.length && !signatureList.length && util.isStream(msg.packets.stream) && !isArrayStream(msg.packets.stream)) {
|
||||
await Promise.all(onePassSigList.map(async onePassSig => {
|
||||
onePassSig.correspondingSig = new Promise((resolve, reject) => {
|
||||
onePassSig.correspondingSigResolve = resolve;
|
||||
onePassSig.correspondingSigReject = reject;
|
||||
});
|
||||
onePassSig.signatureData = stream.fromAsync(async () => (await onePassSig.correspondingSig).signatureData);
|
||||
onePassSig.hashed = stream.readToEnd(await onePassSig.hash(onePassSig.signatureType, literalDataList[0], undefined, false));
|
||||
onePassSig.signatureData = streamFromAsync(async () => (await onePassSig.correspondingSig).signatureData);
|
||||
onePassSig.hashed = streamReadToEnd(await onePassSig.hash(onePassSig.signatureType, literalDataList[0], undefined, false));
|
||||
onePassSig.hashed.catch(() => {});
|
||||
}));
|
||||
msg.packets.stream = stream.transformPair(msg.packets.stream, async (readable, writable) => {
|
||||
const reader = stream.getReader(readable);
|
||||
const writer = stream.getWriter(writable);
|
||||
msg.packets.stream = streamTransformPair(msg.packets.stream, async (readable, writable) => {
|
||||
const reader = streamGetReader(readable);
|
||||
const writer = streamGetWriter(writable);
|
||||
try {
|
||||
for (let i = 0; i < onePassSigList.length; i++) {
|
||||
const { value: signature } = await reader.read();
|
||||
|
@ -15,7 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { fromAsync as streamFromAsync, concat as streamConcat, transformPair as streamTransformPair, pipe as streamPipe, readToEnd as streamReadToEnd, getWriter as streamGetWriter } from '@openpgp/web-stream-tools';
|
||||
import { Message } from './message';
|
||||
import { CleartextMessage } from './cleartext';
|
||||
import { generate, reformat, getPreferredCompressionAlgo } from './key';
|
||||
@ -365,9 +365,9 @@ export async function decrypt({ message, decryptionKeys, passwords, sessionKeys,
|
||||
if (result.signatures.length === 0) {
|
||||
throw new Error('Message is not signed');
|
||||
}
|
||||
result.data = stream.concat([
|
||||
result.data = streamConcat([
|
||||
result.data,
|
||||
stream.fromAsync(async () => {
|
||||
streamFromAsync(async () => {
|
||||
await util.anyPromise(result.signatures.map(sig => sig.verified));
|
||||
return format === 'binary' ? new Uint8Array() : '';
|
||||
})
|
||||
@ -434,10 +434,10 @@ export async function sign({ message, signingKeys, recipientKeys = [], format =
|
||||
const armor = format === 'armored';
|
||||
signature = armor ? signature.armor(config) : signature.write();
|
||||
if (detached) {
|
||||
signature = stream.transformPair(message.packets.write(), async (readable, writable) => {
|
||||
signature = streamTransformPair(message.packets.write(), async (readable, writable) => {
|
||||
await Promise.all([
|
||||
stream.pipe(signature, writable),
|
||||
stream.readToEnd(readable).catch(() => {})
|
||||
streamPipe(signature, writable),
|
||||
streamReadToEnd(readable).catch(() => {})
|
||||
]);
|
||||
});
|
||||
}
|
||||
@ -497,9 +497,9 @@ export async function verify({ message, verificationKeys, expectSigned = false,
|
||||
if (result.signatures.length === 0) {
|
||||
throw new Error('Message is not signed');
|
||||
}
|
||||
result.data = stream.concat([
|
||||
result.data = streamConcat([
|
||||
result.data,
|
||||
stream.fromAsync(async () => {
|
||||
streamFromAsync(async () => {
|
||||
await util.anyPromise(result.signatures.map(sig => sig.verified));
|
||||
return format === 'binary' ? new Uint8Array() : '';
|
||||
})
|
||||
@ -683,7 +683,7 @@ function toArray(param) {
|
||||
async function convertStream(data) {
|
||||
const streamType = util.isStream(data);
|
||||
if (streamType === 'array') {
|
||||
return stream.readToEnd(data);
|
||||
return streamReadToEnd(data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
@ -697,14 +697,14 @@ async function convertStream(data) {
|
||||
* @private
|
||||
*/
|
||||
function linkStreams(result, message) {
|
||||
result.data = stream.transformPair(message.packets.stream, async (readable, writable) => {
|
||||
await stream.pipe(result.data, writable, {
|
||||
result.data = streamTransformPair(message.packets.stream, async (readable, writable) => {
|
||||
await streamPipe(result.data, writable, {
|
||||
preventClose: true
|
||||
});
|
||||
const writer = stream.getWriter(writable);
|
||||
const writer = streamGetWriter(writable);
|
||||
try {
|
||||
// Forward errors in the message stream to result.data.
|
||||
await stream.readToEnd(readable, _ => _);
|
||||
await streamReadToEnd(readable, _ => _);
|
||||
await writer.close();
|
||||
} catch (e) {
|
||||
await writer.abort(e);
|
||||
|
@ -15,7 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { clone as streamClone, parse as streamParse } from '@openpgp/web-stream-tools';
|
||||
import { cipherMode, getRandomBytes } from '../crypto';
|
||||
import enums from '../enums';
|
||||
import util from '../util';
|
||||
@ -69,7 +69,7 @@ class AEADEncryptedDataPacket {
|
||||
* @throws {Error} on parsing failure
|
||||
*/
|
||||
async read(bytes) {
|
||||
await stream.parse(bytes, async reader => {
|
||||
await streamParse(bytes, async reader => {
|
||||
const version = await reader.readByte();
|
||||
if (version !== VERSION) { // The only currently defined value is 1.
|
||||
throw new UnsupportedError(`Version ${version} of the AEAD-encrypted data packet is not supported.`);
|
||||
@ -102,7 +102,7 @@ class AEADEncryptedDataPacket {
|
||||
*/
|
||||
async decrypt(sessionKeyAlgorithm, key, config = defaultConfig) {
|
||||
this.packets = await PacketList.fromBinary(
|
||||
await runAEAD(this, 'decrypt', key, stream.clone(this.encrypted)),
|
||||
await runAEAD(this, 'decrypt', key, streamClone(this.encrypted)),
|
||||
allowedPackets,
|
||||
config
|
||||
);
|
||||
|
@ -16,7 +16,7 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import { Inflate, Deflate, Zlib, Unzlib } from 'fflate';
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { isArrayStream, fromAsync as streamFromAsync, parse as streamParse, readToEnd as streamReadToEnd } from '@openpgp/web-stream-tools';
|
||||
import enums from '../enums';
|
||||
import util from '../util';
|
||||
import defaultConfig from '../config';
|
||||
@ -74,7 +74,7 @@ class CompressedDataPacket {
|
||||
* @param {Object} [config] - Full configuration, defaults to openpgp.config
|
||||
*/
|
||||
async read(bytes, config = defaultConfig) {
|
||||
await stream.parse(bytes, async reader => {
|
||||
await streamParse(bytes, async reader => {
|
||||
|
||||
// One octet that gives the algorithm used to compress the packet.
|
||||
this.algorithm = await reader.readByte();
|
||||
@ -145,8 +145,8 @@ export default CompressedDataPacket;
|
||||
*/
|
||||
function zlib(compressionStreamInstantiator, ZlibStreamedConstructor) {
|
||||
return data => {
|
||||
if (!util.isStream(data) || stream.isArrayStream(data)) {
|
||||
return stream.fromAsync(() => stream.readToEnd(data).then(inputData => {
|
||||
if (!util.isStream(data) || isArrayStream(data)) {
|
||||
return streamFromAsync(() => streamReadToEnd(data).then(inputData => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const zlibStream = new ZlibStreamedConstructor();
|
||||
zlibStream.ondata = processedData => {
|
||||
@ -204,7 +204,7 @@ function zlib(compressionStreamInstantiator, ZlibStreamedConstructor) {
|
||||
function bzip2Decompress() {
|
||||
return async function(data) {
|
||||
const { decode: bunzipDecode } = await import('@openpgp/seek-bzip');
|
||||
return stream.fromAsync(async () => bunzipDecode(await stream.readToEnd(data)));
|
||||
return streamFromAsync(async () => bunzipDecode(await streamReadToEnd(data)));
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { isArrayStream, passiveClone as streamPassiveClone, parse as streamParse, readToEnd as streamReadToEnd } from '@openpgp/web-stream-tools';
|
||||
import enums from '../enums';
|
||||
import util from '../util';
|
||||
|
||||
@ -90,7 +90,7 @@ class LiteralDataPacket {
|
||||
this.data = util.canonicalizeEOL(util.encodeUTF8(this.text));
|
||||
}
|
||||
if (clone) {
|
||||
return stream.passiveClone(this.data);
|
||||
return streamPassiveClone(this.data);
|
||||
}
|
||||
return this.data;
|
||||
}
|
||||
@ -121,7 +121,7 @@ class LiteralDataPacket {
|
||||
* @async
|
||||
*/
|
||||
async read(bytes) {
|
||||
await stream.parse(bytes, async reader => {
|
||||
await streamParse(bytes, async reader => {
|
||||
// - A one-octet field that describes how the data is formatted.
|
||||
const format = await reader.readByte(); // enums.literal
|
||||
|
||||
@ -131,7 +131,7 @@ class LiteralDataPacket {
|
||||
this.date = util.readDate(await reader.readBytes(4));
|
||||
|
||||
let data = reader.remainder();
|
||||
if (stream.isArrayStream(data)) data = await stream.readToEnd(data);
|
||||
if (isArrayStream(data)) data = await streamReadToEnd(data);
|
||||
this.setBytes(data, format);
|
||||
});
|
||||
}
|
||||
|
@ -15,7 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { fromAsync as streamFromAsync } from '@openpgp/web-stream-tools';
|
||||
import SignaturePacket from './signature';
|
||||
import KeyID from '../type/keyid';
|
||||
import enums from '../enums';
|
||||
@ -169,7 +169,7 @@ class OnePassSignaturePacket {
|
||||
}
|
||||
|
||||
calculateTrailer(...args) {
|
||||
return stream.fromAsync(async () => SignaturePacket.prototype.calculateTrailer.apply(await this.correspondingSig, args));
|
||||
return streamFromAsync(async () => SignaturePacket.prototype.calculateTrailer.apply(await this.correspondingSig, args));
|
||||
}
|
||||
|
||||
async verify() {
|
||||
|
@ -20,7 +20,7 @@
|
||||
* @module packet/packet
|
||||
*/
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { ArrayStream, getWriter as streamGetWriter, getReader as streamGetReader } from '@openpgp/web-stream-tools';
|
||||
import enums from '../enums';
|
||||
import util from '../util';
|
||||
|
||||
@ -115,7 +115,7 @@ export function supportsStreaming(tag) {
|
||||
* @returns {Boolean} Returns false if the stream was empty and parsing is done, and true otherwise.
|
||||
*/
|
||||
export async function readPackets(input, callback) {
|
||||
const reader = stream.getReader(input);
|
||||
const reader = streamGetReader(input);
|
||||
let writer;
|
||||
let callbackReturned;
|
||||
try {
|
||||
@ -148,12 +148,12 @@ export async function readPackets(input, callback) {
|
||||
let packet = null;
|
||||
if (packetSupportsStreaming) {
|
||||
if (util.isStream(input) === 'array') {
|
||||
const arrayStream = new stream.ArrayStream();
|
||||
writer = stream.getWriter(arrayStream);
|
||||
const arrayStream = new ArrayStream();
|
||||
writer = streamGetWriter(arrayStream);
|
||||
packet = arrayStream;
|
||||
} else {
|
||||
const transform = new TransformStream();
|
||||
writer = stream.getWriter(transform.writable);
|
||||
writer = streamGetWriter(transform.writable);
|
||||
packet = transform.readable;
|
||||
}
|
||||
// eslint-disable-next-line callback-return
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { transformPair as streamTransformPair, transform as streamTransform, getWriter as streamGetWriter, getReader as streamGetReader, clone as streamClone } from '@openpgp/web-stream-tools';
|
||||
import {
|
||||
readPackets, supportsStreaming,
|
||||
writeTag, writeHeader,
|
||||
@ -68,8 +68,8 @@ class PacketList extends Array {
|
||||
if (config.additionalAllowedPackets.length) {
|
||||
allowedPackets = { ...allowedPackets, ...util.constructAllowedPackets(config.additionalAllowedPackets) };
|
||||
}
|
||||
this.stream = stream.transformPair(bytes, async (readable, writable) => {
|
||||
const writer = stream.getWriter(writable);
|
||||
this.stream = streamTransformPair(bytes, async (readable, writable) => {
|
||||
const writer = streamGetWriter(writable);
|
||||
try {
|
||||
while (true) {
|
||||
await writer.ready;
|
||||
@ -125,7 +125,7 @@ class PacketList extends Array {
|
||||
});
|
||||
|
||||
// Wait until first few packets have been read
|
||||
const reader = stream.getReader(this.stream);
|
||||
const reader = streamGetReader(this.stream);
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (!done) {
|
||||
@ -156,7 +156,7 @@ class PacketList extends Array {
|
||||
let bufferLength = 0;
|
||||
const minLength = 512;
|
||||
arr.push(writeTag(tag));
|
||||
arr.push(stream.transform(packetbytes, value => {
|
||||
arr.push(streamTransform(packetbytes, value => {
|
||||
buffer.push(value);
|
||||
bufferLength += value.length;
|
||||
if (bufferLength >= minLength) {
|
||||
@ -171,7 +171,7 @@ class PacketList extends Array {
|
||||
} else {
|
||||
if (util.isStream(packetbytes)) {
|
||||
let length = 0;
|
||||
arr.push(stream.transform(stream.clone(packetbytes), value => {
|
||||
arr.push(streamTransform(streamClone(packetbytes), value => {
|
||||
length += value.length;
|
||||
}, () => writeHeader(tag, length)));
|
||||
} else {
|
||||
|
@ -15,7 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { fromAsync as streamFromAsync, slice as streamSlice, readToEnd as streamReadToEnd, clone as streamClone, transform as streamTransform } from '@openpgp/web-stream-tools';
|
||||
import { readSimpleLength, UnsupportedError, writeSimpleLength } from './packet';
|
||||
import KeyID from '../type/keyid';
|
||||
import { signature, serializeParams, getRandomBytes, getHashByteLength, computeDigest } from '../crypto';
|
||||
@ -178,7 +178,7 @@ class SignaturePacket {
|
||||
*/
|
||||
writeParams() {
|
||||
if (this.params instanceof Promise) {
|
||||
return stream.fromAsync(
|
||||
return streamFromAsync(
|
||||
async () => serializeParams(this.publicKeyAlgorithm, await this.params)
|
||||
);
|
||||
}
|
||||
@ -255,9 +255,9 @@ class SignaturePacket {
|
||||
const toHash = this.toHash(this.signatureType, data, detached);
|
||||
const hash = await this.hash(this.signatureType, data, toHash, detached);
|
||||
|
||||
this.signedHashValue = stream.slice(stream.clone(hash), 0, 2);
|
||||
this.signedHashValue = streamSlice(streamClone(hash), 0, 2);
|
||||
const signed = async () => signature.sign(
|
||||
this.publicKeyAlgorithm, this.hashAlgorithm, key.publicParams, key.privateParams, toHash, await stream.readToEnd(hash)
|
||||
this.publicKeyAlgorithm, this.hashAlgorithm, key.publicParams, key.privateParams, toHash, await streamReadToEnd(hash)
|
||||
);
|
||||
if (util.isStream(hash)) {
|
||||
this.params = signed();
|
||||
@ -703,7 +703,7 @@ class SignaturePacket {
|
||||
|
||||
calculateTrailer(data, detached) {
|
||||
let length = 0;
|
||||
return stream.transform(stream.clone(this.signatureData), value => {
|
||||
return streamTransform(streamClone(this.signatureData), value => {
|
||||
length += value.length;
|
||||
}, () => {
|
||||
const arr = [];
|
||||
@ -774,7 +774,7 @@ class SignaturePacket {
|
||||
toHash = this.toHash(signatureType, data, detached);
|
||||
hash = await this.hash(signatureType, data, toHash);
|
||||
}
|
||||
hash = await stream.readToEnd(hash);
|
||||
hash = await streamReadToEnd(hash);
|
||||
if (this.signedHashValue[0] !== hash[0] ||
|
||||
this.signedHashValue[1] !== hash[1]) {
|
||||
throw new Error('Signed digest did not match');
|
||||
|
@ -15,7 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { slice as streamSlice, passiveClone as streamPassiveClone, readToEnd as streamReadToEnd, concat as streamConcat, fromAsync as streamFromAsync, getReader as streamGetReader, getWriter as streamGetWriter, clone as streamClone, pipe as streamPipe, transformPair as streamTransformPair, isArrayStream, parse as streamParse } from '@openpgp/web-stream-tools';
|
||||
import { cipherMode, getRandomBytes, getCipherParams, computeDigest } from '../crypto';
|
||||
import computeHKDF from '../crypto/hkdf';
|
||||
import enums from '../enums';
|
||||
@ -82,7 +82,7 @@ class SymEncryptedIntegrityProtectedDataPacket {
|
||||
}
|
||||
|
||||
async read(bytes) {
|
||||
await stream.parse(bytes, async reader => {
|
||||
await streamParse(bytes, async reader => {
|
||||
this.version = await reader.readByte();
|
||||
// - A one-octet version number with value 1 or 2.
|
||||
if (this.version !== 1 && this.version !== 2) {
|
||||
@ -139,7 +139,7 @@ class SymEncryptedIntegrityProtectedDataPacket {
|
||||
}
|
||||
|
||||
let bytes = this.packets.write();
|
||||
if (stream.isArrayStream(bytes)) bytes = await stream.readToEnd(bytes);
|
||||
if (isArrayStream(bytes)) bytes = await streamReadToEnd(bytes);
|
||||
|
||||
if (this.version === 2) {
|
||||
this.cipherAlgorithm = sessionKeyAlgorithm;
|
||||
@ -152,7 +152,7 @@ class SymEncryptedIntegrityProtectedDataPacket {
|
||||
const mdc = new Uint8Array([0xD3, 0x14]); // modification detection code packet
|
||||
|
||||
const tohash = util.concat([prefix, bytes, mdc]);
|
||||
const hash = await computeDigest(enums.hash.sha1, stream.passiveClone(tohash));
|
||||
const hash = await computeDigest(enums.hash.sha1, streamPassiveClone(tohash));
|
||||
const plaintext = util.concat([tohash, hash]);
|
||||
|
||||
this.encrypted = await cipherMode.cfb.encrypt(sessionKeyAlgorithm, key, plaintext, new Uint8Array(blockSize), config);
|
||||
@ -179,8 +179,8 @@ class SymEncryptedIntegrityProtectedDataPacket {
|
||||
throw new Error('Unexpected session key size');
|
||||
}
|
||||
|
||||
let encrypted = stream.clone(this.encrypted);
|
||||
if (stream.isArrayStream(encrypted)) encrypted = await stream.readToEnd(encrypted);
|
||||
let encrypted = streamClone(this.encrypted);
|
||||
if (isArrayStream(encrypted)) encrypted = await streamReadToEnd(encrypted);
|
||||
|
||||
let packetbytes;
|
||||
if (this.version === 2) {
|
||||
@ -195,22 +195,22 @@ class SymEncryptedIntegrityProtectedDataPacket {
|
||||
|
||||
// there must be a modification detection code packet as the
|
||||
// last packet and everything gets hashed except the hash itself
|
||||
const realHash = stream.slice(stream.passiveClone(decrypted), -20);
|
||||
const tohash = stream.slice(decrypted, 0, -20);
|
||||
const realHash = streamSlice(streamPassiveClone(decrypted), -20);
|
||||
const tohash = streamSlice(decrypted, 0, -20);
|
||||
const verifyHash = Promise.all([
|
||||
stream.readToEnd(await computeDigest(enums.hash.sha1, stream.passiveClone(tohash))),
|
||||
stream.readToEnd(realHash)
|
||||
streamReadToEnd(await computeDigest(enums.hash.sha1, streamPassiveClone(tohash))),
|
||||
streamReadToEnd(realHash)
|
||||
]).then(([hash, mdc]) => {
|
||||
if (!util.equalsUint8Array(hash, mdc)) {
|
||||
throw new Error('Modification detected.');
|
||||
}
|
||||
return new Uint8Array();
|
||||
});
|
||||
const bytes = stream.slice(tohash, blockSize + 2); // Remove random prefix
|
||||
packetbytes = stream.slice(bytes, 0, -2); // Remove MDC packet
|
||||
packetbytes = stream.concat([packetbytes, stream.fromAsync(() => verifyHash)]);
|
||||
const bytes = streamSlice(tohash, blockSize + 2); // Remove random prefix
|
||||
packetbytes = streamSlice(bytes, 0, -2); // Remove MDC packet
|
||||
packetbytes = streamConcat([packetbytes, streamFromAsync(() => verifyHash)]);
|
||||
if (!util.isStream(encrypted) || !config.allowUnauthenticatedStream) {
|
||||
packetbytes = await stream.readToEnd(packetbytes);
|
||||
packetbytes = await streamReadToEnd(packetbytes);
|
||||
}
|
||||
}
|
||||
|
||||
@ -268,17 +268,17 @@ export async function runAEAD(packet, fn, key, data) {
|
||||
// ivView is unused in this case
|
||||
}
|
||||
const modeInstance = await mode(packet.cipherAlgorithm, key);
|
||||
return stream.transformPair(data, async (readable, writable) => {
|
||||
return streamTransformPair(data, async (readable, writable) => {
|
||||
if (util.isStream(readable) !== 'array') {
|
||||
const buffer = new TransformStream({}, {
|
||||
highWaterMark: util.getHardwareConcurrency() * 2 ** (packet.chunkSizeByte + 6),
|
||||
size: array => array.length
|
||||
});
|
||||
stream.pipe(buffer.readable, writable);
|
||||
streamPipe(buffer.readable, writable);
|
||||
writable = buffer.writable;
|
||||
}
|
||||
const reader = stream.getReader(readable);
|
||||
const writer = stream.getWriter(writable);
|
||||
const reader = streamGetReader(readable);
|
||||
const writer = streamGetWriter(writable);
|
||||
try {
|
||||
while (true) {
|
||||
let chunk = await reader.readBytes(chunkSize + tagLengthIfDecrypting) || new Uint8Array();
|
||||
|
@ -15,7 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { readToEnd as streamReadToEnd, clone as streamClone } from '@openpgp/web-stream-tools';
|
||||
import { cipherMode, getCipherParams } from '../crypto';
|
||||
import enums from '../enums';
|
||||
import util from '../util';
|
||||
@ -87,7 +87,7 @@ class SymmetricallyEncryptedDataPacket {
|
||||
}
|
||||
|
||||
const { blockSize } = getCipherParams(sessionKeyAlgorithm);
|
||||
const encrypted = await stream.readToEnd(stream.clone(this.encrypted));
|
||||
const encrypted = await streamReadToEnd(streamClone(this.encrypted));
|
||||
const decrypted = await cipherMode.cfb.decrypt(sessionKeyAlgorithm, key,
|
||||
encrypted.subarray(blockSize + 2),
|
||||
encrypted.subarray(2, blockSize + 2)
|
||||
|
20
src/util.js
20
src/util.js
@ -22,7 +22,7 @@
|
||||
* @module util
|
||||
*/
|
||||
|
||||
import * as stream from '@openpgp/web-stream-tools';
|
||||
import { concat as streamConcat, transform as streamTransform, concatUint8Array, isStream, isUint8Array } from '@openpgp/web-stream-tools';
|
||||
import { createRequire } from 'module'; // Must be stripped in browser built
|
||||
import enums from './enums';
|
||||
import defaultConfig from './config';
|
||||
@ -45,9 +45,9 @@ const util = {
|
||||
return data instanceof Array;
|
||||
},
|
||||
|
||||
isUint8Array: stream.isUint8Array,
|
||||
isUint8Array: isUint8Array,
|
||||
|
||||
isStream: stream.isStream,
|
||||
isStream: isStream,
|
||||
|
||||
/**
|
||||
* Load noble-curves lib on demand and return the requested curve function
|
||||
@ -218,7 +218,7 @@ const util = {
|
||||
* @returns {Uint8Array} An array of 8-bit integers.
|
||||
*/
|
||||
stringToUint8Array: function (str) {
|
||||
return stream.transform(str, str => {
|
||||
return streamTransform(str, str => {
|
||||
if (!util.isString(str)) {
|
||||
throw new Error('stringToUint8Array: Data must be in the form of a string');
|
||||
}
|
||||
@ -259,7 +259,7 @@ const util = {
|
||||
function process(value, lastChunk = false) {
|
||||
return encoder.encode(value, { stream: !lastChunk });
|
||||
}
|
||||
return stream.transform(str, process, () => process('', true));
|
||||
return streamTransform(str, process, () => process('', true));
|
||||
},
|
||||
|
||||
/**
|
||||
@ -273,7 +273,7 @@ const util = {
|
||||
function process(value, lastChunk = false) {
|
||||
return decoder.decode(value, { stream: !lastChunk });
|
||||
}
|
||||
return stream.transform(utf8, process, () => process(new Uint8Array(), true));
|
||||
return streamTransform(utf8, process, () => process(new Uint8Array(), true));
|
||||
},
|
||||
|
||||
/**
|
||||
@ -282,14 +282,14 @@ const util = {
|
||||
* @param {Array<Uint8Array|String|ReadableStream>} Array - Of Uint8Arrays/Strings/Streams to concatenate
|
||||
* @returns {Uint8Array|String|ReadableStream} Concatenated array.
|
||||
*/
|
||||
concat: stream.concat,
|
||||
concat: streamConcat,
|
||||
|
||||
/**
|
||||
* Concat Uint8Arrays
|
||||
* @param {Array<Uint8Array>} Array - Of Uint8Arrays to concatenate
|
||||
* @returns {Uint8Array} Concatenated array.
|
||||
*/
|
||||
concatUint8Array: stream.concatUint8Array,
|
||||
concatUint8Array: concatUint8Array,
|
||||
|
||||
/**
|
||||
* Check Uint8Array equality
|
||||
@ -490,7 +490,7 @@ const util = {
|
||||
const LF = 10;
|
||||
let carryOverCR = false;
|
||||
|
||||
return stream.transform(data, bytes => {
|
||||
return streamTransform(data, bytes => {
|
||||
if (carryOverCR) {
|
||||
bytes = util.concatUint8Array([new Uint8Array([CR]), bytes]);
|
||||
}
|
||||
@ -540,7 +540,7 @@ const util = {
|
||||
const LF = 10;
|
||||
let carryOverCR = false;
|
||||
|
||||
return stream.transform(data, bytes => {
|
||||
return streamTransform(data, bytes => {
|
||||
if (carryOverCR && bytes[0] !== LF) {
|
||||
bytes = util.concatUint8Array([new Uint8Array([CR]), bytes]);
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user