diff --git a/web/media/encryption-utils.js b/web/media/encryption-utils.js --- a/web/media/encryption-utils.js +++ b/web/media/encryption-utils.js @@ -21,6 +21,7 @@ +file: File, +uri: string, +encryptionKey: string, + +sha256Hash: string, }; async function encryptFile(input: File): Promise<{ @@ -55,11 +56,14 @@ const startEncrypt = Date.now(); const paddedLength = calculatePaddedLength(data.length); const shouldPad = paddedLength <= PADDING_THRESHOLD; - let key, encryptedData; + let key, encryptedData, sha256; try { const plaintextData = shouldPad ? pad(data) : data; key = await AES.generateKey(); encryptedData = await AES.encrypt(key, plaintextData); + + const hashBytes = await crypto.subtle.digest('SHA-256', encryptedData); + sha256 = btoa(String.fromCharCode(...new Uint8Array(hashBytes))); } catch (e) { success = false; exceptionMessage = getMessageForException(e); @@ -69,11 +73,14 @@ dataSize: encryptedData?.byteLength ?? -1, isPadded: shouldPad, time: Date.now() - startEncrypt, - sha256: null, + sha256, success, exceptionMessage, }); - if (!success || !encryptedData || !key) { + if (encryptedData && !sha256) { + return { steps, result: { success: false, reason: 'digest_failed' } }; + } + if (!success || !encryptedData || !key || !sha256) { return { steps, result: { success: false, reason: 'encryption_failed' } }; } @@ -86,6 +93,7 @@ file: output, uri: URL.createObjectURL(output), encryptionKey: uintArrayToHexString(key), + sha256Hash: sha256, }, }; } diff --git a/web/olm/olm-utils.js b/web/olm/olm-utils.js --- a/web/olm/olm-utils.js +++ b/web/olm/olm-utils.js @@ -1,5 +1,6 @@ // @flow +import type { Utility } from '@commapp/olm'; import olm from '@commapp/olm'; declare var olmFilename: string; @@ -13,4 +14,16 @@ return await olm.init({ locateFile }); } -export { initOlm }; +let olmUtilityInstance; +function olmUtility(): Promise { + if (!olmUtilityInstance) { + olmUtilityInstance = (async () => { + await initOlm(); + olmUtilityInstance = new olm.Utility(); + return olmUtilityInstance; + })(); + } + return Promise.resolve(olmUtilityInstance); +} + +export { initOlm, olmUtility };