diff --git a/src/packet/packet.js b/src/packet/packet.js index 1761fd33..a8ff1a43 100644 --- a/src/packet/packet.js +++ b/src/packet/packet.js @@ -245,7 +245,7 @@ export default { if (streaming) { // Send the remainder of the packet to the callback as a stream reader.releaseLock(); - packet = stream.subarray(stream.clone(input), 0, packet_length); + packet = stream.slice(stream.clone(input), 0, packet_length); await callback({ tag, packet }); // Read the entire packet before parsing the next one diff --git a/src/packet/sym_encrypted_integrity_protected.js b/src/packet/sym_encrypted_integrity_protected.js index f31639a8..5ec88ba1 100644 --- a/src/packet/sym_encrypted_integrity_protected.js +++ b/src/packet/sym_encrypted_integrity_protected.js @@ -103,7 +103,7 @@ SymEncryptedIntegrityProtected.prototype.encrypt = async function (sessionKeyAlg } else { tohash = await stream.readToEnd(tohash); this.encrypted = crypto.cfb.encrypt(prefixrandom, sessionKeyAlgorithm, tohash, key, false); - this.encrypted = stream.subarray(this.encrypted, 0, prefix.length + tohash.length); + this.encrypted = stream.slice(this.encrypted, 0, prefix.length + tohash.length); } return true; }; @@ -127,17 +127,17 @@ SymEncryptedIntegrityProtected.prototype.decrypt = async function (sessionKeyAlg // there must be a modification detection code packet as the // last packet and everything gets hashed except the hash itself - const encryptedPrefix = await stream.readToEnd(stream.subarray(encryptedClone, 0, crypto.cipher[sessionKeyAlgorithm].blockSize + 2)); + const encryptedPrefix = await stream.readToEnd(stream.slice(encryptedClone, 0, crypto.cipher[sessionKeyAlgorithm].blockSize + 2)); const prefix = crypto.cfb.mdc(sessionKeyAlgorithm, key, encryptedPrefix); - const bytes = stream.subarray(stream.clone(decrypted), 0, -20); + const bytes = stream.slice(stream.clone(decrypted), 0, -20); const tohash = util.concat([prefix, stream.clone(bytes)]); this.hash = util.Uint8Array_to_str(await stream.readToEnd(crypto.hash.sha1(tohash))); - const mdc = util.Uint8Array_to_str(await stream.readToEnd(stream.subarray(decrypted, -20))); + const mdc = util.Uint8Array_to_str(await stream.readToEnd(stream.slice(decrypted, -20))); if (this.hash !== mdc) { throw new Error('Modification detected.'); } else { - await this.packets.read(stream.subarray(bytes, 0, -2)); + await this.packets.read(stream.slice(bytes, 0, -2)); } return true; @@ -169,7 +169,7 @@ function aesDecrypt(algo, ct, key) { const cfb = new AES_CFB_Decrypt(key); pt = stream.transform(ct, value => cfb.process(value).result, () => cfb.finish().result); } - return stream.subarray(pt, crypto.cipher[algo].blockSize + 2); // Remove random prefix + return stream.slice(pt, crypto.cipher[algo].blockSize + 2); // Remove random prefix } function nodeEncrypt(algo, pt, key) { diff --git a/src/stream.js b/src/stream.js index c415814d..f225d080 100644 --- a/src/stream.js +++ b/src/stream.js @@ -56,7 +56,7 @@ function tee(input) { teed[0].externalBuffer = teed[1].externalBuffer = input.externalBuffer; return teed; } - return [subarray(input), subarray(input)]; + return [slice(input), slice(input)]; } function clone(input) { @@ -74,10 +74,10 @@ function clone(input) { }); return teed[1]; } - return subarray(input); + return slice(input); } -function subarray(input, begin=0, end=Infinity) { +function slice(input, begin=0, end=Infinity) { if (util.isStream(input)) { if (begin >= 0 && end >= 0) { const reader = getReader(input); @@ -87,7 +87,7 @@ function subarray(input, begin=0, end=Infinity) { const { done, value } = await reader.read(); if (!done && bytesRead < end) { if (bytesRead + value.length >= begin) { - controller.enqueue(value.subarray(Math.max(begin - bytesRead, 0), end - bytesRead)); + controller.enqueue(slice(value, Math.max(begin - bytesRead, 0), end - bytesRead)); } bytesRead += value.length; await this.pull(controller); // Only necessary if the above call to enqueue() didn't happen @@ -98,15 +98,15 @@ function subarray(input, begin=0, end=Infinity) { }); } // TODO: Don't read entire stream into memory here. - return fromAsync(async () => (await readToEnd(input)).subarray(begin, end)); - } - if (util.isString(input)) { - return input.substr(begin, end); + return fromAsync(async () => slice(await readToEnd(input), begin, end)); } if (input.externalBuffer) { input = util.concat(input.externalBuffer.concat([input])); } - return input.subarray(begin, end); + if (util.isUint8Array(input)) { + return input.subarray(begin, end); + } + return input.slice(begin, end); } async function readToEnd(input, join) { @@ -201,7 +201,7 @@ if (nodeStream) { } -export default { concat, getReader, transform, clone, subarray, readToEnd, cancel, nodeToWeb, webToNode, fromAsync }; +export default { concat, getReader, transform, clone, slice, readToEnd, cancel, nodeToWeb, webToNode, fromAsync }; /*const readerAcquiredMap = new Map(); @@ -300,7 +300,7 @@ Reader.prototype.readByte = async function() { const { done, value } = await this.read(); if (done) return; const byte = value[0]; - this.unshift(value.subarray(1)); + this.unshift(slice(value, 1)); return byte; }; @@ -317,8 +317,8 @@ Reader.prototype.readBytes = async function(length) { bufferLength += value.length; if (bufferLength >= length) { const bufferConcat = util.concat(buffer); - this.unshift(bufferConcat.subarray(length)); - return bufferConcat.subarray(0, length); + this.unshift(slice(bufferConcat, length)); + return slice(bufferConcat, 0, length); } } };