diff --git a/src/crypto/hash/index.js b/src/crypto/hash/index.js index 8c45d532..f9a885f1 100644 --- a/src/crypto/hash/index.js +++ b/src/crypto/hash/index.js @@ -38,13 +38,9 @@ function node_hash(type) { function hashjs_hash(hash) { return function(data) { const hashInstance = hash(); - return stream.transform(data, (done, value) => { - if (!done) { - hashInstance.update(value); - } else { - return util.hex_to_Uint8Array(hashInstance.digest('hex')); - } - }); + return stream.transform(data, value => { + hashInstance.update(value); + }, () => util.hex_to_Uint8Array(hashInstance.digest('hex'))); }; } diff --git a/src/encoding/armor.js b/src/encoding/armor.js index 6c3b2e31..a14b29c3 100644 --- a/src/encoding/armor.js +++ b/src/encoding/armor.js @@ -168,16 +168,11 @@ const crc_table = [ function createcrc24(input) { let crc = 0xB704CE; - return stream.transform(input, (done, value) => { - if (!done) { - for (let index = 0; index < value.length; index++) { - crc = (crc << 8) ^ crc_table[((crc >> 16) ^ value[index]) & 0xff]; - } - } else { - crc &= 0xffffff; - return new Uint8Array([crc >> 16, (crc >> 8) & 0xFF, crc & 0xFF]); + return stream.transform(input, value => { + for (let index = 0; index < value.length; index++) { + crc = (crc << 8) ^ crc_table[((crc >> 16) ^ value[index]) & 0xff]; } - }); + }, () => new Uint8Array([crc >> 16, crc >> 8, crc])); } /** diff --git a/src/encoding/base64.js b/src/encoding/base64.js index 3d735d79..135fd757 100644 --- a/src/encoding/base64.js +++ b/src/encoding/base64.js @@ -39,56 +39,55 @@ function s2r(t, u = false) { let l = 0; let s = 0; - return stream.transform(t, (done, value) => { + return stream.transform(t, value => { const r = []; - - if (!done) { - const tl = value.length; - for (let n = 0; n < tl; n++) { - c = value[n]; - if (s === 0) { - r.push(b64[(c >> 2) & 63]); - a = (c & 3) << 4; - } else if (s === 1) { - r.push(b64[a | ((c >> 4) & 15)]); - a = (c & 15) << 2; - } else if (s === 2) { - r.push(b64[a | ((c >> 6) & 3)]); - l += 1; - if ((l % 60) === 0 && !u) { - r.push(10); // "\n" - } - r.push(b64[c & 63]); - } + const tl = value.length; + for (let n = 0; n < tl; n++) { + c = value[n]; + if (s === 0) { + r.push(b64[(c >> 2) & 63]); + a = (c & 3) << 4; + } else if (s === 1) { + r.push(b64[a | ((c >> 4) & 15)]); + a = (c & 15) << 2; + } else if (s === 2) { + r.push(b64[a | ((c >> 6) & 3)]); l += 1; if ((l % 60) === 0 && !u) { r.push(10); // "\n" } + r.push(b64[c & 63]); + } + l += 1; + if ((l % 60) === 0 && !u) { + r.push(10); // "\n" + } - s += 1; - if (s === 3) { - s = 0; - } + s += 1; + if (s === 3) { + s = 0; } - } else { - if (s > 0) { - r.push(b64[a]); - l += 1; - if ((l % 60) === 0 && !u) { - r.push(10); // "\n" - } - if (!u) { - r.push(61); // "=" - l += 1; - } + } + return new Uint8Array(r); + }, () => { + const r = []; + if (s > 0) { + r.push(b64[a]); + l += 1; + if ((l % 60) === 0 && !u) { + r.push(10); // "\n" } - if (s === 1 && !u) { - if ((l % 60) === 0 && !u) { - r.push(10); // "\n" - } + if (!u) { r.push(61); // "=" + l += 1; } } + if (s === 1 && !u) { + if ((l % 60) === 0 && !u) { + r.push(10); // "\n" + } + r.push(61); // "=" + } return new Uint8Array(r); }); } @@ -108,22 +107,20 @@ function r2s(t, u) { let s = 0; let a = 0; - return stream.transform(t, (done, value) => { - if (!done) { - const r = []; - const tl = value.length; - for (let n = 0; n < tl; n++) { - c = b64.indexOf(value[n]); - if (c >= 0) { - if (s) { - r.push(a | ((c >> (6 - s)) & 255)); - } - s = (s + 2) & 7; - a = (c << s) & 255; + return stream.transform(t, value => { + const r = []; + const tl = value.length; + for (let n = 0; n < tl; n++) { + c = b64.indexOf(value[n]); + if (c >= 0) { + if (s) { + r.push(a | ((c >> (6 - s)) & 255)); } + s = (s + 2) & 7; + a = (c << s) & 255; } - return new Uint8Array(r); } + return new Uint8Array(r); }); } diff --git a/src/packet/packetlist.js b/src/packet/packetlist.js index b78b9bc0..05644417 100644 --- a/src/packet/packetlist.js +++ b/src/packet/packetlist.js @@ -80,22 +80,18 @@ List.prototype.write = function () { let bufferLength = 0; const minLength = 512; arr.push(packetParser.writeTag(this[i].tag)); - arr.push(stream.transform(packetbytes, (done, value) => { - if (!done) { - buffer.push(value); - bufferLength += value.length; - if (bufferLength >= minLength) { - const powerOf2 = Math.min(Math.log(bufferLength) / Math.LN2 | 0, 30); - const chunkSize = 2 ** powerOf2; - const bufferConcat = util.concatUint8Array([packetParser.writePartialLength(powerOf2)].concat(buffer)); - buffer = [bufferConcat.subarray(1 + chunkSize)]; - bufferLength = buffer[0].length; - return bufferConcat.subarray(0, 1 + chunkSize); - } - } else { - return util.concatUint8Array([packetParser.writeSimpleLength(bufferLength)].concat(buffer)); + arr.push(stream.transform(packetbytes, value => { + buffer.push(value); + bufferLength += value.length; + if (bufferLength >= minLength) { + const powerOf2 = Math.min(Math.log(bufferLength) / Math.LN2 | 0, 30); + const chunkSize = 2 ** powerOf2; + const bufferConcat = util.concatUint8Array([packetParser.writePartialLength(powerOf2)].concat(buffer)); + buffer = [bufferConcat.subarray(1 + chunkSize)]; + bufferLength = buffer[0].length; + return bufferConcat.subarray(0, 1 + chunkSize); } - })); + }, () => util.concatUint8Array([packetParser.writeSimpleLength(bufferLength)].concat(buffer)))); } else { arr.push(packetParser.writeHeader(this[i].tag, packetbytes.length)); arr.push(packetbytes); diff --git a/src/packet/sym_encrypted_integrity_protected.js b/src/packet/sym_encrypted_integrity_protected.js index d80a664c..8bcced81 100644 --- a/src/packet/sym_encrypted_integrity_protected.js +++ b/src/packet/sym_encrypted_integrity_protected.js @@ -158,12 +158,7 @@ function aesEncrypt(algo, pt, key) { return nodeEncrypt(algo, pt, key); } // asm.js fallback const cfb = new AES_CFB_Encrypt(key); - return stream.transform(pt, (done, value) => { - if (!done) { - return cfb.process(value).result; - } - return cfb.finish().result; - }); + return stream.transform(pt, value => cfb.process(value).result, () => cfb.finish().result); } function aesDecrypt(algo, ct, key) { @@ -172,12 +167,7 @@ function aesDecrypt(algo, ct, key) { pt = nodeDecrypt(algo, ct, key); } else { // asm.js fallback const cfb = new AES_CFB_Decrypt(key); - pt = stream.transform(ct, (done, value) => { - if (!done) { - return cfb.process(value).result; - } - return cfb.finish().result; - }); + pt = stream.transform(ct, value => cfb.process(value).result, () => cfb.finish().result); } return stream.subarray(pt, crypto.cipher[algo].blockSize + 2); // Remove random prefix } diff --git a/src/stream.js b/src/stream.js index 274a1b4a..8eba2921 100644 --- a/src/stream.js +++ b/src/stream.js @@ -21,14 +21,14 @@ function getReader(input) { return new Reader(input); } -function transform(input, fn) { +function transform(input, process = () => undefined, finish = () => undefined) { if (util.isStream(input)) { const reader = getReader(input); return new ReadableStream({ async pull(controller) { try { const { done, value } = await reader.read(); - const result = await fn(done, value); + const result = await (!done ? process : finish)(value); if (result) controller.enqueue(result); else if (!done) await this.pull(controller); // ??? Chrome bug? if (done) controller.close(); @@ -38,8 +38,8 @@ function transform(input, fn) { } }); } - const result1 = fn(false, input); - const result2 = fn(true, undefined); + const result1 = process(input); + const result2 = finish(undefined); if (result1 && result2) return util.concatUint8Array([result1, result2]); return result1 || result2; } diff --git a/src/util.js b/src/util.js index a7b6701a..fe60b339 100644 --- a/src/util.js +++ b/src/util.js @@ -29,7 +29,7 @@ import rfc2822 from 'address-rfc2822'; import config from './config'; import util from './util'; // re-import module to access util functions import b64 from './encoding/base64'; -import Stream from './stream'; +import stream from './stream'; const isIE11 = typeof navigator !== 'undefined' && !!navigator.userAgent.match(/Trident\/7\.0.*rv:([0-9.]+).*\).*Gecko$/); @@ -290,7 +290,7 @@ export default { let totalLength = 0; for (let i = 0; i < arrays.length; i++) { if (util.isStream(arrays[i])) { - return Stream.concat(arrays); + return stream.concat(arrays); } if (!util.isUint8Array(arrays[i])) { throw new Error('concatUint8Array: Data must be in the form of a Uint8Array'); @@ -628,11 +628,7 @@ export default { * Normalize line endings to \r\n */ canonicalizeEOL: function(text) { - return stream.transform(text, (done, value) => { - if (!done) { - return value.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, "\r\n"); - } - }); + return stream.transform(text, value => value.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, "\r\n")); }, /**