Changeset View
Changeset View
Standalone View
Standalone View
web/media/encryption-utils.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import { thumbHashToDataURL } from 'thumbhash'; | |||||
import { hexToUintArray, uintArrayToHexString } from 'lib/media/data-utils.js'; | import { hexToUintArray, uintArrayToHexString } from 'lib/media/data-utils.js'; | ||||
import { fileInfoFromData } from 'lib/media/file-utils.js'; | import { fileInfoFromData } from 'lib/media/file-utils.js'; | ||||
import { fetchableMediaURI } from 'lib/media/media-utils.js'; | import { fetchableMediaURI } from 'lib/media/media-utils.js'; | ||||
import type { | import type { | ||||
MediaMissionFailure, | MediaMissionFailure, | ||||
MediaMissionStep, | MediaMissionStep, | ||||
} from 'lib/types/media-types.js'; | } from 'lib/types/media-types.js'; | ||||
import { getMessageForException } from 'lib/utils/errors.js'; | import { getMessageForException } from 'lib/utils/errors.js'; | ||||
import { calculatePaddedLength, pad, unpad } from 'lib/utils/pkcs7-padding.js'; | import { calculatePaddedLength, pad, unpad } from 'lib/utils/pkcs7-padding.js'; | ||||
import * as AES from './aes-crypto-utils.js'; | import * as AES from './aes-crypto-utils.js'; | ||||
import { base64DecodeBuffer } from '../utils/base64-utils.js'; | |||||
const PADDING_THRESHOLD = 5000000; // 5MB | const PADDING_THRESHOLD = 5000000; // 5MB | ||||
type EncryptFileResult = { | type EncryptFileResult = { | ||||
+success: true, | +success: true, | ||||
+file: File, | +file: File, | ||||
+uri: string, | +uri: string, | ||||
+encryptionKey: string, | +encryptionKey: string, | ||||
▲ Show 20 Lines • Show All 207 Lines • ▼ Show 20 Lines | return { | ||||
steps, | steps, | ||||
result: { success: false, reason: 'save_blob_failed' }, | result: { success: false, reason: 'save_blob_failed' }, | ||||
}; | }; | ||||
} | } | ||||
return { steps, result: { success: true, uri: objectURL } }; | return { steps, result: { success: true, uri: objectURL } }; | ||||
} | } | ||||
export { encryptFile, decryptMedia }; | async function decryptThumbhashToDataURL( | ||||
encryptedThumbHash: string, | |||||
keyHex: string, | |||||
): Promise<string> { | |||||
const encryptedData = base64DecodeBuffer(encryptedThumbHash); | |||||
const thumbhashBytes = await AES.decrypt( | |||||
hexToUintArray(keyHex), | |||||
encryptedData, | |||||
); | |||||
return thumbHashToDataURL(thumbhashBytes); | |||||
} | |||||
export { encryptFile, decryptMedia, decryptThumbhashToDataURL }; |