({encryptedFile, unlockKey, uiLogSource})
| 508 | * @return {Object<data, signatures, filename>} - data as JS binary string |
| 509 | */ |
| 510 | export async function decryptFile({encryptedFile, unlockKey, uiLogSource}) { |
| 511 | let armoredMessage; |
| 512 | let binaryMessage; |
| 513 | try { |
| 514 | const content = dataURL2str(encryptedFile.content); |
| 515 | if (/^-----BEGIN PGP MESSAGE-----/.test(content)) { |
| 516 | armoredMessage = content; |
| 517 | } else { |
| 518 | binaryMessage = str2Uint8Array(content); |
| 519 | } |
| 520 | const message = await readMessage({armoredMessage, binaryMessage}); |
| 521 | const encryptionKeyIds = message.getEncryptionKeyIDs(); |
| 522 | const keyring = await getKeyringWithPrivKey(encryptionKeyIds); |
| 523 | if (!keyring) { |
| 524 | throw noKeyFoundError(encryptionKeyIds); |
| 525 | } |
| 526 | const result = await keyring.getPgpBackend().decrypt({base64: () => dataURL2base64(encryptedFile.content), message, keyring, unlockKey: options => unlockKey({message, ...options}), encryptionKeyIds, format: 'binary'}); |
| 527 | await logDecryption(uiLogSource, keyring, encryptionKeyIds); |
| 528 | if (!result.filename) { |
| 529 | result.filename = encryptedFile.name.slice(0, -4); |
| 530 | } |
| 531 | const sigKeyIds = result.signatures.map(sig => sig.fingerprint || sig.keyId); |
| 532 | // sync public keys for the signatures |
| 533 | await syncPublicKeys({keyring, keyIds: sigKeyIds}); |
| 534 | await addSignatureDetails({signatures: result.signatures, keyring}); |
| 535 | return result; |
| 536 | } catch (error) { |
| 537 | console.log('pgpModel.decryptFile() error', error); |
| 538 | throw error; |
| 539 | } |
| 540 | } |
no test coverage detected