Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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) {
const b64ResourceId = utils.toBase64(resourceId);
throw new DecryptionFailed({ error, b64ResourceId });
}
async upload(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
const encryptor = await this._dataProtector.makeEncryptorStream(sharingOptions);
const { resourceId } = encryptor;
const totalClearSize = getDataLength(clearData);
const totalEncryptedSize = encryptor.getEncryptedSize(totalClearSize);
const { type, ...fileMetadata } = outputOptions;
// clearContentLength shouldn't be used since we may not have that
// information. We leave it here only for compatibility with SDKs up to 2.2.1
const metadata = { ...fileMetadata, clearContentLength: totalClearSize, encryptionFormat: getStreamEncryptionFormatDescription() };
const encryptedMetadata = await this._encryptAndShareMetadata(metadata, resourceId);
const {
urls,
headers,
service,
recommended_chunk_size: recommendedChunkSize
} = await this._client.send('get file upload url', {
resource_id: resourceId,
metadata: encryptedMetadata,
it(`can encrypt and decrypt a ${size} ${getConstructorName(type)}`, async () => {
const onProgress = sinon.spy();
const encrypted = await aliceLaptop.encryptData(clear, { onProgress });
expectSameType(encrypted, clear);
expectProgressReport(onProgress, getDataLength(encrypted));
onProgress.resetHistory();
const decrypted = await aliceLaptop.decryptData(encrypted, { onProgress });
expectSameType(decrypted, clear);
expectDeepEqual(decrypted, clear);
expectProgressReport(onProgress, getDataLength(decrypted), encryptionV4.defaultMaxEncryptedChunkSize - encryptionV4.overhead);
});
});
decryptor.on('initialized', () => {
const encryptedSize = getDataLength(encryptedData);
const clearSize = decryptor.getClearSize(encryptedSize);
progressHandler.start(clearSize);
});
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 _streamEncryptData(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions, b64ResourceId?: b64string): Promise {
const slicer = new SlicerStream({ source: clearData });
const encryptor = await this.makeEncryptorStream(sharingOptions, b64ResourceId);
const clearSize = getDataLength(clearData);
const encryptedSize = encryptor.getEncryptedSize(clearSize);
const progressHandler = new ProgressHandler(progressOptions).start(encryptedSize);
encryptor.on('data', (chunk: Uint8Array) => progressHandler.report(chunk.byteLength));
const merger = new MergerStream(outputOptions);
return new Promise((resolve, reject) => {
[slicer, encryptor, merger].forEach(s => s.on('error', reject));
slicer.pipe(encryptor).pipe(merger).on('data', resolve);
});
}
async _simpleEncryptDataWithResourceId(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions, b64ResourceId: b64string): Promise {
const encryption = getSimpleEncryptionWithFixedResourceId();
const clearSize = getDataLength(clearData);
const encryptedSize = encryption.getEncryptedSize(clearSize);
const progressHandler = new ProgressHandler(progressOptions).start(encryptedSize);
const castClearData = await castData(clearData, { type: Uint8Array });
if (typeof b64ResourceId !== 'string')
throw new InternalError('Assertion error: called _simpleEncryptDataWithResourceId without a resourceId');
const resourceId = utils.fromBase64(b64ResourceId);
const key = await this._resourceManager.findKeyFromResourceId(resourceId);
const encryptedData = encryption.serialize(encryption.encrypt(key, castClearData, resourceId));
const castEncryptedData = await castData(encryptedData, outputOptions);
progressHandler.report(encryptedSize);
return castEncryptedData;
}
async encryptData(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions, b64ResourceId?: b64string): Promise {
if (getDataLength(clearData) >= STREAM_THRESHOLD)
return this._streamEncryptData(clearData, sharingOptions, outputOptions, progressOptions, b64ResourceId);
if (b64ResourceId)
return this._simpleEncryptDataWithResourceId(clearData, sharingOptions, outputOptions, progressOptions, b64ResourceId);
return this._simpleEncryptData(clearData, sharingOptions, outputOptions, progressOptions);
}