Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
stream.on('end', async () => {
try {
expect(output).to.have.lengthOf(1);
expect(output[0]).to.be.an.instanceOf(type);
const outputBytes = await castData(output[0], { type: Uint8Array });
expect(outputBytes).to.deep.equal(bytes);
if (global.Blob && output[0] instanceof global.Blob) {
// $FlowExpectedError
expect(output[0].type).to.equal(options.mime);
}
if (global.File && output[0] instanceof global.File) {
// $FlowExpectedError
expect(output[0].name).to.equal(options.name);
}
resolve();
} catch (e) {
reject(e);
}
});
});
const progressHandler = new ProgressHandler(progressOptions).start(clearSize);
const resourceId = encryption.extractResourceId(castEncryptedData);
const key = await this._resourceManager.findKeyFromResourceId(resourceId);
let clearData;
try {
// $FlowIKnow Already checked we are using a simple encryption
clearData = encryption.decrypt(key, encryption.unserialize(castEncryptedData));
} catch (error) {
const b64ResourceId = utils.toBase64(resourceId);
throw new DecryptionFailed({ error, b64ResourceId });
}
const castClearData = await castData(clearData, outputOptions);
progressHandler.report(clearSize);
return castClearData;
}
async _pushLastChunk() {
// Always push last chunk even if zero bytes
const uint8array = this._buffer.consume(this._buffer.byteSize());
const lastChunk = await castData(uint8array, this._options);
this.push(lastChunk);
}
}
async _simpleDecryptData(encryptedData: Data, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
const castEncryptedData = await castData(encryptedData, { type: Uint8Array });
const encryption = extractEncryptionFormat(castEncryptedData);
const encryptedSize = getDataLength(castEncryptedData);
// $FlowIKnow Already checked we are using a simple encryption
const clearSize = encryption.getClearSize(encryptedSize);
const progressHandler = new ProgressHandler(progressOptions).start(clearSize);
const resourceId = encryption.extractResourceId(castEncryptedData);
const key = await this._resourceManager.findKeyFromResourceId(resourceId);
let clearData;
try {
// $FlowIKnow Already checked we are using a simple encryption
clearData = encryption.decrypt(key, encryption.unserialize(castEncryptedData));
} catch (error) {
async _simpleEncryptData(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
const encryption = getSimpleEncryption();
const clearSize = getDataLength(clearData);
const encryptedSize = encryption.getEncryptedSize(clearSize);
const progressHandler = new ProgressHandler(progressOptions).start(encryptedSize);
const castClearData = await castData(clearData, { type: Uint8Array });
const { key } = makeResource();
const encryptedData = encryption.serialize(encryption.encrypt(key, castClearData));
const resourceId = encryption.extractResourceId(encryptedData);
await this._shareResources([{ resourceId, key }], sharingOptions, true);
const castEncryptedData = await castData(encryptedData, outputOptions);
progressHandler.report(encryptedSize);
return castEncryptedData;
}
async _simpleEncryptData(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
const encryption = getSimpleEncryption();
const clearSize = getDataLength(clearData);
const encryptedSize = encryption.getEncryptedSize(clearSize);
const progressHandler = new ProgressHandler(progressOptions).start(encryptedSize);
const castClearData = await castData(clearData, { type: Uint8Array });
const { key } = makeResource();
const encryptedData = encryption.serialize(encryption.encrypt(key, castClearData));
const resourceId = encryption.extractResourceId(encryptedData);
await this._shareResources([{ resourceId, key }], sharingOptions, true);
const castEncryptedData = await castData(encryptedData, outputOptions);
progressHandler.report(encryptedSize);
return castEncryptedData;
}
async decryptData(encryptedData: Data, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
const leadingBytes = await castData(encryptedData, { type: Uint8Array }, SAFE_EXTRACTION_LENGTH);
const encryption = extractEncryptionFormat(leadingBytes);
if (encryption.features.chunks)
return this._streamDecryptData(encryptedData, outputOptions, progressOptions);
return this._simpleDecryptData(encryptedData, outputOptions, progressOptions);
}
async getResourceId(encryptedData: Uint8Array): Promise {
this.assert(statuses.READY, 'get a resource id');
assertDataType(encryptedData, 'encryptedData');
const castEncryptedData = await castData(encryptedData, { type: Uint8Array }, SAFE_EXTRACTION_LENGTH);
const encryption = extractEncryptionFormat(castEncryptedData);
return utils.toBase64(encryption.extractResourceId(castEncryptedData));
}